bug #38127 [Translator] Make sure a null locale is handled properly (jschaedl)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Translator] Make sure a null locale is handled properly

| Q             | A
| ------------- | ---
| Branch?       | 4.4<!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #38124  <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | - <!-- 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/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - 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 master.
-->

Commits
-------

080ea5a0f7 [Translator] Make sure a null locale is handled properly
This commit is contained in:
Fabien Potencier 2020-09-18 10:46:54 +02:00
commit 785a06653c
2 changed files with 16 additions and 3 deletions

View File

@ -19,6 +19,19 @@ use Symfony\Component\Translation\Translator;
class TranslatorTest extends TestCase
{
private $defaultLocale;
protected function setUp(): void
{
$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}
protected function tearDown(): void
{
\Locale::setDefault($this->defaultLocale);
}
/**
* @dataProvider getInvalidLocalesTests
*/
@ -45,7 +58,7 @@ class TranslatorTest extends TestCase
{
$translator = new Translator(null);
$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
}
public function testSetGetLocale()
@ -87,7 +100,7 @@ class TranslatorTest extends TestCase
$translator = new Translator('en');
$translator->setLocale(null);
$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
}
public function testGetCatalogue()

View File

@ -171,7 +171,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
*/
public function getLocale()
{
return $this->locale;
return $this->locale ?? \Locale::getDefault();
}
/**