Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 20120a3

Browse filesBrowse files
[Translator] fix fallback to Locale::getDefault()
1 parent b8cbb93 commit 20120a3
Copy full SHA for 20120a3

File tree

2 files changed

+7
-6
lines changed
Filter options

2 files changed

+7
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testConstructorValidLocale($locale)
5050
{
5151
$translator = new Translator($locale);
5252

53-
$this->assertSame($locale, $translator->getLocale());
53+
$this->assertSame($locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en'), $translator->getLocale());
5454
}
5555

5656
/**
@@ -91,7 +91,7 @@ public function testSetValidLocale($locale)
9191
$translator = new Translator($locale);
9292
$translator->setLocale($locale);
9393

94-
$this->assertEquals($locale, $translator->getLocale());
94+
$this->assertEquals($locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en'), $translator->getLocale());
9595
}
9696

9797
/**

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function addResource($format, $resource, $locale, $domain = null)
143143
}
144144

145145
$this->assertValidLocale($locale);
146+
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
146147

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

@@ -163,15 +164,15 @@ public function setLocale($locale)
163164
}
164165

165166
$this->assertValidLocale($locale);
166-
$this->locale = $locale ?? (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
167+
$this->locale = $locale;
167168
}
168169

169170
/**
170171
* {@inheritdoc}
171172
*/
172173
public function getLocale()
173174
{
174-
return $this->locale;
175+
return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
175176
}
176177

177178
/**
@@ -281,7 +282,7 @@ public function transChoice($id, $number, array $parameters = [], $domain = null
281282
*/
282283
public function getCatalogue($locale = null)
283284
{
284-
if (null === $locale) {
285+
if (!$locale) {
285286
$locale = $this->getLocale();
286287
} else {
287288
$this->assertValidLocale($locale);
@@ -505,7 +506,7 @@ protected function computeFallbackLocales($locale)
505506
*/
506507
protected function assertValidLocale($locale)
507508
{
508-
if (null !== $locale && 1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
509+
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', (string) $locale)) {
509510
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
510511
}
511512
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.