Fix incorrect preferred locale selection with multiple languages configured - #70
Merged
Merged
Conversation
fluiddot
commented
Apr 30, 2024
Comment on lines
+177
to
+180
| // Set translations based on supported locale | ||
| const locale = getSupportedLocale(); | ||
| const localeData = getLocaleData( locale ); | ||
| defaultI18n.setLocaleData( localeData?.locale_data?.messages ); |
Contributor
Author
There was a problem hiding this comment.
The supported locale uses app.getLocale that is only available when the app is read.
Comment on lines
-28
to
-47
| it( "falls back to lesser preferred languages if the most preferred isn't supported", () => { | ||
| mockPreferredLanguages( [ 'mi-NZ', 'fr-FR', 'en-US' ] ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'fr' ); | ||
| } ); | ||
|
|
||
| it( 'ignores region if the best match is a matching language with a different region', () => { | ||
| mockPreferredLanguages( [ 'mi-NZ', 'pt-PT' ] ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'pt-br' ); | ||
| } ); | ||
|
|
||
| it( "prefers an exact language-region match, even if it's lower in the preference order", () => { | ||
| mockPreferredLanguages( [ 'mi-NZ', 'pt-PT', 'zh-CN' ] ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'zh-cn' ); | ||
| } ); |
Contributor
Author
There was a problem hiding this comment.
We no longer need to test preferred languages as we don't use it.
Comment on lines
-114
to
-116
| it( "returns 'en' as default language", async () => { | ||
| mockPreferredLanguages( [] ); | ||
| mockFetchTranslations( WP_VERSION, AVAILABLE_LOCALES ); | ||
|
|
||
| expect( await getPreferredSiteLanguage( WP_VERSION ) ).toBe( 'en' ); | ||
| } ); |
Contributor
Author
There was a problem hiding this comment.
This test case is no longer needed as app.getLocale always returns a value.
| } ); | ||
|
|
||
| mockPreferredLanguages( WP_5_0_LOCALES.map( ( item ) => item.locale ) ); | ||
| mockAppLocale( WP_5_0_LOCALES[ 0 ].locale ); |
Contributor
Author
There was a problem hiding this comment.
We pick the first locale but any valid locale should work.
derekblank
self-requested a review
April 30, 2024 23:20
derekblank
approved these changes
May 1, 2024
derekblank
left a comment
Contributor
There was a problem hiding this comment.
LGTM. I was able to follow the testing steps and review the automated test cases successfully.
wojtekn
approved these changes
May 2, 2024
wojtekn
left a comment
Contributor
There was a problem hiding this comment.
Thanks, it looks cleaner now and works as expected.
katinthehatsite
approved these changes
May 3, 2024
katinthehatsite
left a comment
Contributor
There was a problem hiding this comment.
The test plan succeded for me! Nice work 👍
# Conflicts: # src/lib/tests/site-language.test.ts # src/tests/site-server.test.ts
3 tasks
wojtekn
added a commit
that referenced
this pull request
Jun 3, 2026
…th (CodeQL #70) Replace the two chained .replace() calls with a single-pass regex that handles both \ and " in one substitution. This eliminates any ordering ambiguity between the two transforms and resolves the CodeQL js/incomplete-sanitization alert: there is now no intermediate state where a newly introduced backslash could interact with a not-yet-processed double-quote character.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to https://github.com/Automattic/dotcom-forge/issues/6698.
Proposed Changes
app.getLocalefunction as the requested locale to determine the supported locale. This approach supersedes the use of preferred languages, preventing the selection of an unexpected locale for the app.Testing Instructions
Preparation:
Supported languages
Unsupported languages
Pre-merge Checklist