Skip to content

Commit c1273ab

Browse files
committed
deprecate support for null locales
1 parent 5a753b1 commit c1273ab

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
@@ -189,6 +189,11 @@ Stopwatch
189189

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

192+
Translation
193+
-----------
194+
195+
* Deprecated support for using `null` as the locale in `Translator`.
196+
192197
TwigBridge
193198
----------
194199

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy