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
-------
20120a3df4
[Translator] fix fallback to Locale::getDefault()
This commit is contained in:
commit
45015b968d
|
@ -50,7 +50,7 @@ class TranslatorTest extends TestCase
|
||||||
{
|
{
|
||||||
$translator = new Translator($locale);
|
$translator = new Translator($locale);
|
||||||
|
|
||||||
$this->assertSame($locale, $translator->getLocale());
|
$this->assertSame($locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en'), $translator->getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,7 +91,7 @@ class TranslatorTest extends TestCase
|
||||||
$translator = new Translator($locale);
|
$translator = new Translator($locale);
|
||||||
$translator->setLocale($locale);
|
$translator->setLocale($locale);
|
||||||
|
|
||||||
$this->assertEquals($locale, $translator->getLocale());
|
$this->assertEquals($locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en'), $translator->getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -143,6 +143,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertValidLocale($locale);
|
$this->assertValidLocale($locale);
|
||||||
|
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
|
||||||
|
|
||||||
$this->resources[$locale][] = [$format, $resource, $domain];
|
$this->resources[$locale][] = [$format, $resource, $domain];
|
||||||
|
|
||||||
|
@ -163,7 +164,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertValidLocale($locale);
|
$this->assertValidLocale($locale);
|
||||||
$this->locale = $locale ?? (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
|
$this->locale = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -171,7 +172,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||||
*/
|
*/
|
||||||
public function getLocale()
|
public function getLocale()
|
||||||
{
|
{
|
||||||
return $this->locale;
|
return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -281,7 +282,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||||
*/
|
*/
|
||||||
public function getCatalogue($locale = null)
|
public function getCatalogue($locale = null)
|
||||||
{
|
{
|
||||||
if (null === $locale) {
|
if (!$locale) {
|
||||||
$locale = $this->getLocale();
|
$locale = $this->getLocale();
|
||||||
} else {
|
} else {
|
||||||
$this->assertValidLocale($locale);
|
$this->assertValidLocale($locale);
|
||||||
|
@ -505,7 +506,7 @@ EOF
|
||||||
*/
|
*/
|
||||||
protected function assertValidLocale($locale)
|
protected function assertValidLocale($locale)
|
||||||
{
|
{
|
||||||
if (null !== $locale && 1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', (string) $locale)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue