ts
44 lines
930 Bytes
| 1 | export const defaultLang = "en"; |
| 2 | |
| 3 | interface Lang { |
| 4 | code: string; |
| 5 | name: string; |
| 6 | } |
| 7 | |
| 8 | export const supportedLangs: Lang[] = [ |
| 9 | { code: "en", name: "🇺🇸 English" }, |
| 10 | { code: "id", name: "🇮🇩 Indonesian" }, |
| 11 | { code: "se", name: "🇸🇪 Swedish" }, |
| 12 | { code: "de", name: "🇩🇪 German" }, |
| 13 | { code: "fr", name: "🇫🇷 French" }, |
| 14 | { code: "zh", name: "🇨🇳 Chinese" }, |
| 15 | { code: "es", name: "🇪🇸 Spanish" }, |
| 16 | { code: "ja", name: "🇯🇵 Japanese" }, |
| 17 | ]; |
| 18 | |
| 19 | export function determineUserLang(acceptedLangs: string[]) { |
| 20 | |
| 21 | const acceptedLangCodes = acceptedLangs.map(stripCountry); |
| 22 | |
| 23 | const supportedLangCodes = Object.keys(supportedLangs); |
| 24 | |
| 25 | const matchingLangCode = acceptedLangCodes.find(code => |
| 26 | |
| 27 | supportedLangCodes.includes(code), |
| 28 | |
| 29 | ); |
| 30 | |
| 31 | return matchingLangCode || defaultLang; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function stripCountry(lang: string) { |
| 36 | return lang |
| 37 | |
| 38 | .trim() |
| 39 | |
| 40 | .replace("_", "-") |
| 41 | |
| 42 | .split("-")[0]; |
| 43 | |
| 44 | } |