Skip to content

[Translator] Make sure a null locale is handled properly #38127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@

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
*/
Expand All @@ -45,7 +58,7 @@ public function testConstructorWithoutLocale()
{
$translator = new Translator(null);

$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
}

public function testSetGetLocale()
Expand Down Expand Up @@ -87,7 +100,7 @@ public function testSetNullLocale()
$translator = new Translator('en');
$translator->setLocale(null);

$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
}

public function testGetCatalogue()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function setLocale($locale)
*/
public function getLocale()
{
return $this->locale;
return $this->locale ?? \Locale::getDefault();
}

/**
Expand Down