deprecate support for null locales

This commit is contained in:
Christian Flothmann 2019-08-21 10:02:59 +02:00
parent 5a753b1428
commit e6b6a9d33a
5 changed files with 63 additions and 9 deletions

View File

@ -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
----------

View File

@ -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`

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.4.0
-----
* deprecated support for using `null` as the locale in `Translator`
4.3.0
-----

View File

@ -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'],

View File

@ -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);
}