Browse Source

Update uses of translator() to use non-deprecated method.

Mark Story 8 years ago
parent
commit
73dc38c09e
3 changed files with 75 additions and 73 deletions
  1. 11 10
      src/I18n/I18n.php
  2. 8 8
      src/I18n/functions.php
  3. 56 55
      tests/TestCase/I18n/I18nTest.php

+ 11 - 10
src/I18n/I18n.php

@@ -92,13 +92,13 @@ class I18n
      * ### Example:
      *
      * ```
-     *  I18n::translator('default', 'fr_FR', function () {
+     *  I18n::setTranslator('default', function () {
      *      $package = new \Aura\Intl\Package();
      *      $package->setMessages([
      *          'Cake' => 'Gâteau'
      *      ]);
      *      return $package;
-     *  });
+     *  }, 'fr_FR');
      *
      *  $translator = I18n::translator('default', 'fr_FR');
      *  echo $translator->translate('Cake');
@@ -162,14 +162,14 @@ class I18n
      * ```
      * I18n::setTranslator(
      *  'default',
-     *  new MessagesFileLoader('my_translations', 'custom', 'po');
+     *  new MessagesFileLoader('my_translations', 'custom', 'po'),
      *  'fr_FR'
      * );
      * ```
      *
      * @param string $name The domain of the translation messages.
      * @param callable|null $loader A callback function or callable class responsible for
-     * constructing a translations package instance.
+     *   constructing a translations package instance.
      * @param string|null $locale The locale for the translator.
      * @return void
      */
@@ -177,15 +177,16 @@ class I18n
     {
         $locale = $locale ?: static::getLocale();
 
-        $loader = static::translators()->setLoaderFallback($name, $loader);
-
-        $packages = static::translators()->getPackages();
+        $translators = static::translators();
+        $loader = $translators->setLoaderFallback($name, $loader);
+        $packages = $translators->getPackages();
         $packages->set($name, $locale, $loader);
     }
 
     /**
-     * Returns an instance of a translator that was configured for the name and passed
-     * locale. If no locale is passed then it takes the value returned by the `getLocale()` method.
+     * Returns an instance of a translator that was configured for the name and locale.
+     *
+     * If no locale is passed then it takes the value returned by the `getLocale()` method.
      *
      * @param string $name The domain of the translation messages.
      * @param string|null $locale The locale for the translator.
@@ -197,7 +198,7 @@ class I18n
 
         if ($locale) {
             $currentLocale = $translators->getLocale();
-            static::translators()->setLocale($locale);
+            $translators->setLocale($locale);
         }
 
         $translator = $translators->get($name);

+ 8 - 8
src/I18n/functions.php

@@ -32,7 +32,7 @@ if (!function_exists('__')) {
             $args = $args[0];
         }
 
-        return I18n::translator()->translate($singular, $args);
+        return I18n::getTranslator()->translate($singular, $args);
     }
 
 }
@@ -58,7 +58,7 @@ if (!function_exists('__n')) {
             $args = $args[0];
         }
 
-        return I18n::translator()->translate(
+        return I18n::getTranslator()->translate(
             $plural,
             ['_count' => $count, '_singular' => $singular] + $args
         );
@@ -85,7 +85,7 @@ if (!function_exists('__d')) {
             $args = $args[0];
         }
 
-        return I18n::translator($domain)->translate($msg, $args);
+        return I18n::getTranslator($domain)->translate($msg, $args);
     }
 
 }
@@ -113,7 +113,7 @@ if (!function_exists('__dn')) {
             $args = $args[0];
         }
 
-        return I18n::translator($domain)->translate(
+        return I18n::getTranslator($domain)->translate(
             $plural,
             ['_count' => $count, '_singular' => $singular] + $args
         );
@@ -142,7 +142,7 @@ if (!function_exists('__x')) {
             $args = $args[0];
         }
 
-        return I18n::translator()->translate($singular, ['_context' => $context] + $args);
+        return I18n::getTranslator()->translate($singular, ['_context' => $context] + $args);
     }
 
 }
@@ -171,7 +171,7 @@ if (!function_exists('__xn')) {
             $args = $args[0];
         }
 
-        return I18n::translator()->translate(
+        return I18n::getTranslator()->translate(
             $plural,
             ['_count' => $count, '_singular' => $singular, '_context' => $context] + $args
         );
@@ -201,7 +201,7 @@ if (!function_exists('__dx')) {
             $args = $args[0];
         }
 
-        return I18n::translator($domain)->translate(
+        return I18n::getTranslator($domain)->translate(
             $msg,
             ['_context' => $context] + $args
         );
@@ -234,7 +234,7 @@ if (!function_exists('__dxn')) {
             $args = $args[0];
         }
 
-        return I18n::translator($domain)->translate(
+        return I18n::getTranslator($domain)->translate(
             $plural,
             ['_count' => $count, '_singular' => $singular, '_context' => $context] + $args
         );

+ 56 - 55
tests/TestCase/I18n/I18nTest.php

@@ -65,11 +65,12 @@ class I18nTest extends TestCase
      *
      * @return void
      */
-    public function testDefaultTranslator()
+    public function testGetDefaultTranslator()
     {
-        $translator = I18n::translator();
+        $translator = I18n::getTranslator();
         $this->assertInstanceOf('Aura\Intl\TranslatorInterface', $translator);
         $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
+        $this->assertSame($translator, I18n::translator(), 'backwards compat works');
     }
 
     /**
@@ -77,9 +78,9 @@ class I18nTest extends TestCase
      *
      * @return void
      */
-    public function testTranslatorLoadMoFile()
+    public function testGetTranslatorLoadMoFile()
     {
-        $translator = I18n::translator('default', 'es_ES');
+        $translator = I18n::getTranslator('default', 'es_ES');
         $this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
     }
 
@@ -92,7 +93,7 @@ class I18nTest extends TestCase
     public function testPluralSelection()
     {
         I18n::defaultFormatter('sprintf');
-        $translator = I18n::translator(); // en_US
+        $translator = I18n::getTranslator(); // en_US
         $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
         $this->assertEquals('1 is 1 (po translated)', $result);
 
@@ -108,7 +109,7 @@ class I18nTest extends TestCase
      */
     public function testPluralSelectionBasicFormatter()
     {
-        $translator = I18n::translator('special');
+        $translator = I18n::getTranslator('special');
         $result = $translator->translate('There are {0} things', ['_count' => 2, 'plenty']);
         $this->assertEquals('There are plenty things', $result);
 
@@ -123,7 +124,7 @@ class I18nTest extends TestCase
      */
     public function testPluralSelectionRussian()
     {
-        $translator = I18n::translator('default', 'ru');
+        $translator = I18n::getTranslator('default', 'ru');
         $result = $translator->translate('{0} months', ['_count' => 1, 1]);
         $this->assertEquals('1 months ends in 1, not 11', $result);
 
@@ -141,16 +142,16 @@ class I18nTest extends TestCase
      */
     public function testCreateCustomTranslationPackage()
     {
-        I18n::translator('custom', 'fr_FR', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Cow' => 'Le moo'
             ]);
 
             return $package;
-        });
+        }, 'fr_FR');
 
-        $translator = I18n::translator('custom', 'fr_FR');
+        $translator = I18n::getTranslator('custom', 'fr_FR');
         $this->assertEquals('Le moo', $translator->translate('Cow'));
     }
 
@@ -167,13 +168,13 @@ class I18nTest extends TestCase
             'Company/TestPluginThree'
         ]);
 
-        $translator = I18n::translator('test_plugin');
+        $translator = I18n::getTranslator('test_plugin');
         $this->assertEquals(
             'Plural Rule 1 (from plugin)',
             $translator->translate('Plural Rule 1')
         );
 
-        $translator = I18n::translator('company/test_plugin_three');
+        $translator = I18n::getTranslator('company/test_plugin_three');
         $this->assertEquals(
             'String 1 (from plugin three)',
             $translator->translate('String 1')
@@ -189,7 +190,7 @@ class I18nTest extends TestCase
     public function testPluginOverride()
     {
         Plugin::load('TestTheme');
-        $translator = I18n::translator('test_theme');
+        $translator = I18n::getTranslator('test_theme');
         $this->assertEquals(
             'translated',
             $translator->translate('A Message')
@@ -218,17 +219,17 @@ class I18nTest extends TestCase
      */
     public function testGetTranslatorByDefaultLocale()
     {
-        I18n::translator('custom', 'fr_FR', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Cow' => 'Le moo'
             ]);
 
             return $package;
-        });
+        }, 'fr_FR');
 
         I18n::locale('fr_FR');
-        $translator = I18n::translator('custom');
+        $translator = I18n::getTranslator('custom');
         $this->assertEquals('Le moo', $translator->translate('Cow'));
     }
 
@@ -318,7 +319,7 @@ class I18nTest extends TestCase
      */
     public function testBasicDomainFunction()
     {
-        I18n::translator('custom', 'en_US', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Cow' => 'Le moo',
@@ -327,7 +328,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
         $this->assertEquals('Le moo', __d('custom', 'Cow'));
 
         $result = __d('custom', 'The {0} is tasty', ['fruit']);
@@ -347,7 +348,7 @@ class I18nTest extends TestCase
      */
     public function testBasicDomainPluralFunction()
     {
-        I18n::translator('custom', 'en_US', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Cow' => 'Le Moo',
@@ -358,7 +359,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
         $this->assertEquals('Le Moo', __dn('custom', 'Cow', 'Cows', 1));
         $this->assertEquals('Les Moos', __dn('custom', 'Cow', 'Cows', 2));
     }
@@ -370,7 +371,7 @@ class I18nTest extends TestCase
      */
     public function testBasicContextFunction()
     {
-        I18n::translator('default', 'en_US', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -394,7 +395,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
 
         $this->assertEquals('The letters A and B', __x('character', 'letters', ['A', 'B']));
         $this->assertEquals('The letter A', __x('character', 'letter', ['A']));
@@ -428,7 +429,7 @@ class I18nTest extends TestCase
      */
     public function testBasicContextFunctionNoString()
     {
-        I18n::translator('default', 'en_US', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -439,7 +440,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
 
         $this->assertEquals('', __x('character', 'letter'));
     }
@@ -451,7 +452,7 @@ class I18nTest extends TestCase
      */
     public function testBasicContextFunctionInvalidContext()
     {
-        I18n::translator('default', 'en_US', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -462,7 +463,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
 
         $this->assertEquals('letter', __x('garbage', 'letter'));
         $this->assertEquals('a paper letter', __('letter'));
@@ -475,7 +476,7 @@ class I18nTest extends TestCase
      */
     public function testPluralContextFunction()
     {
-        I18n::translator('default', 'en_US', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -499,7 +500,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
         $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']));
 
@@ -529,7 +530,7 @@ class I18nTest extends TestCase
      */
     public function testDomainContextFunction()
     {
-        I18n::translator('custom', 'en_US', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -553,7 +554,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
 
         $this->assertEquals('The letters A and B', __dx('custom', 'character', 'letters', ['A', 'B']));
         $this->assertEquals('The letter A', __dx('custom', 'character', 'letter', ['A']));
@@ -584,7 +585,7 @@ class I18nTest extends TestCase
      */
     public function testDomainPluralContextFunction()
     {
-        I18n::translator('custom', 'en_US', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'letter' => [
@@ -608,7 +609,7 @@ class I18nTest extends TestCase
             ]);
 
             return $package;
-        });
+        }, 'en_US');
         $this->assertEquals(
             'The letters A and B',
             __dxn('custom', 'character', 'letter', 'letters', 2, ['A', 'B'])
@@ -644,8 +645,8 @@ class I18nTest extends TestCase
      */
     public function testTranslatorCache()
     {
-        $english = I18n::translator();
-        $spanish = I18n::translator('default', 'es_ES');
+        $english = I18n::getTranslator();
+        $spanish = I18n::getTranslator('default', 'es_ES');
 
         $cached = Cache::read('translations.default.en_US', '_cake_core_');
         $this->assertEquals($english, $cached);
@@ -653,9 +654,9 @@ class I18nTest extends TestCase
         $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());
+        $this->assertSame($english, I18n::getTranslator());
+        $this->assertSame($spanish, I18n::getTranslator('default', 'es_ES'));
+        $this->assertSame($english, I18n::getTranslator());
     }
 
     /**
@@ -693,15 +694,15 @@ class I18nTest extends TestCase
             return $package;
         });
 
-        $translator = I18n::translator('custom', 'fr_FR');
+        $translator = I18n::getTranslator('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');
+        $translator = I18n::getTranslator('custom', 'es_ES');
         $this->assertEquals('El Moo', $translator->translate('Cow'));
         $this->assertEquals('Los Moos', $translator->translate('Cows', ['_count' => 2]));
 
-        $translator = I18n::translator();
+        $translator = I18n::getTranslator();
         $this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
     }
 
@@ -728,10 +729,10 @@ class I18nTest extends TestCase
             return $package;
         });
 
-        $translator = I18n::translator('custom');
+        $translator = I18n::getTranslator('custom');
         $this->assertEquals('Le Moo custom', $translator->translate('Cow'));
 
-        $translator = I18n::translator();
+        $translator = I18n::getTranslator();
         $this->assertEquals('Le Moo default', $translator->translate('Cow'));
     }
 
@@ -742,25 +743,25 @@ class I18nTest extends TestCase
      */
     public function testFallbackTranslator()
     {
-        I18n::translator('default', 'fr_FR', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Dog' => 'Le bark'
             ]);
 
             return $package;
-        });
+        }, 'fr_FR');
 
-        I18n::translator('custom', 'fr_FR', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Cow' => 'Le moo'
             ]);
 
             return $package;
-        });
+        }, 'fr_FR');
 
-        $translator = I18n::translator('custom', 'fr_FR');
+        $translator = I18n::getTranslator('custom', 'fr_FR');
         $this->assertEquals('Le moo', $translator->translate('Cow'));
         $this->assertEquals('Le bark', $translator->translate('Dog'));
     }
@@ -774,21 +775,21 @@ class I18nTest extends TestCase
     {
         I18n::useFallback(false);
 
-        I18n::translator('default', 'fr_FR', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages(['Dog' => 'Le bark']);
 
             return $package;
-        });
+        }, 'fr_FR');
 
-        I18n::translator('custom', 'fr_FR', function () {
+        I18n::setTranslator('custom', function () {
             $package = new Package('default');
             $package->setMessages(['Cow' => 'Le moo']);
 
             return $package;
-        });
+        }, 'fr_FR');
 
-        $translator = I18n::translator('custom', 'fr_FR');
+        $translator = I18n::getTranslator('custom', 'fr_FR');
         $this->assertEquals('Le moo', $translator->translate('Cow'));
         $this->assertEquals('Dog', $translator->translate('Dog'));
     }
@@ -801,14 +802,14 @@ class I18nTest extends TestCase
      */
     public function testFallbackTranslatorWithFactory()
     {
-        I18n::translator('default', 'fr_FR', function () {
+        I18n::setTranslator('default', function () {
             $package = new Package('default');
             $package->setMessages([
                 'Dog' => 'Le bark'
             ]);
 
             return $package;
-        });
+        }, 'fr_FR');
         I18n::config('custom', function ($name, $locale) {
             $this->assertEquals('custom', $name);
             $package = new Package('default');
@@ -819,7 +820,7 @@ class I18nTest extends TestCase
             return $package;
         });
 
-        $translator = I18n::translator('custom', 'fr_FR');
+        $translator = I18n::getTranslator('custom', 'fr_FR');
         $this->assertEquals('Le moo', $translator->translate('Cow'));
         $this->assertEquals('Le bark', $translator->translate('Dog'));
     }