Skip to content

Fix incorrect preferred locale selection with multiple languages configured - #70

Merged
fluiddot merged 5 commits into
trunkfrom
fix/supported-locale-based-on-app-locale
May 6, 2024
Merged

Fix incorrect preferred locale selection with multiple languages configured#70
fluiddot merged 5 commits into
trunkfrom
fix/supported-locale-based-on-app-locale

Conversation

@fluiddot

@fluiddot fluiddot commented Apr 30, 2024

Copy link
Copy Markdown
Contributor

Related to https://github.com/Automattic/dotcom-forge/issues/6698.

Proposed Changes

Testing Instructions

Preparation:

  • Add different languages to the preferred languages list on your OS.

Supported languages

  • Change the language of your OS to "English" and the region to a non-English location (e.g. Spain).
  • Open the app.
  • Observe the app is localized to English.
  • Change the language of your OS to "Spanish" and the region to a non-Spanish location (e.g. Australia).
  • Open the app.
  • Observe the app is localized to Spanish.
  • Change the language of your OS to "French" and the region to a non-French location (e.g. Japan).
  • Open the app.
  • Observe the app is localized to French.

Unsupported languages

  • Change the language of your OS to "Finnish".
  • Open the app.
  • Observe the app is localized to English.

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
@fluiddot
fluiddot requested a review from a team April 30, 2024 17:18
@fluiddot fluiddot self-assigned this Apr 30, 2024
Comment thread src/index.ts
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 );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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' );
} );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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' );
} );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We pick the first locale but any valid locale should work.

@derekblank
derekblank self-requested a review April 30, 2024 23:20

@derekblank derekblank left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. I was able to follow the testing steps and review the automated test cases successfully.

@wojtekn wojtekn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, it looks cleaner now and works as expected.

@katinthehatsite katinthehatsite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test plan succeded for me! Nice work 👍

fluiddot added 2 commits May 6, 2024 10:37
# Conflicts:
#	src/lib/tests/site-language.test.ts
#	src/tests/site-server.test.ts
@fluiddot
fluiddot merged commit 741c444 into trunk May 6, 2024
@fluiddot
fluiddot deleted the fix/supported-locale-based-on-app-locale branch May 6, 2024 08:46
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants