Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e477bf2

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

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-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 "$locale" in %s::%s has been deprecated since Symfony 4.4 and will throw in 5.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
37+
}
38+
3539
$this->locale = $locale;
3640
$this->messages = $messages;
3741
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ public function testAddResourceValidLocales($locale)
190190
$this->addToAssertionCount(1);
191191
}
192192

193+
/**
194+
* @group legacy
195+
* @expectedDeprecation Passing "null" to "$locale" in Symfony\Component\Translation\Translator::Symfony\Component\Translation\Translator::addResource has been deprecated since Symfony 4.4 and will throw in 5.0.
196+
*/
197+
public function testAddResourceNull()
198+
{
199+
$translator = new Translator('fr');
200+
$translator->addResource('array', ['foo' => 'foofoo'], null);
201+
// no assertion. this method just asserts that no exception is thrown
202+
$this->addToAssertionCount(1);
203+
}
193204
public function testAddResourceAfterTrans()
194205
{
195206
$translator = new Translator('fr');
@@ -527,7 +538,6 @@ public function getValidLocalesTests()
527538
{
528539
return [
529540
[''],
530-
[null],
531541
['fr'],
532542
['francais'],
533543
['FR'],

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 "$locale" in %s::%s has been deprecated since Symfony 4.4 and will throw in 5.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
137+
}
138+
135139
$this->assertValidLocale($locale);
136140

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

0 commit comments

Comments
 (0)