You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #52986 [HttpFoundation] Similar locale selection (Spomky)
This PR was merged into the 7.1 branch.
Discussion
----------
[HttpFoundation] Similar locale selection
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues | none
| License | MIT
Allow the nearest locale to be selected instead the default one.
Ping `@welcoMattic`
I noted a non-optimized locale selection. Let say I have the following controller and root route:
```php
#[Route('/', name: 'app_root', methods: [Request::METHOD_GET])]
public function indexNoLocale(Request $request): Response
{
$locale = $request->getPreferredLanguage($this->supportedLocales) ?? $this->defaultLocale;
return $this->redirectToRoute('app_homepage', [
'_locale' => $locale,
], Response::HTTP_SEE_OTHER);
}
```
And the following parameters:
* `$this->supportedLocales` = `['fr_FR', 'en_US']`
* `$this->defaultLocale` = `'en_US'`
When a user arrives on this page with the header `accept-language: ja-JP,fr_CA;q=0.7,fr;q=0.5`, the default locale `en_US` is returned.
In this situation, I would expect to use `fr_FR` as the browser indicates French is a possible choice (`fr_CA` and `fr`).
With this change, and if no exact match is found, a similar language is selected. In this example, `fr_FR` is acceptable and then returned.
Commits
-------
ef73505 Allow the nearest locale to be selected instead the default one.
yield'"es_PA" is selected as no supported locale is set' => ['es_PA', 'es-pa, en-us; q=0.8, en; q=0.6', []];
1522
+
yield'No supported locales' => [null, null, []];
1523
+
yield'"fr" selected as first choice when no header is present' => ['fr', null, ['fr', 'en']];
1524
+
yield'"en" selected as first choice when no header is present' => ['en', null, ['en', 'fr']];
1525
+
yield'"fr_CH" selected as first choice when no header is present' => ['fr_CH', null, ['fr-ch', 'fr-fr']];
1526
+
yield'"en_US" is selected as an exact match is found (1)' => ['en_US', 'zh, en-us; q=0.8, en; q=0.6', ['en', 'en-us']];
1527
+
yield'"en_US" is selected as an exact match is found (2)' => ['en_US', 'ja-JP,fr_CA;q=0.7,fr;q=0.5,en_US;q=0.3', ['en_US', 'fr_FR']];
1528
+
yield'"en" is selected as an exact match is found' => ['en', 'zh, en-us; q=0.8, en; q=0.6', ['fr', 'en']];
1529
+
yield'"fr" is selected as an exact match is found' => ['fr', 'zh, en-us; q=0.8, fr-fr; q=0.6, fr; q=0.5', ['fr', 'en']];
1530
+
yield'"en" is selected as "en-us" is a similar dialect' => ['en', 'zh, en-us; q=0.8', ['fr', 'en']];
1531
+
yield'"fr_FR" is selected as "fr_CA" is a similar dialect (1)' => ['fr_FR', 'ja-JP,fr_CA;q=0.7,fr;q=0.5', ['en_US', 'fr_FR']];
1532
+
yield'"fr_FR" is selected as "fr_CA" is a similar dialect (2)' => ['fr_FR', 'ja-JP,fr_CA;q=0.7', ['en_US', 'fr_FR']];
1533
+
yield'"fr_FR" is selected as "fr" is a similar dialect' => ['fr_FR', 'ja-JP,fr;q=0.5', ['en_US', 'fr_FR']];
1534
+
yield'"fr_FR" is selected as "fr_CA" is a similar dialect and has a greater "q" compared to "en_US" (2)' => ['fr_FR', 'ja-JP,fr_CA;q=0.7,ru-ru;q=0.3', ['en_US', 'fr_FR']];
1535
+
yield'"en_US" is selected it is an exact match' => ['en_US', 'ja-JP,fr;q=0.5,en_US;q=0.3', ['en_US', 'fr_FR']];
1536
+
yield'"fr_FR" is selected as "fr_CA" is a similar dialect and has a greater "q" compared to "en"' => ['fr_FR', 'ja-JP,fr_CA;q=0.7,en;q=0.5', ['en_US', 'fr_FR']];
1537
+
yield'"fr_FR" is selected as is is an exact match as well as "en_US", but with a greater "q" parameter' => ['fr_FR', 'en-us;q=0.5,fr-fr', ['en_US', 'fr_FR']];
1538
+
yield'"hi_IN" is selected as "hi_Latn_IN" is a similar dialect' => ['hi_IN', 'fr-fr,hi_Latn_IN;q=0.5', ['hi_IN', 'en_US']];
1539
+
yield'"hi_Latn_IN" is selected as "hi_IN" is a similar dialect' => ['hi_Latn_IN', 'fr-fr,hi_IN;q=0.5', ['hi_Latn_IN', 'en_US']];
1540
+
yield'"en_US" is selected as "en_Latn_US+variants+extensions" is a similar dialect' => ['en_US', 'en-latn-us-fonapi-u-nu-numerical-x-private,fr;q=0.5', ['fr_FR', 'en_US']];
1541
+
yield'"zh_Hans" is selected over "zh_TW" as the script as a greater priority over the region' => ['zh_Hans', 'zh-hans-tw, zh-hant-tw', ['zh_Hans', 'zh_tw']];
1526
1542
}
1527
1543
1528
1544
publicfunctiontestIsXmlHttpRequest()
@@ -1601,30 +1617,28 @@ public function testGetAcceptableContentTypes()
0 commit comments