-
-
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?
Conversation
bb06f51
to
3fabce5
Compare
Hey! I think @rvanlaak has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
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.
I see value in the usecase where you want to be able to take more control over the fallback locales. Besides that it also abstracts out a responsibility from the big Translator
class, so 👍 for continuing with the remaining TODOs from the PR description.
7ff72be
to
1913770
Compare
…es in FallbackLocaleProvider as well
826daa0
to
532a72c
Compare
@@ -59,6 +61,7 @@ | |||
'debug' => param('kernel.debug'), | |||
], | |||
abstract_arg('enabled locales'), | |||
service('translation.fallback_locale_provider') |
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.
Is this correct, or should I use FallbackLocaleProviderInterface::class
here?
48bd240
to
9c39534
Compare
9c39534
to
47fe72a
Compare
There is a possible mistake people might run into when they start using the The One solution I see would be:
Would that be acceptable? |
Additionally, we might get rid of |
I think I need some opinions and/or feedback on this before I can proceed. 👀 🙏🏻 |
As
I guess it's acceptable, but we need to provide a BC layer, to let |
Co-authored-by: Mathieu Santostefano <msantostefano@protonmail.com>
…the fallback locale configuration changes
@welcoMattic and @rvanlaak Thanks for your initial reviews! I haven't dealt with deprecations and replacement code in Symfony for a long time. Do you think you could help me to get this straight? The |
I also renamed |
*/ | ||
private array $ultimateFallbackLocales; | ||
|
||
private ?array $parentLocales = null; |
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.
Should this be static
? It is used to cache parsed JSON data, so we might want some extra efficiency.
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.
It could be, I've no strong opinion on it. @nicolas-grekas WDYT?
* | ||
* @return string[] | ||
*/ | ||
public function getUltimateFallbackLocales(): array; |
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.
Given that it is @internal
, should it even be part of the interface?
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.
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.
|
||
// needed as the fallback locales are linked to the already loaded catalogues | ||
$this->catalogues = []; | ||
$this->cacheVary['fallback_locales'] = $fallbackLocaleProvider->getUltimateFallbackLocales(); |
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.
I don't like this for the following reasons:
When, in general, a FallbackLocaleProvider
is responsible for giving the sequence of locales to try for a given starting locale, we should not assume it has this list of "last resort" locales, and we should also not assume that this configurable setting is the only thing affecting the computation results.
It might not be relevant in practice, but what if the JSON file with the locale hierarchy changes?
If the FallbackLocaleProvider
just provides something like a cache key/suffix or similar (not exposing the ultimateFallbackLocales
list), what would that mean for the "fallback locale" information gathered in the DataCollectorTranslator
?
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.
I'm not sure to understand your concerns here. What you did seems really similar to what was done before FallbackLocaleProvider
introduction.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
trigger_deprecation('symfony/translation', '6.2', 'DataCollectorTranslator::getFallbackLocales() is deprecated. Get the FallbackLocaleProvider instead.'); | |
trigger_deprecation('symfony/translation', '6.2', '"%s::getFallbackLocales()" is deprecated, use "%s::getUltimateFallbackLocales()" instead.', DataCollectorTranslator::class, FallbackLocaleProvider::class); |
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.
6.2 must be replaced with 6.4 here and in all other trigger_deprecation
occurrences.
What's the status here? |
I am still interested in working on it and the use case still exists. Will have to think this through again, forgot about most of the parts. I hope I have all the feedback and information regarding the deprecation steps that I need. |
@@ -6,6 +6,7 @@ CHANGELOG | |||
|
|||
* Parameters implementing `TranslatableInterface` are processed | |||
* Add the file extension to the `XliffFileDumper` constructor | |||
* Add `FallbackLocaleProvider` to configure the fallback locale chain in `Translator` |
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
@@ -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 comment
The 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 trigger_deprecation
occurrences.
*/ | ||
private array $ultimateFallbackLocales; | ||
|
||
private ?array $parentLocales = null; |
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.
It could be, I've no strong opinion on it. @nicolas-grekas WDYT?
|
||
// needed as the fallback locales are linked to the already loaded catalogues | ||
$this->catalogues = []; | ||
$this->cacheVary['fallback_locales'] = $fallbackLocaleProvider->getUltimateFallbackLocales(); |
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.
I'm not sure to understand your concerns here. What you did seems really similar to what was done before FallbackLocaleProvider
introduction.
This branch needs to be rebased on 6.4. Otherwise, I guess we are close to an acceptable feature. I think here we must stick to the existing feature but moved to its own class |
I am working on an application that in addition to Symfony-based translations needs to work with its own translation system. It would be 💯 if both parts could easily use the same locale hierarchy and fallback rules.
Since fallback resolution is in fact very involved, this PR aims to expose the logic in a dedicated new class, the
FallbackLocaleProvider
.In the future, when the fallback locales are to be changed, a new instance of
FallbackLocaleProviderInterface
needs to be set on theTranslator
.TODO
\Symfony\Component\Translation\Translator::assertValidLocale()
to a place where it can also be used by theFallbackLocaleProvider