From 2c54be85be18f46c549e38467bbf6b889ba541ce Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 26 Feb 2022 16:17:04 +0100 Subject: [PATCH] web: prioritise ?locale parameter over saved locale Signed-off-by: Jens Langhammer --- web/src/api/Users.ts | 4 ++-- web/src/interfaces/locale.ts | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/web/src/api/Users.ts b/web/src/api/Users.ts index d3cc0497a..201322a24 100644 --- a/web/src/api/Users.ts +++ b/web/src/api/Users.ts @@ -1,5 +1,5 @@ import { CoreApi, SessionUser } from "@goauthentik/api"; -import { i18n } from "@lingui/core"; +import { activateLocale } from "../interfaces/locale"; import { DEFAULT_CONFIG } from "./Config"; let globalMePromise: Promise; @@ -12,7 +12,7 @@ export function me(): Promise { const locale = user.user.settings.locale; if (locale && locale !== "") { console.debug(`authentik/locale: Activating user's configured locale '${locale}'`); - i18n.activate(locale); + activateLocale(locale); } return user; }).catch((ex) => { diff --git a/web/src/interfaces/locale.ts b/web/src/interfaces/locale.ts index 2bd5a78dd..65d635061 100644 --- a/web/src/interfaces/locale.ts +++ b/web/src/interfaces/locale.ts @@ -1,7 +1,7 @@ import { de, en, es, fr, pl, tr, zh } from "make-plural/plurals"; import { Messages, i18n } from "@lingui/core"; -import { detect, fromNavigator, fromStorage, fromUrl } from "@lingui/detect-locale"; +import { detect, fromNavigator, fromUrl } from "@lingui/detect-locale"; import { t } from "@lingui/macro"; import { messages as localeDE } from "../locales/de"; @@ -93,7 +93,7 @@ const DEFAULT_FALLBACK = () => "en"; export function autoDetectLanguage() { let detected = - detect(fromUrl("lang"), fromStorage("lang"), fromNavigator(), DEFAULT_FALLBACK) || + detect(fromUrl("locale"), fromNavigator(), DEFAULT_FALLBACK) || DEFAULT_FALLBACK(); // For now we only care about the first locale part if (detected.includes("_")) { @@ -107,4 +107,12 @@ export function autoDetectLanguage() { i18n.activate(DEFAULT_FALLBACK()); } } +export function activateLocale(code: string) { + const urlLocale = fromUrl("locale"); + if (urlLocale !== null && urlLocale !== "") { + i18n.activate(urlLocale); + return; + } + i18n.activate(code); +} autoDetectLanguage();