Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HttpFoundation] Similar locale selection#52986

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@Spomky
Copy link
Contributor

@SpomkySpomky commentedDec 10, 2023
edited by fabpot
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issuesnone
LicenseMIT

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:

#[Route('/', name:'app_root', methods: [Request::METHOD_GET])]publicfunctionindexNoLocale(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 headeraccept-language: ja-JP,fr_CA;q=0.7,fr;q=0.5, the default localeen_US is returned.
In this situation, I would expect to usefr_FR as the browser indicates French is a possible choice (fr_CA andfr).

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.

OskarStark reacted with rocket emoji
Copy link
Member

@NyholmNyholm left a comment

Choose a reason for hiding this comment

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

Thank you. It looks cool.

Can you also test these:

ja-JP,fr;q=0.5ja-JP,fr_CA;q=0.7

Both of them should go tofr_FR, right?


How about these?

ja-JP,fr_CA;q=0.7,fr;q=0.5,en_US;q=0.3ja-JP,fr;q=0.5,en_US;q=0.3ja-JP,fr_CA;q=0.7,en_US;q=0.3ja-JP,fr_CA;q=0.7,en;q=0.5

Spomky reacted with thumbs up emoji
@welcoMattic
Copy link
Member

Maybe for another PR, but shouldn't we add support forq= parameter?https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#directives

Spomky and jdreesen reacted with thumbs up emoji

@Spomky
Copy link
ContributorAuthor

Many thanks for your comments. Indeed there is something wrong with some casessuch as this one whereen is selected whenen-us is preferred just because of the order of the array values.
I will add use cases and make sureq is understood.

@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch from48c8c98 tob1525a3CompareDecember 11, 2023 19:19
@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch 4 times, most recently fromf8b2126 to4ed69e0CompareDecember 11, 2023 19:57
@Spomky
Copy link
ContributorAuthor

Hi,

I updated the PR and changed the comparison logic:

  • If an exact match exists, it is returned
  • Else if a similar locale exists, the corresponding language is returned
  • If nothing is acceptable, the first supported locale is returned

Please note that theq parameter is already used. Thetest with a reversed order priority returns the expected language.

In addition, the returned locale value is alwaysxx_XX to make it consistant with the value used internally.
Also, it accepts locales such ashi_Latn_IN to be compared.

@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch from10b0a45 to85e3a7fCompareFebruary 1, 2024 19:45
@Spomky
Copy link
ContributorAuthor

Many thanks for the last suggestions.
The PR is now rebased, squashed and ready for other reviewers comments

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

That's an improvement, for 7.1 ;)

@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch 2 times, most recently from16a03e4 to1d5e821CompareApril 3, 2024 18:34
@SpomkySpomky changed the base branch from6.4 to7.1April 3, 2024 18:34
@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch from1d5e821 tof67a5d7CompareApril 3, 2024 18:39
@stof
Copy link
Member

stof commentedApr 3, 2024

PHP already has a locale matching algorithm in ext-intl:https://www.php.net/manual/fr/locale.lookup.php (or maybe some of the other methods there)

Would this algorithm help for this PR ? If yes, the proper way to solve it might be to implement them in the polyfill inhttps://github.com/symfony/polyfill/blob/1.x/src/Intl/Icu/Locale.php (where it currently throw an exception saying it is not implemented) and to use it.

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM thanks

PHP already has a locale matching algorithm in ext-intl [...] the proper way to solve it might be to implement them in the polyfill

Good idea! I suggest merging this PR, and switch to the polyfill once we have it. Nothing is public here so the switch should be easy.

@SpomkySpomkyforce-pushed thebugs/incorrect-locale-selection branch from8a259ab tofbe44f0CompareApril 4, 2024 06:10
@fabpotfabpot modified the milestones:6.4,7.1Apr 5, 2024
@fabpotfabpotforce-pushed thebugs/incorrect-locale-selection branch fromfbe44f0 toef73505CompareApril 5, 2024 06:35
@fabpot
Copy link
Member

Thank you@Spomky.

Spomky reacted with rocket emoji

@fabpotfabpot merged commitd1f3ee8 intosymfony:7.1Apr 5, 2024
@SpomkySpomky deleted the bugs/incorrect-locale-selection branchApril 7, 2024 16:37
@fabpotfabpot mentioned this pull requestMay 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@welcoMatticwelcoMatticwelcoMattic left review comments

@OskarStarkOskarStarkOskarStark left review comments

@NyholmNyholmNyholm left review comments

@ycerutoycerutoyceruto left review comments

@fabpotfabpotfabpot approved these changes

@dunglasdunglasdunglas approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

+2 more reviewers

@stloydstloydstloyd left review comments

@smnandresmnandresmnandre left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

12 participants

@Spomky@welcoMattic@smnandre@stof@fabpot@dunglas@stloyd@nicolas-grekas@OskarStark@Nyholm@yceruto@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp