web: prioritise ?locale parameter over saved locale

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-02-26 16:17:04 +01:00
parent 2f8dbe9b97
commit 2c54be85be
2 changed files with 12 additions and 4 deletions

View File

@ -1,5 +1,5 @@
import { CoreApi, SessionUser } from "@goauthentik/api"; import { CoreApi, SessionUser } from "@goauthentik/api";
import { i18n } from "@lingui/core"; import { activateLocale } from "../interfaces/locale";
import { DEFAULT_CONFIG } from "./Config"; import { DEFAULT_CONFIG } from "./Config";
let globalMePromise: Promise<SessionUser>; let globalMePromise: Promise<SessionUser>;
@ -12,7 +12,7 @@ export function me(): Promise<SessionUser> {
const locale = user.user.settings.locale; const locale = user.user.settings.locale;
if (locale && locale !== "") { if (locale && locale !== "") {
console.debug(`authentik/locale: Activating user's configured locale '${locale}'`); console.debug(`authentik/locale: Activating user's configured locale '${locale}'`);
i18n.activate(locale); activateLocale(locale);
} }
return user; return user;
}).catch((ex) => { }).catch((ex) => {

View File

@ -1,7 +1,7 @@
import { de, en, es, fr, pl, tr, zh } from "make-plural/plurals"; import { de, en, es, fr, pl, tr, zh } from "make-plural/plurals";
import { Messages, i18n } from "@lingui/core"; 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 { t } from "@lingui/macro";
import { messages as localeDE } from "../locales/de"; import { messages as localeDE } from "../locales/de";
@ -93,7 +93,7 @@ const DEFAULT_FALLBACK = () => "en";
export function autoDetectLanguage() { export function autoDetectLanguage() {
let detected = let detected =
detect(fromUrl("lang"), fromStorage("lang"), fromNavigator(), DEFAULT_FALLBACK) || detect(fromUrl("locale"), fromNavigator(), DEFAULT_FALLBACK) ||
DEFAULT_FALLBACK(); DEFAULT_FALLBACK();
// For now we only care about the first locale part // For now we only care about the first locale part
if (detected.includes("_")) { if (detected.includes("_")) {
@ -107,4 +107,12 @@ export function autoDetectLanguage() {
i18n.activate(DEFAULT_FALLBACK()); 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(); autoDetectLanguage();