@@ -6,28 +6,39 @@ import { isLocaleSupported } from '../'
66 * @param {string[] } translatedLocales Translations provided by the application
77 * @param {number } index The index of the unified browser locale in `array`
88 * @param {Object[] } array The unified browser locale array
9+ * @param {Object } [options={}] Configuration options
10+ * @param {boolean } [options.languageOnly=false] If true, returns `en` instead of `en-US` or `en-us`
911 * @returns {boolean } Is the language available?
1012 * @private
1113 */
1214export const isLanguageAvailable = (
1315 userLocale ,
1416 translatedLocales ,
1517 index ,
16- array
17- ) => translatedLocales . filter ( translatedLocale => {
18- // Strip the region code from both locales (en-gb -> en)
19- const translatedLanguage = isLocaleSupported ( )
20- ? new Intl . Locale ( translatedLocale ) . minimize ( ) . language
21- : translatedLocale . split ( '-' ) [ 0 ]
22- const browserLanguage = isLocaleSupported ( )
23- ? new Intl . Locale ( userLocale ) . minimize ( ) . language
24- : userLocale . split ( '-' ) [ 0 ]
18+ array ,
19+ options = { }
20+ ) => {
21+ if ( ! options . languageOnly ) options . languageOnly = false
2522
26- const isLanguageAvailable = translatedLanguage === browserLanguage
23+ return translatedLocales . filter ( translatedLocale => {
24+ const localeSupported = isLocaleSupported ( )
25+ // Backwards compatibility for older browsers
26+ if ( ! localeSupported && ! options . languageOnly && userLocale . split ( '-' ) [ 1 ] === undefined ) return false
2727
28- // Update the locale field to the canonical region if there is no translations for the browser-provided region
29- // For example, en-XX (unknown region) to en-US (translated)
30- if ( isLanguageAvailable ) array [ index ] . locale = translatedLanguage
28+ // Strip the region code from both locales (en-gb -> en)
29+ const translatedLanguage = localeSupported
30+ ? new Intl . Locale ( translatedLocale ) . minimize ( ) . language
31+ : translatedLocale . split ( '-' ) [ 0 ]
32+ const browserLanguage = localeSupported
33+ ? new Intl . Locale ( userLocale ) . minimize ( ) . language
34+ : userLocale . split ( '-' ) [ 0 ]
3135
32- return isLanguageAvailable
33- } ) . length > 0
36+ const isLanguageAvailable = translatedLanguage === browserLanguage
37+
38+ // Update the locale field to the canonical region if there is no translations for the browser-provided region
39+ // For example, en-XX (unknown region) to en-US (translated)
40+ if ( isLanguageAvailable ) array [ index ] . locale = translatedLanguage
41+
42+ return isLanguageAvailable
43+ } ) . length > 0
44+ }
0 commit comments