Skip to content
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

Fix incorrect preferred locale selection with multiple languages configured #70

Merged
merged 5 commits into from
May 6, 2024

Conversation

fluiddot
Copy link
Contributor

@fluiddot fluiddot commented Apr 30, 2024

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 added the [Type] Bug Something isn't working label Apr 30, 2024
@fluiddot fluiddot requested a review from a team April 30, 2024 17:18
@fluiddot fluiddot self-assigned this 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 );
Copy link
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
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
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.

@@ -133,7 +126,7 @@ describe( 'getPreferredSiteLanguage', () => {
/* NOOP */
} );

mockPreferredLanguages( WP_5_0_LOCALES.map( ( item ) => item.locale ) );
mockAppLocale( WP_5_0_LOCALES[ 0 ].locale );
Copy link
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
Copy link
Contributor

@derekblank derekblank left a comment

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.

Copy link
Contributor

@wojtekn wojtekn left a comment

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.

Copy link
Contributor

@katinthehatsite katinthehatsite left a comment

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 👍

# Conflicts:
#	src/lib/tests/site-language.test.ts
#	src/tests/site-server.test.ts
@fluiddot fluiddot merged commit 741c444 into trunk May 6, 2024
6 checks passed
@fluiddot fluiddot deleted the fix/supported-locale-based-on-app-locale branch May 6, 2024 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants