mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Switch to ICU MessageFormat (#2759)
This commit is contained in:
committed by
GitHub
parent
9461df8803
commit
b5ee8a034b
2
framework/core/tests/fixtures/locales/en+intl-icu.yml
vendored
Normal file
2
framework/core/tests/fixtures/locales/en+intl-icu.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
test:
|
||||
hello-intl: World-intl {name}
|
23
framework/core/tests/fixtures/locales/en.yml
vendored
Normal file
23
framework/core/tests/fixtures/locales/en.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
test:
|
||||
hello: World {name}
|
||||
party-invitation: | # From https://symfony.com/doc/current/translation/message_format.html#pluralization
|
||||
{gender_of_host, select,
|
||||
female {{num_guests, plural, offset:1
|
||||
=0 {{host} does not give a party.}
|
||||
=1 {{host} invites {guest} to her party.}
|
||||
=2 {{host} invites {guest} and one other person to her party.}
|
||||
other {{host} invites {guest} and # other people to her party.}
|
||||
}}
|
||||
male {{num_guests, plural, offset:1
|
||||
=0 {{host} does not give a party.}
|
||||
=1 {{host} invites {guest} to his party.}
|
||||
=2 {{host} invites {guest} and one other person to his party.}
|
||||
other {{host} invites {guest} and # other people to his party.}
|
||||
}}
|
||||
other {{num_guests, plural, offset:1
|
||||
=0 {{host} does not give a party.}
|
||||
=1 {{host} invites {guest} to their party.}
|
||||
=2 {{host} invites {guest} and one other person to their party.}
|
||||
other {{host} invites {guest} and # other people to their party.}
|
||||
}}
|
||||
}
|
105
framework/core/tests/integration/extenders/LocalesTest.php
Normal file
105
framework/core/tests/integration/extenders/LocalesTest.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Locale\Translator;
|
||||
use Flarum\Testing\integration\TestCase;
|
||||
|
||||
class LocalesTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
array_map('unlink', glob($this->tmpDir().'/storage/locale/*'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_translation_does_not_exist_by_default()
|
||||
{
|
||||
$this->app()->getContainer()->make('flarum.locales');
|
||||
$translator = $this->app()->getContainer()->make(Translator::class);
|
||||
|
||||
$this->assertEquals('test.hello', $translator->trans('test.hello', ['name' => 'ACME']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_translation_exists_if_added()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Locales(dirname(__FILE__, 3).'/fixtures/locales'))
|
||||
);
|
||||
|
||||
$this->app()->getContainer()->make('flarum.locales');
|
||||
$translator = $this->app()->getContainer()->make(Translator::class);
|
||||
|
||||
$this->assertEquals('World ACME', $translator->trans('test.hello', ['name' => 'ACME']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_translation_exists_if_added_with_intl_suffix()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Locales(dirname(__FILE__, 3).'/fixtures/locales'))
|
||||
);
|
||||
|
||||
$this->app()->getContainer()->make('flarum.locales');
|
||||
$translator = $this->app()->getContainer()->make(Translator::class);
|
||||
|
||||
$this->assertEquals('World-intl ACME', $translator->trans('test.hello-intl', ['name' => 'ACME']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function messageformat_works_in_translations()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Locales(dirname(__FILE__, 3).'/fixtures/locales'))
|
||||
);
|
||||
|
||||
$this->app()->getContainer()->make('flarum.locales');
|
||||
$translator = $this->app()->getContainer()->make(Translator::class);
|
||||
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->trans('test.party-invitation', ['gender_of_host' => 'female', 'host' => 'ACME', 'num_guests' => 2, 'guest' => 'ACME2']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function laravel_interface_methods_work()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Locales(dirname(__FILE__, 3).'/fixtures/locales'))
|
||||
);
|
||||
|
||||
$this->app()->getContainer()->make('flarum.locales');
|
||||
$translator = $this->app()->getContainer()->make(Translator::class);
|
||||
|
||||
$args = ['gender_of_host' => 'female', 'host' => 'ACME', 'num_guests' => 2, 'guest' => 'ACME2'];
|
||||
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->get('test.party-invitation', $args));
|
||||
// Number doesn't matter
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->choice('test.party-invitation', 2, $args));
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->choice('test.party-invitation', 50, $args));
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->choice('test.party-invitation', -1000, $args));
|
||||
$this->assertEquals('ACME invites ACME2 and one other person to her party.', $translator->choice('test.party-invitation', null, $args));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user