-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Extract locale fallback computation into a dedicated class #45553
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
aa6a433
2afae5e
54e6a4a
042757a
2455bac
532a72c
47fe72a
0c19e4f
c2ac4a2
ddb9462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -102,6 +102,8 @@ public function warmUp(string $cacheDir): array | |||||
*/ | ||||||
public function getFallbackLocales(): array | ||||||
{ | ||||||
trigger_deprecation('symfony/translation', '6.2', 'DataCollectorTranslator::getFallbackLocales() is deprecated. Get the FallbackLocaleProvider instead.'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 6.2 must be replaced with 6.4 here and in all other |
||||||
|
||||||
if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { | ||||||
return $this->translator->getFallbackLocales(); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
use Symfony\Component\Translation\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Derives fallback locales based on ICU parent locale information, by shortening locale | ||
* sub tags and ultimately by going through a list of configured fallback locales. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
class FallbackLocaleProvider implements FallbackLocaleProviderInterface | ||
{ | ||
/** | ||
* @var string[] | ||
* | ||
* List of fallback locales to add _after_ the ones derived from ICU information. | ||
*/ | ||
private array $ultimateFallbackLocales; | ||
|
||
private ?array $parentLocales = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be, I've no strong opinion on it. @nicolas-grekas WDYT? |
||
|
||
/** | ||
* @param string[] $ultimateFallbackLocales | ||
* | ||
* @throws InvalidArgumentException If a locale contains invalid characters | ||
*/ | ||
public function __construct(array $ultimateFallbackLocales = []) | ||
{ | ||
foreach ($ultimateFallbackLocales as $locale) { | ||
LocaleValidator::validate($locale); | ||
} | ||
|
||
$this->ultimateFallbackLocales = $ultimateFallbackLocales; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getUltimateFallbackLocales(): array | ||
{ | ||
return $this->ultimateFallbackLocales; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function computeFallbackLocales(string $locale): array | ||
{ | ||
LocaleValidator::validate($locale); | ||
|
||
$this->parentLocales ??= json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true); | ||
|
||
$originLocale = $locale; | ||
$locales = []; | ||
|
||
while ($locale) { | ||
$parent = $this->parentLocales[$locale] ?? null; | ||
|
||
if ($parent) { | ||
$locale = 'root' !== $parent ? $parent : null; | ||
} elseif (\function_exists('locale_parse')) { | ||
$localeSubTags = locale_parse($locale); | ||
$locale = null; | ||
if (1 < \count($localeSubTags)) { | ||
array_pop($localeSubTags); | ||
$locale = locale_compose($localeSubTags) ?: null; | ||
} | ||
} elseif ($i = strrpos($locale, '_') ?: strrpos($locale, '-')) { | ||
$locale = substr($locale, 0, $i); | ||
} else { | ||
$locale = null; | ||
} | ||
|
||
if (null !== $locale) { | ||
$locales[] = $locale; | ||
} | ||
} | ||
|
||
foreach ($this->ultimateFallbackLocales as $fallback) { | ||
if ($fallback === $originLocale) { | ||
continue; | ||
} | ||
|
||
$locales[] = $fallback; | ||
} | ||
|
||
return array_unique($locales); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
use Symfony\Component\Translation\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* For a given locale, implementations provide the list of alternative locales to | ||
* try when a translation cannot be found. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
interface FallbackLocaleProviderInterface | ||
{ | ||
/** | ||
* @internal | ||
* | ||
* @return string[] | ||
*/ | ||
public function getUltimateFallbackLocales(): array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have callees from external? If not, feel free to indeed remove from the interface. If we have calls from outside the class, do we really need those calls because calling internal classes might be considered a violation. |
||
|
||
/** | ||
* For a given locale, this method provides the ordered list of alternative (fallback) locales | ||
* to try. | ||
* | ||
* @return string[] | ||
*/ | ||
public function computeFallbackLocales(string $locale): array; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
use Symfony\Component\Translation\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Asserts (syntactical) validity for given locale identifiers. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
class LocaleValidator | ||
{ | ||
/** | ||
* Asserts that the locale is valid, throws an Exception if not. | ||
* | ||
* @throws InvalidArgumentException If the locale contains invalid characters | ||
*/ | ||
public static function validate(string $locale): void | ||
{ | ||
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) { | ||
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch must be rebased on 6.4 and this line moves to 6.4 section