diff --git a/apps/compliance-portal/src/lib/i18n/resolveLanguage.ts b/apps/compliance-portal/src/lib/i18n/resolveLanguage.ts index 78295803d..85388ba91 100644 --- a/apps/compliance-portal/src/lib/i18n/resolveLanguage.ts +++ b/apps/compliance-portal/src/lib/i18n/resolveLanguage.ts @@ -18,27 +18,57 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -export const SUPPORTED_LANGUAGES = ["en-US", "fr-FR"] as const; +export const SUPPORTED_LANGUAGES = [ + "en-US", + "fr-FR", + "de-DE", + "es-ES", + "id-ID", + "it-IT", + "ja-JP", + "ko-KR", + "pl-PL", + "pt-PT", + "tr-TR", + "uk-UA", + "zh-CN", +] as const; export type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number]; -// Collapse the browser's preferred languages to one of our supported locales: -// any fr* tag maps to fr-FR, any en* tag maps to en-US. en-US is the ultimate -// fallback when nothing matches. Resolving to a canonical supported tag here -// means i18next is never asked to load an unsupported locale; fallbackLng only -// has to cover individual missing keys. +// Maps a two-letter language prefix (lowercased) to its canonical supported +// tag, e.g. any "de*" browser tag resolves to "de-DE". +const PREFIX_TO_LANGUAGE: Record = { + en: "en-US", + fr: "fr-FR", + de: "de-DE", + es: "es-ES", + id: "id-ID", + it: "it-IT", + ja: "ja-JP", + ko: "ko-KR", + pl: "pl-PL", + pt: "pt-PT", + tr: "tr-TR", + uk: "uk-UA", + zh: "zh-CN", +}; + +// Collapse the browser's preferred languages to one of our supported locales +// by matching the two-letter language prefix (e.g. any "fr*" tag maps to +// fr-FR). en-US is the ultimate fallback when nothing matches. Resolving to a +// canonical supported tag here means i18next is never asked to load an +// unsupported locale; fallbackLng only has to cover individual missing keys. export function resolveLanguage(): SupportedLanguage { const candidates = navigator.languages?.length ? navigator.languages : [navigator.language]; for (const tag of candidates) { - const lower = tag.toLowerCase(); - if (lower.startsWith("fr")) { - return "fr-FR"; - } - if (lower.startsWith("en")) { - return "en-US"; + const prefix = tag.toLowerCase().split("-")[0]; + const language = PREFIX_TO_LANGUAGE[prefix]; + if (language) { + return language; } }