From e6b6a9d33aab6fc3ac4f918cc8e0a3c6931a66db Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 21 Aug 2019 10:02:59 +0200 Subject: [PATCH] deprecate support for null locales --- UPGRADE-4.4.md | 5 ++ UPGRADE-5.0.md | 1 + .../Component/Translation/CHANGELOG.md | 5 ++ .../Translation/Tests/TranslatorTest.php | 48 +++++++++++++++---- .../Component/Translation/Translator.php | 13 ++++- 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/UPGRADE-4.4.md b/UPGRADE-4.4.md index 2189f6b231..c7470622be 100644 --- a/UPGRADE-4.4.md +++ b/UPGRADE-4.4.md @@ -189,6 +189,11 @@ Stopwatch * Deprecated passing `null` as 1st (`$id`) argument of `Section::get()` method, pass a valid child section identifier instead. +Translation +----------- + + * Deprecated support for using `null` as the locale in `Translator`. + TwigBridge ---------- diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index c03573af53..6ccb845144 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -518,6 +518,7 @@ Stopwatch Translation ----------- + * Support for using `null` as the locale in `Translator` has been removed. * The `FileDumper::setBackup()` method has been removed. * The `TranslationWriter::disableBackup()` method has been removed. * The `TranslatorInterface` has been removed in favor of `Symfony\Contracts\Translation\TranslatorInterface` diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index c80716838b..2cd4bfc633 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.4.0 +----- + + * deprecated support for using `null` as the locale in `Translator` + 4.3.0 ----- diff --git a/src/Symfony/Component/Translation/Tests/TranslatorTest.php b/src/Symfony/Component/Translation/Tests/TranslatorTest.php index a482c782ac..87603a9dc8 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorTest.php @@ -34,9 +34,12 @@ class TranslatorTest extends TestCase { $translator = new Translator($locale); - $this->assertEquals($locale, $translator->getLocale()); + $this->assertSame($locale, $translator->getLocale()); } + /** + * @group legacy + */ public function testConstructorWithoutLocale() { $translator = new Translator(null); @@ -75,6 +78,17 @@ class TranslatorTest extends TestCase $this->assertEquals($locale, $translator->getLocale()); } + /** + * @group legacy + */ + public function testSetNullLocale() + { + $translator = new Translator('en'); + $translator->setLocale(null); + + $this->assertNull($translator->getLocale()); + } + public function testGetCatalogue() { $translator = new Translator('en'); @@ -158,6 +172,17 @@ class TranslatorTest extends TestCase $this->addToAssertionCount(1); } + /** + * @group legacy + */ + public function testSetNullFallbackLocale() + { + $translator = new Translator('en'); + $translator->setFallbackLocales(['fr', null]); + // no assertion. this method just asserts that no exception is thrown + $this->addToAssertionCount(1); + } + public function testTransWithFallbackLocale() { $translator = new Translator('fr_FR'); @@ -184,9 +209,6 @@ class TranslatorTest extends TestCase */ public function testAddResourceValidLocales($locale) { - if (null === $locale) { - $this->markTestSkipped('null is not a valid locale'); - } $translator = new Translator('fr'); $translator->addResource('array', ['foo' => 'foofoo'], $locale); // no assertion. this method just asserts that no exception is thrown @@ -382,9 +404,6 @@ class TranslatorTest extends TestCase */ public function testTransValidLocale($locale) { - if (null === $locale) { - $this->markTestSkipped('null is not a valid locale'); - } $translator = new Translator($locale); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', ['test' => 'OK'], $locale); @@ -458,6 +477,20 @@ class TranslatorTest extends TestCase $this->addToAssertionCount(1); } + /** + * @group legacy + */ + public function testTransChoiceNullLocale() + { + $translator = new Translator('en'); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['foo' => 'foofoo'], 'en'); + + $translator->transChoice('foo', 1, [], '', null); + // no assertion. this method just asserts that no exception is thrown + $this->addToAssertionCount(1); + } + public function getTransFileTests() { return [ @@ -552,7 +585,6 @@ class TranslatorTest extends TestCase { return [ [''], - [null], ['fr'], ['francais'], ['FR'], diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 048e5b68f9..47784bd8af 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -88,7 +88,11 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran */ public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false) { - $this->setLocale($locale); + if (null === $locale) { + @trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED); + } + + $this->setLocale($locale, false); if (null === $formatter) { $formatter = new MessageFormatter(); @@ -151,6 +155,10 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran */ public function setLocale($locale) { + if (null === $locale && (2 > \func_num_args() || func_get_arg(1))) { + @trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED); + } + $this->assertValidLocale($locale); $this->locale = $locale; } @@ -176,6 +184,9 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran $this->catalogues = []; foreach ($locales as $locale) { + if (null === $locale) { + @trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED); + } $this->assertValidLocale($locale); }