web: improve detection for locales

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>

#3163
This commit is contained in:
Jens Langhammer 2022-06-29 11:15:44 +02:00
parent 4d80e207da
commit d1479a1b16
2 changed files with 18 additions and 10 deletions

4
web/package-lock.json generated
View File

@ -1,11 +1,11 @@
{ {
"name": "authentik-web", "name": "@goauthentik/web",
"version": "2022.6.3", "version": "2022.6.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "authentik-web", "name": "@goauthentik/web",
"version": "2022.6.3", "version": "2022.6.3",
"license": "GNU GPLv3", "license": "GNU GPLv3",
"dependencies": { "dependencies": {

View File

@ -120,19 +120,27 @@ export const LOCALES: {
const DEFAULT_FALLBACK = () => "en"; const DEFAULT_FALLBACK = () => "en";
export function autoDetectLanguage() { export function autoDetectLanguage() {
let detected = const detected =
detect(fromUrl("locale"), fromNavigator(), DEFAULT_FALLBACK) || DEFAULT_FALLBACK(); detect(fromUrl("locale"), fromNavigator(), DEFAULT_FALLBACK) || DEFAULT_FALLBACK();
const locales = [detected];
// 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("_")) {
detected = detected.split("_")[0]; locales.push(detected.split("_")[0]);
} }
if (detected in i18n._messages) { if (detected.includes("-")) {
console.debug(`authentik/locale: Activating detected locale '${detected}'`); locales.push(detected.split("-")[0]);
activateLocale(detected);
} else {
console.debug(`authentik/locale: No locale for '${detected}', falling back to en`);
activateLocale(DEFAULT_FALLBACK());
} }
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) { export function activateLocale(code: string) {
const urlLocale = fromUrl("locale"); const urlLocale = fromUrl("locale");