Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 950c72e

Browse files
committed
fix(isLanguageAvailable): correct typo in assigned variable name
Was erroneously returning the locale language instead of the locale itself.
1 parent 870672b commit 950c72e

5 files changed

Lines changed: 135 additions & 5 deletions

File tree

packages/preferred-locale/src/index.spec.browser.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@ import preferredLocale from './'
22
import * as isLocaleSupported from './utils/isLocaleSupported'
33

44
describe('preferredLocale', () => {
5+
describe('with Intl.Locale supported', () => {
6+
it('handles region-less locales', () => {
7+
expect.assertions(1)
8+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en', 'en-US' ])
9+
const translations = [ 'fr-FR', 'id-ID', 'en-US' ]
10+
expect(preferredLocale(translations, 'en-US')).toBe('en-US')
11+
})
12+
13+
it('returns \'en-us\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca not translated)', () => {
14+
expect.assertions(1)
15+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
16+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
17+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
18+
})
19+
20+
it('returns \'en-ca\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca translated)', () => {
21+
expect.assertions(1)
22+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
23+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
24+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
25+
})
26+
})
27+
528
describe('without Intl.Locale supported', () => {
629
beforeEach(() => {
730
jest.spyOn(isLocaleSupported, 'isLocaleSupported').mockReturnValue(false)
@@ -13,5 +36,19 @@ describe('preferredLocale', () => {
1336
const translations = [ 'fr-FR', 'id-ID', 'en-US' ]
1437
expect(preferredLocale(translations, 'en-US')).toBe('en-US')
1538
})
39+
40+
it('returns \'en-us\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca not translated)', () => {
41+
expect.assertions(1)
42+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
43+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
44+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
45+
})
46+
47+
it('returns \'en-ca\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca translated)', () => {
48+
expect.assertions(1)
49+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
50+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
51+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
52+
})
1653
})
1754
})

packages/preferred-locale/src/utils/availableLocales/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isLanguageAvailable } from '../'
1+
import { isLanguageAvailable, deduplicate } from '../'
22

33
/**
44
* @name availableLocales
@@ -18,7 +18,7 @@ export const availableLocales = (
1818
if (!options.regionLowerCase) options.regionLowerCase = false
1919
if (!options.languageOnly) options.languageOnly = false
2020

21-
return userLocales.filter((userLocale, index, array) => {
21+
return deduplicate(userLocales.filter((userLocale, index, array) => {
2222
const formattedLocale = options.regionLowerCase
2323
? userLocale.locale.toLowerCase()
2424
: userLocale.locale
@@ -34,5 +34,5 @@ export const availableLocales = (
3434
array,
3535
options
3636
)
37-
})
37+
}))
3838
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { availableLocales } from './'
2+
import * as isLocaleSupported from '../isLocaleSupported'
3+
4+
describe('availableLocales', () => {
5+
describe('with Intl.Locale supported', () => {
6+
it('returns \'en-us\' when \'en-us\' is translated but \'en-ca\' is not', () => {
7+
expect.assertions(1)
8+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
9+
const browser = [
10+
{ locale: 'en-ca', priority: 0 },
11+
{ locale: 'en-us', priority: 1 }
12+
]
13+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
14+
{ locale: 'en-us', priority: 0 }
15+
])
16+
})
17+
18+
it('returns \'en-ca\' when \'en-ca\' and \'en-us\' are translated', () => {
19+
expect.assertions(1)
20+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
21+
const browser = [
22+
{ locale: 'en-ca', priority: 0 },
23+
{ locale: 'en-us', priority: 1 }
24+
]
25+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
26+
{ locale: 'en-ca', priority: 0 },
27+
{ locale: 'en-us', priority: 1 }
28+
])
29+
})
30+
})
31+
32+
describe('without Intl.Locale supported', () => {
33+
beforeEach(() => {
34+
jest.spyOn(isLocaleSupported, 'isLocaleSupported').mockReturnValue(false)
35+
})
36+
37+
it('returns \'en-us\' when \'en-us\' is translated but \'en-ca\' is not', () => {
38+
expect.assertions(1)
39+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
40+
const browser = [
41+
{ locale: 'en-ca', priority: 0 },
42+
{ locale: 'en-us', priority: 1 },
43+
{ locale: 'en', priority: 2 }
44+
]
45+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
46+
{ locale: 'en-us', priority: 0 }
47+
])
48+
})
49+
50+
it('returns \'en-ca\' when \'en-ca\' and \'en-us\' are translated', () => {
51+
expect.assertions(1)
52+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
53+
const browser = [
54+
{ locale: 'en-ca', priority: 0 },
55+
{ locale: 'en-us', priority: 1 },
56+
{ locale: 'en', priority: 2 }
57+
]
58+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
59+
{ locale: 'en-ca', priority: 0 },
60+
{ locale: 'en-us', priority: 1 }
61+
])
62+
})
63+
})
64+
})

packages/preferred-locale/src/utils/isLanguageAvailable/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const isLanguageAvailable = (
2222

2323
return translatedLocales.filter(translatedLocale => {
2424
const localeSupported = isLocaleSupported()
25-
// Backwards compatibility for older browsers
25+
// Backwards compatibility for older browsers (Don't return regionless locales unless language only)
2626
if (!localeSupported && !options.languageOnly && userLocale.split('-')[1] === undefined) return false
2727

2828
// Strip the region code from both locales (en-gb -> en)
@@ -37,7 +37,7 @@ export const isLanguageAvailable = (
3737

3838
// Update the locale field to the canonical region if there is no translations for the browser-provided region
3939
// For example, en-XX (unknown region) to en-US (translated)
40-
if (isLanguageAvailable) array[index].locale = translatedLanguage
40+
if (isLanguageAvailable) array[index].locale = translatedLocale
4141

4242
return isLanguageAvailable
4343
}).length > 0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { unifyUserLocales } from './'
2+
import * as isLocaleSupported from '../isLocaleSupported'
3+
4+
describe('preferredLocale', () => {
5+
describe('with Intl.Locale supported', () => {
6+
it('returns \'en-ca and en-us\' with \'en-CA, en-US, en and \'en-us\'', () => {
7+
expect.assertions(1)
8+
expect(unifyUserLocales([ 'en-CA', 'en-US', 'en', 'en-us' ], { regionLowerCase: true })).toStrictEqual([
9+
{ locale: 'en-ca', priority: 0 },
10+
{ locale: 'en-us', priority: 1 }
11+
])
12+
})
13+
})
14+
15+
describe('without Intl.Locale supported', () => {
16+
beforeEach(() => {
17+
jest.spyOn(isLocaleSupported, 'isLocaleSupported').mockReturnValue(false)
18+
})
19+
20+
it('returns \'en-ca, en-us and en\' with \'en-CA, en-US, en and \'en-us\'', () => {
21+
expect.assertions(1)
22+
expect(unifyUserLocales([ 'en-CA', 'en-US', 'en', 'en-us' ], { regionLowerCase: true })).toStrictEqual([
23+
{ locale: 'en-ca', priority: 0 },
24+
{ locale: 'en-us', priority: 1 },
25+
{ locale: 'en', priority: 2 }
26+
])
27+
})
28+
})
29+
})

0 commit comments

Comments
 (0)