Skip to content

Commit

Permalink
i18n: accept array of locales in lookupLocale (#11349)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Aug 31, 2020
1 parent e9215f1 commit aac0b68
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lighthouse-core/lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const MessageFormat = require('intl-messageformat').default;
const lookupClosestLocale = require('lookup-closest-locale');
const LOCALES = require('./locales.js');

const DEFAULT_LOCALE = 'en';

/** @typedef {import('intl-messageformat-parser').Element} MessageElement */
/** @typedef {import('intl-messageformat-parser').ArgumentElement} ArgumentElement */

Expand Down Expand Up @@ -157,18 +159,17 @@ const formats = {
* Look up the best available locale for the requested language through these fall backs:
* - exact match
* - progressively shorter prefixes (`de-CH-1996` -> `de-CH` -> `de`)
* - the default locale ('en') if no match is found
*
* If `locale` isn't provided, the default is used.
* @param {string=} locale
* If `locale` isn't provided or one could not be found, DEFAULT_LOCALE is returned.
* @param {string|string[]=} locales
* @return {LH.Locale}
*/
function lookupLocale(locale) {
function lookupLocale(locales) {
// TODO: could do more work to sniff out default locale
const canonicalLocale = Intl.getCanonicalLocales(locale)[0];
const canonicalLocales = Intl.getCanonicalLocales(locales);

const closestLocale = lookupClosestLocale(canonicalLocale, LOCALES);
return closestLocale || 'en';
const closestLocale = lookupClosestLocale(canonicalLocales, LOCALES);
return closestLocale || DEFAULT_LOCALE;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions lighthouse-core/test/lib/i18n/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,28 @@ describe('i18n', () => {
});

describe('#lookupLocale', () => {
const invalidLocale = 'jk-Latn-DE-1996-a-ext-x-phonebk-i-klingon';

it('canonicalizes the locale', () => {
expect(i18n.lookupLocale('en-xa')).toEqual('en-XA');
});

it('canonicalizes the locales', () => {
expect(i18n.lookupLocale([invalidLocale, 'en-xa'])).toEqual('en-XA');
});

it('falls back to default if locale not provided or cant be found', () => {
expect(i18n.lookupLocale(undefined)).toEqual('en');
expect(i18n.lookupLocale(invalidLocale)).toEqual('en');
expect(i18n.lookupLocale([invalidLocale, invalidLocale])).toEqual('en');
});

it('falls back to root tag prefix if specific locale not available', () => {
expect(i18n.lookupLocale('en-JKJK')).toEqual('en');
expect(i18n.lookupLocale('es-JKJK')).toEqual('es');
});

it('falls back to en if no match is available', () => {
expect(i18n.lookupLocale('jk-Latn-DE-1996-a-ext-x-phonebk-i-klingon')).toEqual('en');
expect(i18n.lookupLocale(invalidLocale)).toEqual('en');
});
});

Expand Down
2 changes: 1 addition & 1 deletion types/lookup-closest-locale/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

declare module 'lookup-closest-locale' {
function lookupClosestLocale(locale: string|undefined, available: Record<LH.Locale, any>): LH.Locale|undefined;
function lookupClosestLocale(locale: string[]|string|undefined, available: Record<LH.Locale, any>): LH.Locale|undefined;

export = lookupClosestLocale;
}

0 comments on commit aac0b68

Please sign in to comment.