From d1479a1b164c263d93b80e3a3cc91ac64b3dbb0e Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 29 Jun 2022 11:15:44 +0200 Subject: [PATCH] web: improve detection for locales Signed-off-by: Jens Langhammer #3163 --- web/package-lock.json | 4 ++-- web/src/interfaces/locale.ts | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/web/package-lock.json b/web/package-lock.json index 5f43d63cf..8847109b5 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -1,11 +1,11 @@ { - "name": "authentik-web", + "name": "@goauthentik/web", "version": "2022.6.3", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "authentik-web", + "name": "@goauthentik/web", "version": "2022.6.3", "license": "GNU GPLv3", "dependencies": { diff --git a/web/src/interfaces/locale.ts b/web/src/interfaces/locale.ts index 8a486e02d..b8337d0ba 100644 --- a/web/src/interfaces/locale.ts +++ b/web/src/interfaces/locale.ts @@ -120,19 +120,27 @@ export const LOCALES: { const DEFAULT_FALLBACK = () => "en"; export function autoDetectLanguage() { - let detected = + const detected = detect(fromUrl("locale"), fromNavigator(), DEFAULT_FALLBACK) || DEFAULT_FALLBACK(); + const locales = [detected]; // For now we only care about the first locale part if (detected.includes("_")) { - detected = detected.split("_")[0]; + locales.push(detected.split("_")[0]); } - if (detected in i18n._messages) { - console.debug(`authentik/locale: Activating detected locale '${detected}'`); - activateLocale(detected); - } else { - console.debug(`authentik/locale: No locale for '${detected}', falling back to en`); - activateLocale(DEFAULT_FALLBACK()); + if (detected.includes("-")) { + locales.push(detected.split("-")[0]); } + for (const tryLocale of locales) { + if (LOCALES.find((locale) => locale.code === tryLocale)) { + console.debug(`authentik/locale: Activating detected locale '${tryLocale}'`); + activateLocale(tryLocale); + return; + } else { + console.debug(`authentik/locale: No matching locale for ${tryLocale}`); + } + } + console.debug(`authentik/locale: No locale for '${locales}', falling back to en`); + activateLocale(DEFAULT_FALLBACK()); } export function activateLocale(code: string) { const urlLocale = fromUrl("locale");