Skip to content

Commit bca34cf

Browse files
committed
[Translation] deprecate passing a null locale
1 parent 6811aaa commit bca34cf

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
3232
*/
3333
public function __construct(?string $locale, array $messages = [])
3434
{
35+
if (null === $locale) {
36+
@trigger_error(sprintf('Passing "null" to "$locale" in %s has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED);
37+
}
38+
3539
$this->locale = $locale;
3640
$this->messages = $messages;
3741
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ public function testGetLocale()
2323
$this->assertEquals('en', $catalogue->getLocale());
2424
}
2525

26+
/**
27+
* @group legacy
28+
* @expectedDeprecation Passing "null" to "$locale" in Symfony\Component\Translation\MessageCatalogue::__construct has been deprecated since Symfony 4.4 and will throw an error in 5.0.
29+
*/
30+
public function testGetNullLocale()
31+
{
32+
$catalogue = new MessageCatalogue(null);
33+
34+
$this->assertNull($catalogue->getLocale());
35+
}
36+
2637
public function testGetDomains()
2738
{
2839
$catalogue = new MessageCatalogue('en', ['domain1' => [], 'domain2' => [], 'domain2+intl-icu' => [], 'domain3+intl-icu' => []]);

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,26 @@ public function testAddResourceInvalidLocales($locale)
184184
*/
185185
public function testAddResourceValidLocales($locale)
186186
{
187+
if ($locale === null) {
188+
$this->markTestSkipped('null is not a valid locale');
189+
}
187190
$translator = new Translator('fr');
188191
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
189192
// no assertion. this method just asserts that no exception is thrown
190193
$this->addToAssertionCount(1);
191194
}
192195

196+
/**
197+
* @group legacy
198+
* @expectedDeprecation Passing "null" to "$locale" in Symfony\Component\Translation\Translator::addResource has been deprecated since Symfony 4.4 and will throw an error in 5.0.
199+
*/
200+
public function testAddResourceNull()
201+
{
202+
$translator = new Translator('fr');
203+
$translator->addResource('array', ['foo' => 'foofoo'], null);
204+
// no assertion. this method just asserts that no exception is thrown
205+
}
206+
193207
public function testAddResourceAfterTrans()
194208
{
195209
$translator = new Translator('fr');
@@ -367,10 +381,13 @@ public function testTransInvalidLocale($locale)
367381
}
368382

369383
/**
370-
* @dataProvider getValidLocalesTests
384+
* @dataProvider getValidLocalesTests
371385
*/
372386
public function testTransValidLocale($locale)
373387
{
388+
if ($locale === null) {
389+
$this->markTestSkipped('null is not a valid locale');
390+
}
374391
$translator = new Translator($locale);
375392
$translator->addLoader('array', new ArrayLoader());
376393
$translator->addResource('array', ['test' => 'OK'], $locale);
@@ -379,6 +396,17 @@ public function testTransValidLocale($locale)
379396
$this->assertEquals('OK', $translator->trans('test', [], null, $locale));
380397
}
381398

399+
/**
400+
* @group legacy
401+
* @expectedDeprecation Passing "null" to "$locale" in Symfony\Component\Translation\Translator::addResource has been deprecated since Symfony 4.4 and will throw an error in 5.0.
402+
*/
403+
public function testTransNullLocale()
404+
{
405+
$translator = new Translator(null);
406+
$translator->addLoader('array', new ArrayLoader());
407+
$translator->addResource('array', ['test' => 'OK'], null);
408+
}
409+
382410
/**
383411
* @dataProvider getFlattenedTransTests
384412
*/

src/Symfony/Component/Translation/Translator.php

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function addResource($format, $resource, $locale, $domain = null)
132132
$domain = 'messages';
133133
}
134134

135+
if (null === $locale) {
136+
@trigger_error(sprintf('Passing "null" to "$locale" in %s has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED);
137+
}
138+
135139
$this->assertValidLocale($locale);
136140

137141
$this->resources[$locale][] = [$format, $resource, $domain];

0 commit comments

Comments
 (0)