2023-01-15 21:23:47 +09:00
|
|
|
export function matchLanguages(languages: string[], acceptLanguages: readonly string[]): string | null {
|
2023-01-13 02:52:52 +09:00
|
|
|
{
|
2023-01-25 07:35:26 +09:00
|
|
|
// const lang = acceptLanguages.map(userLang => languages.find(lang => lang.startsWith(userLang))).filter(v => !!v)[0]
|
|
|
|
// TODO: Support es-419, remove this code if we include spanish country variants
|
2023-03-01 06:36:05 +09:00
|
|
|
const lang = acceptLanguages.map(userLang => languages.find((currentLang) => {
|
|
|
|
if (currentLang === userLang)
|
|
|
|
return currentLang
|
2023-01-25 07:35:26 +09:00
|
|
|
|
2023-03-01 06:36:05 +09:00
|
|
|
// Edge browser: case for ca-valencia
|
|
|
|
if (currentLang === 'ca-valencia' && userLang === 'ca-Es-VALENCIA')
|
|
|
|
return currentLang
|
|
|
|
|
|
|
|
if (userLang.startsWith('es-') && userLang !== 'es-ES' && currentLang === 'es-419')
|
|
|
|
return currentLang
|
|
|
|
|
|
|
|
return currentLang.startsWith(userLang) ? currentLang : undefined
|
|
|
|
})).filter(v => !!v)?.[0]
|
2023-01-13 02:52:52 +09:00
|
|
|
if (lang)
|
|
|
|
return lang
|
|
|
|
}
|
|
|
|
|
|
|
|
const lang = acceptLanguages.map((userLang) => {
|
|
|
|
userLang = userLang.split('-')[0]!
|
|
|
|
return languages.find(lang => lang.startsWith(userLang))
|
|
|
|
}).filter(v => !!v)[0]
|
|
|
|
if (lang)
|
|
|
|
return lang
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|