fix(i18n): require BCP-47 boundary in normalizeLang prefix match (#302)
This commit is contained in:
@@ -54,7 +54,8 @@ export function normalizeLang(raw: string | null | undefined): Lang {
|
||||
// traditional-script Chinese variants must win over the generic 'zh' prefix
|
||||
if (/^zh[-_](tw|hk|mo|hant)/.test(value)) return 'zh-TW'
|
||||
for (const lang of LANGS) {
|
||||
if (lang !== 'en' && lang !== 'zh-TW' && value.startsWith(lang)) return lang
|
||||
if (lang === 'en' || lang === 'zh-TW') continue
|
||||
if (value === lang || value.startsWith(`${lang}-`) || value.startsWith(`${lang}_`)) return lang
|
||||
}
|
||||
// 'in' is the legacy ISO code for Indonesian still reported by some systems
|
||||
if (/^in\b/.test(value) || /^in[-_]/.test(value)) return 'id'
|
||||
|
||||
@@ -56,11 +56,24 @@ describe('normalizeLang', () => {
|
||||
|
||||
it('maps everything else (and missing) to en', () => {
|
||||
expect(normalizeLang('en-US')).toBe('en')
|
||||
expect(normalizeLang('en_US')).toBe('en')
|
||||
expect(normalizeLang('sv-SE')).toBe('en')
|
||||
expect(normalizeLang('')).toBe('en')
|
||||
expect(normalizeLang(undefined)).toBe('en')
|
||||
expect(normalizeLang(null)).toBe('en')
|
||||
})
|
||||
|
||||
it('requires a BCP-47 boundary after the language code', () => {
|
||||
expect(normalizeLang('deleted')).toBe('en')
|
||||
expect(normalizeLang('french')).toBe('en')
|
||||
expect(normalizeLang('thread')).toBe('en')
|
||||
expect(normalizeLang('italian')).toBe('en')
|
||||
expect(normalizeLang('hebrew')).toBe('en')
|
||||
expect(normalizeLang('de')).toBe('de')
|
||||
expect(normalizeLang('de-DE')).toBe('de')
|
||||
expect(normalizeLang('de_DE')).toBe('de')
|
||||
expect(normalizeLang('es-419')).toBe('es')
|
||||
})
|
||||
})
|
||||
|
||||
describe('isLang', () => {
|
||||
|
||||
Reference in New Issue
Block a user