|
| 1 | +import type { LocaleName } from '@common/i18n' |
| 2 | + |
| 3 | +export const languageOptions = [ |
| 4 | + { value: 'zh', label: '简体中文' }, |
| 5 | + { value: 'zh_hant', label: '繁體中文' }, |
| 6 | + { value: 'en', label: 'English' }, |
| 7 | + { value: 'fr', label: 'Français' }, |
| 8 | + { value: 'de', label: 'Deutsch' }, |
| 9 | + { value: 'ja', label: '日本語' }, |
| 10 | + { value: 'tr', label: 'Türkçe' }, |
| 11 | + { value: 'ko', label: '한국어' }, |
| 12 | + { value: 'pl', label: 'Polski' }, |
| 13 | +] as const satisfies ReadonlyArray<{ value: LocaleName; label: string }> |
| 14 | + |
| 15 | +type LanguageOptionValue = (typeof languageOptions)[number]['value'] |
| 16 | + |
| 17 | +const languageOptionValues = new Set<LocaleName>( |
| 18 | + languageOptions.map(({ value }) => value), |
| 19 | +) |
| 20 | + |
| 21 | +const localeAliases: Partial<Record<LocaleName, LanguageOptionValue>> = { |
| 22 | + cn: 'zh', |
| 23 | + 'zh-CN': 'zh', |
| 24 | + 'zh-TW': 'zh_hant', |
| 25 | +} |
| 26 | + |
| 27 | +export function normalizeLanguageOptionValue( |
| 28 | + locale?: LocaleName, |
| 29 | +): LanguageOptionValue | undefined { |
| 30 | + if (!locale) return undefined |
| 31 | + |
| 32 | + const alias = localeAliases[locale] |
| 33 | + if (alias) return alias |
| 34 | + |
| 35 | + return languageOptionValues.has(locale) ? (locale as LanguageOptionValue) : undefined |
| 36 | +} |
| 37 | + |
| 38 | +export function resolveLanguageSelectValue( |
| 39 | + configLocale: LocaleName | undefined, |
| 40 | + activeLocale: LocaleName, |
| 41 | +): LanguageOptionValue { |
| 42 | + return ( |
| 43 | + normalizeLanguageOptionValue(configLocale) ?? |
| 44 | + normalizeLanguageOptionValue(activeLocale) ?? |
| 45 | + 'en' |
| 46 | + ) |
| 47 | +} |
0 commit comments