| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\I18n;
- use Aura\Intl\Package;
- use Cake\Cache\Cache;
- use Cake\Core\Plugin;
- use Cake\I18n\I18n;
- use Cake\TestSuite\TestCase;
- /**
- * I18nTest class
- */
- class I18nTest extends TestCase
- {
- /**
- * Used to restore the internal locale after tests
- *
- * @var string
- */
- public $locale;
- /**
- * Set Up
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->locale = I18n::locale();
- }
- /**
- * Tear down method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- I18n::clear();
- I18n::defaultFormatter('default');
- I18n::locale($this->locale);
- Plugin::unload();
- Cache::clear(false, '_cake_core_');
- }
- /**
- * Tests that a default translator is created and messages are parsed
- * correctly
- *
- * @return void
- */
- public function testDefaultTranslator()
- {
- $translator = I18n::translator();
- $this->assertInstanceOf('Aura\Intl\Translator', $translator);
- $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
- }
- /**
- * Tests that the translator can automatically load messages from a .mo file
- *
- * @return void
- */
- public function testTranslatorLoadMoFile()
- {
- $translator = I18n::translator('default', 'es_ES');
- $this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
- }
- /**
- * Tests that plural rules are correctly used for the English language
- * using the sprintf formatter
- *
- * @return void
- */
- public function testPluralSelection()
- {
- I18n::defaultFormatter('sprintf');
- $translator = I18n::translator(); // en_US
- $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
- $this->assertEquals('1 is 1 (po translated)', $result);
- $result = $translator->translate('%d = 0 or > 1', ['_count' => 2]);
- $this->assertEquals('2 is 2-4 (po translated)', $result);
- }
- /**
- * Tests that plural rules are correctly used for the English language
- * using the basic formatter
- *
- * @return void
- */
- public function testPluralSelectionBasicFormatter()
- {
- $translator = I18n::translator('special');
- $result = $translator->translate('There are {0} things', ['_count' => 2, 'plenty']);
- $this->assertEquals('There are plenty things', $result);
- $result = $translator->translate('There are {0} things', ['_count' => 1]);
- $this->assertEquals('There is only one', $result);
- }
- /**
- * Test plural rules are used for non-english languages
- *
- * @return void
- */
- public function testPluralSelectionRussian()
- {
- $translator = I18n::translator('default', 'ru');
- $result = $translator->translate('{0} months', ['_count' => 1, 1]);
- $this->assertEquals('1 months ends in 1, not 11', $result);
- $result = $translator->translate('{0} months', ['_count' => 2, 2]);
- $this->assertEquals('2 months ends in 2-4, not 12-14', $result);
- $result = $translator->translate('{0} months', ['_count' => 7, 7]);
- $this->assertEquals('7 months everything else', $result);
- }
- /**
- * Tests that custom translation packages can be created on the fly and used later on
- *
- * @return void
- */
- public function testCreateCustomTranslationPackage()
- {
- I18n::translator('custom', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le moo'
- ]);
- return $package;
- });
- $translator = I18n::translator('custom', 'fr_FR');
- $this->assertEquals('Le moo', $translator->translate('Cow'));
- }
- /**
- * Tests that messages can also be loaded from plugins by using the
- * domain = plugin_name convention
- *
- * @return void
- */
- public function testPluginMesagesLoad()
- {
- Plugin::load([
- 'TestPlugin',
- 'Company/TestPluginThree'
- ]);
- $translator = I18n::translator('test_plugin');
- $this->assertEquals(
- 'Plural Rule 1 (from plugin)',
- $translator->translate('Plural Rule 1')
- );
- $translator = I18n::translator('company/test_plugin_three');
- $this->assertEquals(
- 'String 1 (from plugin three)',
- $translator->translate('String 1')
- );
- }
- /**
- * Tests that messages messages from a plugin can be automatically
- * overridden by messages in app
- *
- * @return void
- */
- public function testPluginOverride()
- {
- Plugin::load('TestTheme');
- $translator = I18n::translator('test_theme');
- $this->assertEquals(
- 'translated',
- $translator->translate('A Message')
- );
- }
- /**
- * Tests the locale method
- *
- * @return void
- */
- public function testDefaultLocale()
- {
- $this->assertEquals('en_US', I18n::locale());
- $this->assertEquals('en_US', ini_get('intl.default_locale'));
- I18n::locale('fr_FR');
- $this->assertEquals('fr_FR', I18n::locale());
- $this->assertEquals('fr_FR', ini_get('intl.default_locale'));
- }
- /**
- * Tests that changing the default locale also changes the way translators
- * are fetched
- *
- * @return void
- */
- public function testGetTranslatorByDefaultLocale()
- {
- I18n::translator('custom', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le moo'
- ]);
- return $package;
- });
- I18n::locale('fr_FR');
- $translator = I18n::translator('custom');
- $this->assertEquals('Le moo', $translator->translate('Cow'));
- }
- /**
- * Tests the __() function
- *
- * @return void
- */
- public function testBasicTranslateFunction()
- {
- I18n::defaultFormatter('sprintf');
- $this->assertEquals('%d is 1 (po translated)', __('%d = 1'));
- $this->assertEquals('1 is 1 (po translated)', __('%d = 1', 1));
- }
- /**
- * Tests the __n() function
- *
- * @return void
- */
- public function testBasicTranslatePluralFunction()
- {
- I18n::defaultFormatter('sprintf');
- $result = __n('singular msg', '%d = 0 or > 1', 1);
- $this->assertEquals('1 is 1 (po translated)', $result);
- $result = __n('singular msg', '%d = 0 or > 1', 2);
- $this->assertEquals('2 is 2-4 (po translated)', $result);
- }
- /**
- * Tests the __d() function
- *
- * @return void
- */
- public function testBasicDomainFunction()
- {
- I18n::translator('custom', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le moo',
- 'The {0} is tasty' => 'The {0} is delicious',
- 'Average price {0}' => 'Price Average {0}',
- ]);
- return $package;
- });
- $this->assertEquals('Le moo', __d('custom', 'Cow'));
- $result = __d('custom', 'The {0} is tasty', ['fruit']);
- $this->assertEquals('The fruit is delicious', $result);
- $result = __d('custom', 'Average price {0}', ['9.99']);
- $this->assertEquals('Price Average 9.99', $result);
- }
- /**
- * Tests the __dn() function
- *
- * @return void
- */
- public function testBasicDomainPluralFunction()
- {
- I18n::translator('custom', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le Moo',
- 'Cows' => [
- 'Le Moo',
- 'Les Moos'
- ]
- ]);
- return $package;
- });
- $this->assertEquals('Le Moo', __dn('custom', 'Cow', 'Cows', 1));
- $this->assertEquals('Les Moos', __dn('custom', 'Cow', 'Cows', 2));
- }
- /**
- * Tests the __x() function
- *
- * @return void
- */
- public function testBasicContextFunction()
- {
- I18n::translator('default', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'letter' => [
- '_context' => [
- 'character' => 'The letter {0}',
- 'communication' => 'She wrote a letter to {0}'
- ]
- ],
- 'letters' => [
- '_context' => [
- 'character' => [
- 'The letter {0}',
- 'The letters {0} and {1}'
- ],
- 'communication' => [
- 'She wrote a letter to {0}',
- 'She wrote a letter to {0} and {1}'
- ]
- ]
- ]
- ]);
- return $package;
- });
- $this->assertEquals('The letters A and B', __x('character', 'letters', ['A', 'B']));
- $this->assertEquals('The letter A', __x('character', 'letter', ['A']));
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __x('communication', 'letters', ['Thomas', 'Sara'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __x('communication', 'letter', ['Thomas'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __x('communication', 'letters', 'Thomas', 'Sara')
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __x('communication', 'letter', 'Thomas')
- );
- }
- /**
- * Tests the __x() function with no msgstr
- *
- * @return void
- */
- public function testBasicContextFunctionNoString()
- {
- I18n::translator('default', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'letter' => [
- '_context' => [
- 'character' => '',
- ]
- ]
- ]);
- return $package;
- });
- $this->assertEquals('', __x('character', 'letter'));
- }
- /**
- * Tests the __xn() function
- *
- * @return void
- */
- public function testPluralContextFunction()
- {
- I18n::translator('default', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'letter' => [
- '_context' => [
- 'character' => 'The letter {0}',
- 'communication' => 'She wrote a letter to {0}',
- ]
- ],
- 'letters' => [
- '_context' => [
- 'character' => [
- 'The letter {0}',
- 'The letters {0} and {1}'
- ],
- 'communication' => [
- 'She wrote a letter to {0}',
- 'She wrote a letter to {0} and {1}'
- ]
- ]
- ]
- ]);
- return $package;
- });
- $this->assertEquals('The letters A and B', __xn('character', 'letter', 'letters', 2, ['A', 'B']));
- $this->assertEquals('The letter A', __xn('character', 'letter', 'letters', 1, ['A']));
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __xn('communication', 'letter', 'letters', 2, ['Thomas', 'Sara'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __xn('communication', 'letter', 'letters', 1, ['Thomas'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __xn('communication', 'letter', 'letters', 2, 'Thomas', 'Sara')
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __xn('communication', 'letter', 'letters', 1, 'Thomas')
- );
- }
- /**
- * Tests the __dx() function
- *
- * @return void
- */
- public function testDomainContextFunction()
- {
- I18n::translator('custom', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'letter' => [
- '_context' => [
- 'character' => 'The letter {0}',
- 'communication' => 'She wrote a letter to {0}'
- ]
- ],
- 'letters' => [
- '_context' => [
- 'character' => [
- 'The letter {0}',
- 'The letters {0} and {1}'
- ],
- 'communication' => [
- 'She wrote a letter to {0}',
- 'She wrote a letter to {0} and {1}'
- ]
- ]
- ]
- ]);
- return $package;
- });
- $this->assertEquals('The letters A and B', __dx('custom', 'character', 'letters', ['A', 'B']));
- $this->assertEquals('The letter A', __dx('custom', 'character', 'letter', ['A']));
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __dx('custom', 'communication', 'letters', ['Thomas', 'Sara'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __dx('custom', 'communication', 'letter', ['Thomas'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __dx('custom', 'communication', 'letters', 'Thomas', 'Sara')
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __dx('custom', 'communication', 'letter', 'Thomas')
- );
- }
- /**
- * Tests the __dxn() function
- *
- * @return void
- */
- public function testDomainPluralContextFunction()
- {
- I18n::translator('custom', 'en_US', function () {
- $package = new Package('default');
- $package->setMessages([
- 'letter' => [
- '_context' => [
- 'character' => 'The letter {0}',
- 'communication' => 'She wrote a letter to {0}',
- ]
- ],
- 'letters' => [
- '_context' => [
- 'character' => [
- 'The letter {0}',
- 'The letters {0} and {1}'
- ],
- 'communication' => [
- 'She wrote a letter to {0}',
- 'She wrote a letter to {0} and {1}'
- ]
- ]
- ]
- ]);
- return $package;
- });
- $this->assertEquals(
- 'The letters A and B',
- __dxn('custom', 'character', 'letter', 'letters', 2, ['A', 'B'])
- );
- $this->assertEquals(
- 'The letter A',
- __dxn('custom', 'character', 'letter', 'letters', 1, ['A'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __dxn('custom', 'communication', 'letter', 'letters', 2, ['Thomas', 'Sara'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __dxn('custom', 'communication', 'letter', 'letters', 1, ['Thomas'])
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas and Sara',
- __dxn('custom', 'communication', 'letter', 'letters', 2, 'Thomas', 'Sara')
- );
- $this->assertEquals(
- 'She wrote a letter to Thomas',
- __dxn('custom', 'communication', 'letter', 'letters', 1, 'Thomas')
- );
- }
- /**
- * Tests that translators are cached for performance
- *
- * @return void
- */
- public function testTranslatorCache()
- {
- $english = I18n::translator();
- $spanish = I18n::translator('default', 'es_ES');
- $cached = Cache::read('translations.default.en_US', '_cake_core_');
- $this->assertEquals($english, $cached);
- $cached = Cache::read('translations.default.es_ES', '_cake_core_');
- $this->assertEquals($spanish, $cached);
- $this->assertSame($english, I18n::translator());
- $this->assertSame($spanish, I18n::translator('default', 'es_ES'));
- $this->assertSame($english, I18n::translator());
- }
- /**
- * Tests that it is possible to register a generic translators factory for a domain
- * instead of having to create them manually
- *
- * @return void
- */
- public function testloaderFactory()
- {
- I18n::config('custom', function ($name, $locale) {
- $this->assertEquals('custom', $name);
- $package = new Package('default');
- if ($locale == 'fr_FR') {
- $package->setMessages([
- 'Cow' => 'Le Moo',
- 'Cows' => [
- 'Le Moo',
- 'Les Moos'
- ]
- ]);
- }
- if ($locale === 'es_ES') {
- $package->setMessages([
- 'Cow' => 'El Moo',
- 'Cows' => [
- 'El Moo',
- 'Los Moos'
- ]
- ]);
- }
- return $package;
- });
- $translator = I18n::translator('custom', 'fr_FR');
- $this->assertEquals('Le Moo', $translator->translate('Cow'));
- $this->assertEquals('Les Moos', $translator->translate('Cows', ['_count' => 2]));
- $translator = I18n::translator('custom', 'es_ES');
- $this->assertEquals('El Moo', $translator->translate('Cow'));
- $this->assertEquals('Los Moos', $translator->translate('Cows', ['_count' => 2]));
- $translator = I18n::translator();
- $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
- }
- /**
- * Tests that missing translations will get fallbacked to the default translator
- *
- * @return void
- */
- public function testFallbackTranslator()
- {
- I18n::translator('default', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Dog' => 'Le bark'
- ]);
- return $package;
- });
- I18n::translator('custom', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le moo'
- ]);
- return $package;
- });
- $translator = I18n::translator('custom', 'fr_FR');
- $this->assertEquals('Le moo', $translator->translate('Cow'));
- $this->assertEquals('Le bark', $translator->translate('Dog'));
- }
- /**
- * Test that the translation fallback can be disabled
- *
- * @return void
- */
- public function testFallbackTranslatorDisabled()
- {
- I18n::useFallback(false);
- I18n::translator('default', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages(['Dog' => 'Le bark']);
- return $package;
- });
- I18n::translator('custom', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages(['Cow' => 'Le moo']);
- return $package;
- });
- $translator = I18n::translator('custom', 'fr_FR');
- $this->assertEquals('Le moo', $translator->translate('Cow'));
- $this->assertEquals('Dog', $translator->translate('Dog'));
- }
- /**
- * Tests that it is possible to register a generic translators factory for a domain
- * instead of having to create them manually
- *
- * @return void
- */
- public function testFallbackTranslatorWithFactory()
- {
- I18n::translator('default', 'fr_FR', function () {
- $package = new Package('default');
- $package->setMessages([
- 'Dog' => 'Le bark'
- ]);
- return $package;
- });
- I18n::config('custom', function ($name, $locale) {
- $this->assertEquals('custom', $name);
- $package = new Package('default');
- $package->setMessages([
- 'Cow' => 'Le moo',
- ]);
- return $package;
- });
- $translator = I18n::translator('custom', 'fr_FR');
- $this->assertEquals('Le moo', $translator->translate('Cow'));
- $this->assertEquals('Le bark', $translator->translate('Dog'));
- }
- }
|