-
Notifications
You must be signed in to change notification settings - Fork 86
Fix incorrect preferred locale selection with multiple languages configured #70
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
Changes from all commits
fa84beb
497f9d0
a058684
e5ff9ee
34e7f3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,71 +5,42 @@ import { app } from 'electron'; | |
| import { createI18n } from '@wordpress/i18n'; | ||
| import { getLocaleData, getSupportedLocale } from '../locale'; | ||
|
|
||
| jest.mock( 'electron', () => ( { | ||
| app: { | ||
| getPreferredSystemLanguages: jest.fn().mockReturnValue( [ 'en-US' ] ), | ||
| }, | ||
| } ) ); | ||
|
|
||
| function mockPreferredLanguages( languages: string[] ) { | ||
| ( app.getPreferredSystemLanguages as jest.Mock ).mockReturnValue( languages ); | ||
| function mockAppLocale( language: string ) { | ||
| ( app.getLocale as jest.Mock ).mockReturnValue( language ); | ||
| } | ||
|
|
||
| describe( 'getSupportedLocale', () => { | ||
| it( 'converts a language-region pair to a glotpress locale slug', () => { | ||
| mockPreferredLanguages( [ 'en-US' ] ); | ||
| mockAppLocale( 'en-US' ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'en' ); | ||
| } ); | ||
|
|
||
| it( 'returns English if preferred language is unsupported', () => { | ||
| mockPreferredLanguages( [ 'mi-NZ' ] ); | ||
| it( 'returns English if app locale is unsupported', () => { | ||
| mockAppLocale( 'mi-NZ' ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'en' ); | ||
| } ); | ||
|
|
||
| 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' ); | ||
| } ); | ||
|
Comment on lines
-31
to
-47
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| it( 'returns zh-cn variant', () => { | ||
| mockPreferredLanguages( [ 'zh-cn', 'zh-tw' ] ); | ||
| mockAppLocale( 'zh-cn' ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'zh-cn' ); | ||
| } ); | ||
|
|
||
| it( 'returns zh-tw variant', () => { | ||
| mockPreferredLanguages( [ 'zh-tw', 'zh-cn' ] ); | ||
| mockAppLocale( 'zh-tw' ); | ||
|
|
||
| expect( getSupportedLocale() ).toBe( 'zh-tw' ); | ||
| } ); | ||
|
|
||
| it( "prefers a language with a different region over an exact language _and_ region match which is further down the user's preference list", () => { | ||
| mockPreferredLanguages( [ 'fr-PL', 'pt-BR' ] ); | ||
| expect( getSupportedLocale() ).toBe( 'fr' ); | ||
| } ); | ||
|
|
||
| it( 'returns the Simplified Chinese zh-cn option when the user preference is zh-Hans', () => { | ||
| mockPreferredLanguages( [ 'zh-NZ', 'zh-Hans' ] ); | ||
| mockAppLocale( 'zh-Hans' ); | ||
| expect( getSupportedLocale() ).toBe( 'zh-cn' ); | ||
| } ); | ||
|
|
||
| it( 'returns the Traditional Chinese zh-tw option when the user preference is zh-Hant', () => { | ||
| mockPreferredLanguages( [ 'zh-Hant' ] ); | ||
| mockAppLocale( 'zh-Hant' ); | ||
| expect( getSupportedLocale() ).toBe( 'zh-tw' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ import { getPreferredSiteLanguage } from '../site-language'; | |
|
|
||
| const originalFetch = global.fetch; | ||
|
|
||
| function mockPreferredLanguages( languages: string[] ) { | ||
| ( app.getPreferredSystemLanguages as jest.Mock ).mockReturnValue( languages ); | ||
| function mockAppLocale( language: string ) { | ||
| ( app.getLocale as jest.Mock ).mockReturnValue( language ); | ||
| } | ||
|
|
||
| function mockFetchTranslations( wpVersion: string, translations: string[] ) { | ||
|
|
@@ -62,15 +62,15 @@ describe( 'getPreferredSiteLanguage', () => { | |
| ]; | ||
|
|
||
| it( "returns 'en' as default language", async () => { | ||
| mockPreferredLanguages( [] ); | ||
| mockAppLocale( 'mi-NZ' ); | ||
|
|
||
| expect( await getPreferredSiteLanguage() ).toBe( 'en' ); | ||
| } ); | ||
|
|
||
| it.each( LATEST_WP_VERSION_LOCALES )( | ||
| "returns '$expected' for language '$locale'", | ||
| async ( { locale, expected } ) => { | ||
| mockPreferredLanguages( [ locale ] ); | ||
| mockAppLocale( locale ); | ||
|
|
||
| expect( await getPreferredSiteLanguage() ).toBe( expected ); | ||
| } | ||
|
|
@@ -108,17 +108,10 @@ describe( 'getPreferredSiteLanguage', () => { | |
| { locale: 'zh-Hant-TW', expected: 'en' }, | ||
| ]; | ||
|
|
||
| it( "returns 'en' as default language", async () => { | ||
| mockPreferredLanguages( [] ); | ||
| mockFetchTranslations( WP_VERSION, AVAILABLE_LOCALES ); | ||
|
|
||
| expect( await getPreferredSiteLanguage( WP_VERSION ) ).toBe( 'en' ); | ||
| } ); | ||
|
Comment on lines
-111
to
-116
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case is no longer needed as |
||
|
|
||
| it.each( WP_5_0_LOCALES )( | ||
| "returns '$expected' for language '$locale'", | ||
| async ( { locale, expected } ) => { | ||
| mockPreferredLanguages( [ locale ] ); | ||
| mockAppLocale( locale ); | ||
| mockFetchTranslations( WP_VERSION, AVAILABLE_LOCALES ); | ||
|
|
||
| expect( await getPreferredSiteLanguage( WP_VERSION ) ).toBe( expected ); | ||
|
|
@@ -130,7 +123,7 @@ describe( 'getPreferredSiteLanguage', () => { | |
| /* NOOP */ | ||
| } ); | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We pick the first locale but any valid locale should work. |
||
| mockFetchTranslations( 'unknown', [] ); | ||
|
|
||
| expect( await getPreferredSiteLanguage( WP_VERSION ) ).toBe( 'en' ); | ||
|
|
||
There was a problem hiding this comment.
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.getLocalethat is only available when the app is read.