Skip to content

Commit

Permalink
[Translation] deprecate passing a null locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Simperfit committed Jul 7, 2019
1 parent 6811aaa commit e477bf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Expand Up @@ -32,6 +32,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function __construct(?string $locale, array $messages = [])
{
if (null === $locale) {
@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);
}

$this->locale = $locale;
$this->messages = $messages;
}
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Translation/Tests/TranslatorTest.php
Expand Up @@ -190,6 +190,17 @@ public function testAddResourceValidLocales($locale)
$this->addToAssertionCount(1);
}

/**
* @group legacy
* @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.
*/
public function testAddResourceNull()
{
$translator = new Translator('fr');
$translator->addResource('array', ['foo' => 'foofoo'], null);
// no assertion. this method just asserts that no exception is thrown
$this->addToAssertionCount(1);
}
public function testAddResourceAfterTrans()
{
$translator = new Translator('fr');
Expand Down Expand Up @@ -527,7 +538,6 @@ public function getValidLocalesTests()
{
return [
[''],
[null],
['fr'],
['francais'],
['FR'],
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/Translator.php
Expand Up @@ -132,6 +132,10 @@ public function addResource($format, $resource, $locale, $domain = null)
$domain = 'messages';
}

if (null === $locale) {
@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);
}

$this->assertValidLocale($locale);

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

0 comments on commit e477bf2

Please sign in to comment.