Skip to content

Commit

Permalink
feature #32415 [Translation] deprecate passing a null locale (Simperfit)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Translation] deprecate passing a null locale

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | none <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | not needed <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

According to the discussion in #32386 (comment) it seems that allowing null here was not the right thing to do, so we are deprecating this behaviour.

Commits
-------

088615d [Translation] deprecate passing a null locale
  • Loading branch information
fabpot committed Jul 8, 2019
2 parents 51cf65e + 088615d commit a640c30
Show file tree
Hide file tree
Showing 4 changed files with 47 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 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);
}

$this->locale = $locale;
$this->messages = $messages;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
Expand Up @@ -23,6 +23,17 @@ public function testGetLocale()
$this->assertEquals('en', $catalogue->getLocale());
}

/**
* @group legacy
* @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.
*/
public function testGetNullLocale()
{
$catalogue = new MessageCatalogue(null);

$this->assertNull($catalogue->getLocale());
}

public function testGetDomains()
{
$catalogue = new MessageCatalogue('en', ['domain1' => [], 'domain2' => [], 'domain2+intl-icu' => [], 'domain3+intl-icu' => []]);
Expand Down
29 changes: 28 additions & 1 deletion src/Symfony/Component/Translation/Tests/TranslatorTest.php
Expand Up @@ -184,12 +184,25 @@ public function testAddResourceInvalidLocales($locale)
*/
public function testAddResourceValidLocales($locale)
{
if (null === $locale) {
$this->markTestSkipped('null is not a valid locale');
}
$translator = new Translator('fr');
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
// no assertion. this method just asserts that no exception is thrown
$this->addToAssertionCount(1);
}

/**
* @group legacy
* @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.
*/
public function testAddResourceNull()
{
$translator = new Translator('fr');
$translator->addResource('array', ['foo' => 'foofoo'], null);
}

public function testAddResourceAfterTrans()
{
$translator = new Translator('fr');
Expand Down Expand Up @@ -367,10 +380,13 @@ public function testTransInvalidLocale($locale)
}

/**
* @dataProvider getValidLocalesTests
* @dataProvider getValidLocalesTests
*/
public function testTransValidLocale($locale)
{
if (null === $locale) {
$this->markTestSkipped('null is not a valid locale');
}
$translator = new Translator($locale);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['test' => 'OK'], $locale);
Expand All @@ -379,6 +395,17 @@ public function testTransValidLocale($locale)
$this->assertEquals('OK', $translator->trans('test', [], null, $locale));
}

/**
* @group legacy
* @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.
*/
public function testTransNullLocale()
{
$translator = new Translator(null);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['test' => 'OK'], null);
}

/**
* @dataProvider getFlattenedTransTests
*/
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 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);
}

$this->assertValidLocale($locale);

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

0 comments on commit a640c30

Please sign in to comment.