Skip to content

Commit 088615d

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

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

Lines changed: 4 additions & 0 deletions
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 the first argument of the "%s" method 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

Lines changed: 11 additions & 0 deletions
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 the first argument of the "Symfony\Component\Translation\MessageCatalogue::__construct" method 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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,25 @@ public function testAddResourceInvalidLocales($locale)
184184
*/
185185
public function testAddResourceValidLocales($locale)
186186
{
187+
if (null === $locale) {
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 the third argument of the "Symfony\Component\Translation\Translator::addResource" method 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+
}
205+
193206
public function testAddResourceAfterTrans()
194207
{
195208
$translator = new Translator('fr');
@@ -367,10 +380,13 @@ public function testTransInvalidLocale($locale)
367380
}
368381

369382
/**
370-
* @dataProvider getValidLocalesTests
383+
* @dataProvider getValidLocalesTests
371384
*/
372385
public function testTransValidLocale($locale)
373386
{
387+
if (null === $locale) {
388+
$this->markTestSkipped('null is not a valid locale');
389+
}
374390
$translator = new Translator($locale);
375391
$translator->addLoader('array', new ArrayLoader());
376392
$translator->addResource('array', ['test' => 'OK'], $locale);
@@ -379,6 +395,17 @@ public function testTransValidLocale($locale)
379395
$this->assertEquals('OK', $translator->trans('test', [], null, $locale));
380396
}
381397

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

src/Symfony/Component/Translation/Translator.php

Lines changed: 4 additions & 0 deletions
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 the third argument of the "%s" method 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)
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