8000 [Translation] deprecate support for null locales by xabbuh · Pull Request #33272 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] deprecate support for null locales #33272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------

Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* deprecated support for using `null` as the locale in `Translator`

4.3.0
-----

Expand Down
48 changes: 40 additions & 8 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -552,7 +585,6 @@ public function getValidLocalesTests()
{
return [
[''],
[null],
['fr'],
['francais'],
['FR'],
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
0