8000 feature #33272 [Translation] deprecate support for null locales (xabbuh) · symfony/symfony@7046cac · GitHub
[go: up one dir, main page]

Skip to content

Commit 7046cac

Browse files
committed
feature #33272 [Translation] deprecate support for null locales (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Translation] deprecate support for null locales | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- e6b6a9d deprecate support for null locales
2 parents 7aedfe7 + e6b6a9d commit 7046cac

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

UPGRADE-4.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ Stopwatch
194194

195195
* Deprecated passing `null` as 1st (`$id`) argument of `Section::get()` method, pass a valid child section identifier instead.
196196

197+
Translation
198+
-----------
199+
200+
* Deprecated support for using `null` as the locale in `Translator`.
201+
197202
TwigBridge
198203
----------
199204

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ Stopwatch
518518
Translation
519519
-----------
520520

521+
* Support for using `null` as the locale in `Translator` has been removed.
521522
* The `FileDumper::setBackup()` method has been removed.
522523
* The `TranslationWriter::disableBackup()` method has been removed.
523524
* The `TranslatorInterface` has been removed in favor of `Symfony\Contracts\Translation\TranslatorInterface`

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* deprecated support for using `null` as the locale in `Translator`
8+
49
4.3.0
510
-----
611

src/Symfony/Component/Translation/Tests/TranslatorTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public function testConstructorValidLocale($locale)
3434
{
3535
$translator = new Translator($locale);
3636

37-
$this->assertEquals($locale, $translator->getLocale());
37+
$this->assertSame($locale, $translator->getLocale());
3838
}
3939

40+
/**
41+
* @group legacy
42+
*/
4043
public function testConstructorWithoutLocale()
4144
{
4245
$translator = new Translator(null);
@@ -75,6 +78,17 @@ public function testSetValidLocale($locale)
7578
$this->assertEquals($locale, $translator->getLocale());
7679
}
7780

81+
/**
82+
* @group legacy
83+
*/
84+
public function testSetNullLocale()
85+
{
86+
$translator = new Translator('en');
87+
$translator->setLocale(null);
88+
89+
$this->assertNull($translator->getLocale());
90+
}
91+
7892
public function testGetCatalogue()
7993
{
8094
$translator = new Translator('en');
@@ -158,6 +172,17 @@ public function testSetFallbackValidLocales($locale)
158172
$this->addToAssertionCount(1);
159173
}
160174

175+
/**
176+
* @group legacy
177+
*/
178+
public function testSetNullFallbackLocale()
179+
{
180+
$translator = new Translator('en');
181+
$translator->setFallbackLocales(['fr', null]);
182+
// no assertion. this method just asserts that no exception is thrown
183+
$this->addToAssertionCount(1);
184+
}
185+
161186
public function testTransWithFallbackLocale()
162187
{
163188
$translator = new Translator('fr_FR');
@@ -184,9 +209,6 @@ public function testAddResourceInvalidLocales($locale)
184209
*/
185210
public function testAddResourceValidLocales($locale)
186211
{
187-
if (null === $locale) {
188-
$this->markTestSkipped('null is not a valid locale');
189-
}
190212
$translator = new Translator('fr');
191213
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
192214
// no assertion. this method just asserts that no exception is thrown
@@ -382,9 +404,6 @@ public function testTransInvalidLocale($locale)
382404
*/
383405
public function testTransValidLocale($locale)
384406
{
385-
if (null === $locale) {
386-
$this->markTestSkipped('null is not a valid locale');
387-
}
388407
$translator = new Translator($locale);
389408
$translator->addLoader('array', new ArrayLoader());
390409
$translator->addResource('array', ['test' => 'OK'], $locale);
@@ -458,6 +477,20 @@ public function testTransChoiceValidLocale($locale)
458477
$this->addToAssertionCount(1);
459478
}
460479

480+
/**
481+
* @group legacy
482+
*/
483+
public function testTransChoiceNullLocale()
484+
{
485+
$translator = new Translator('en');
486+
$translator->addLoader('array', new ArrayLoader());
487+
$translator->addResource('array', ['foo' => 'foofoo'], 'en');
488+
489+
$translator->transChoice('foo', 1, [], '', null);
490+
// no assertion. this method just asserts that no exception is thrown
491+
$this->addToAssertionCount(1);
492+
}
493+
461494
public function getTransFileTests()
462495
{
463496
return [
@@ -552,7 +585,6 @@ public function getValidLocalesTests()
552585
{
553586
return [
554587
[''],
555-
[null],
556588
['fr'],
557589
['francais'],
558590
['FR'],

src/Symfony/Component/Translation/Translator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
8888
*/
8989
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false)
9090
{
91-
$this->setLocale($locale);
91+
if (null === $locale) {
92+
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED);
93+
}
94+
95+
$this->setLocale($locale, false);
9296

9397
if (null === $formatter) {
9498
$formatter = new MessageFormatter();
@@ -151,6 +155,10 @@ public function addResource($format, $resource, $locale, $domain = null)
151155
*/
152156
public function setLocale($locale)
153157
{
158+
if (null === $locale && (2 > \func_num_args() || func_get_arg(1))) {
159+
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED);
160+
}
161+
154162
$this->assertValidLocale($locale);
155163
$this->locale = $locale;
156164
}
@@ -176,6 +184,9 @@ public function setFallbackLocales(array $locales)
176184
$this->catalogues = [];
177185

178186
foreach ($locales as $locale) {
187+
if (null === $locale) {
188+
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED);
189+
}
179190
$this->assertValidLocale($locale);
180191
}
181192

0 commit comments

Comments
 (0)
0