8000 bug #42130 [Translation] fix fallback to Locale::getDefault() (nicola… · symfony/symfony@45015b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45015b9

Browse files
committed
bug #42130 [Translation] fix fallback to Locale::getDefault() (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Translation] fix fallback to Locale::getDefault() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Replaces #42128 and #38230 Provides consistent behavior with `TranslatorTrait::getLocale()`. Commits ------- 20120a3 [Translator] fix fallback to Locale::getDefault()
2 parents dd43b32 + 20120a3 commit 45015b9

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

Lines 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

Lines 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