8000 [Translator] Use ICU parent locales as fallback locales by thewilkybarkid · Pull Request #28070 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translator] Use ICU parent locales as fallback locales #28070

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

Merged
merged 1 commit into from
Aug 28, 2018

Conversation

thewilkybarkid
Copy link
Contributor
@thewilkybarkid thewilkybarkid commented Jul 26, 2018
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #12319
License MIT
Doc PR symfony/symfony-docs#10122

Currently the Translator fall backs based on the locale separator (eg es_AR to es), but the ICU data contains parent locales (eg es_AR is a child of es_419, as is es_BO, es_EC etc).

This makes use of the ICU data to add add in these fallbacks. This means the specific locales can be used, but the translations can stored in these groupings (eg es_419 for Latin American Spanish), as well as adding other sensible fallbacks (eg Cape Verdean Portuguese to pt_PT).

@thewilkybarkid thewilkybarkid changed the title Use ICU parent locales as fallback locales [Translator] Use ICU parent locales as fallback locales Jul 26, 2018

// Aliases contain the text "%%PARENT" followed by the aliased locale
if (\preg_match('/%%Parent{"([^"]+)"}/', $content, $matches)) {
if ('root' === $matches[1]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just understood why some have root as a parent: az_Cyrl would fall back to az (Azerbaijani in Latin) otherwise (so has a script change). Will update to stop az being a fallback in this case.

@@ -392,6 +397,10 @@ private function loadFallbackCatalogues($locale): void

protected function computeFallbackLocales($locale)
{
if (null === $this->parentLocales) {
$parentLocales = \json_decode(\file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe 8000 this comment to others. Learn more.

we don't fully-qualify all function calls in Symfony (to reduce the visual clutter). We only fully-qualify the function which can be optimized by the compiler (as the compiler cannot optimize them when it does not know for sure that the global function is the one being called)

@@ -401,10 +410,20 @@ protected function computeFallbackLocales($locale)
$locales[] = $fallback;
}

$parentLocale = $parentLocales[$locale] ?? null;

if ('root' === $parentLocale) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a BC break, as it removes some of the existing fallbacks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a quick go and looks possible to keep track of deprecated fallbacks.

But this does raise a question of what should happen when the data changes, eg CLDR 27 (eg en_JE switched from falling back from en_GB to en_001 directly).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should follow CLDR

@nicolas-grekas
Copy link
Member

what's the lowest branch for this patch? bug fix should target the lowest maintained branch;

@thewilkybarkid
Copy link
Contributor Author

I'd describe sr_Latn etc as bugs (fallbacks changing scripts), but did originally consider this a feature.

@nicolas-grekas nicolas-grekas added this to the next milestone Aug 3, 2018
*
* @param string $sourceDir The directory with ICU files
*
* @return array An array with the locale as keys and the parent locales
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type annotations should be replaced by real PHP types

@thewilkybarkid
Copy link
Contributor Author

Looking at previous ICU updates, the data is merged into patch releases so future changes here will presumably do the same.

@fabpot
Copy link
Member
fabpot commented Aug 27, 2018

That's a new feature. Can you add a note in the CHANGELOG ?

@thewilkybarkid
Copy link
Contributor 8000 Author

Travis failure is unrelated.

@fabpot fabpot force-pushed the icu-parent-locales branch from 48f4a81 to e0f402f Compare August 28, 2018 06:30
@fabpot
Copy link
Member
fabpot commented Aug 28, 2018

Thank you @thewilkybarkid.

@fabpot fabpot merged commit e0f402f into symfony:master Aug 28, 2018
fabpot added a commit that referenced this pull request Aug 28, 2018
…s (thewilkybarkid)

This PR was squashed before being merged into the 4.2-dev branch (closes #28070).

Discussion
----------

[Translator] Use ICU parent locales as fallback locales

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12319
| License       | MIT
| Doc PR        | symfony/symfony-docs#10122

Currently the `Translator` fall backs based on the locale separator (eg `es_AR` to `es`), but the ICU data contains parent locales (eg `es_AR` is a child of `es_419`, as is `es_BO`, `es_EC` etc).

This makes use of the ICU data to add add in these fallbacks. This means the specific locales can be used, but the translations can stored in these groupings (eg `es_419` for Latin American Spanish), as well as adding other sensible fallbacks (eg Cape Verdean Portuguese to `pt_PT`).

Commits
-------

e0f402f [Translator] Use ICU parent locales as fallback locales
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 4, 2018
…s (thewilkybarkid, javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

Update translator fallback example to include ICU parents

Changes the example to show symfony/symfony#28070.

Commits
-------

7ccf603 Reworded and added the versionadded directive
54631df Update translator fallback example to include ICU parents
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
@thewilkybarkid thewilkybarkid deleted the icu-parent-locales branch April 7, 2019 14:02
nicolas-grekas added a commit that referenced this pull request Jun 30, 2020
…ator (jvasseur)

This PR was merged into the 4.4 branch.

Discussion
----------

[Translation] Fix caching of parent locale
8089
s file in translator

| Q             | A
| ------------- | ---
| Branch?       | 4.4 (this is the lowest maintained branch with this code)
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

The `parentLocales` property was probably meant as a cache for the content of the `parents.json` file but instead the content is stored in a local variable and the property stays `null`. This means the file is read on each call to `computeFallbackLocales`.

This PR update the code to what was probably meant to be.

(Ref #28070)

Commits
-------

02c9ac6 Fix caching of parent locales file in translator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0