diff --git a/UPGRADE-4.4.md b/UPGRADE-4.4.md index 2189f6b231120..c7470622be773 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 c03573af53372..6ccb845144e4e 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 c80716838b4b6..2cd4bfc6330af 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 a482c782acdc4..87603a9dc867b 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorTest.php @@ -34,9 +34,12 @@ public function testConstructorValidLocale($locale) { $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 @@ public function testSetValidLocale($locale) $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 @@ public function testSetFallbackValidLocales($locale) $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 @@ public function testAddResourceInvalidLocales($locale) */ 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 @@ public function testTransInvalidLocale($locale) */ 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 @@ public function testTransChoiceValidLocale($locale) $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 @@ public function getValidLocalesTests() { return [ [''], - [null], ['fr'], ['francais'], ['FR'], diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 048e5b68f9315..47784bd8af214 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 @@ public function addResource($format, $resource, $locale, $domain = null) */ 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 @@ public function setFallbackLocales(array $locales) $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); }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: