Skip to content

Commit

Permalink
web/common: fix locale detection for user-set locale (#9436)
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu authored Apr 25, 2024
1 parent a306bb8 commit 8d37e83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
2 changes: 1 addition & 1 deletion web/src/common/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function me(): Promise<SessionUser> {
if (!user.user.settings || !("locale" in user.user.settings)) {
return user;
}
const locale = user.user.settings.locale;
const locale: string | undefined = user.user.settings.locale;
if (locale && locale !== "") {
console.debug(
`authentik/locale: Activating user's configured locale '${locale}'`,
Expand Down
25 changes: 7 additions & 18 deletions web/src/elements/ak-locale-context/ak-locale-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EVENT_LOCALE_CHANGE, EVENT_LOCALE_REQUEST } from "@goauthentik/common/c
import { customEvent } from "@goauthentik/elements/utils/customEvents";

import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { customElement, property } from "lit/decorators.js";

import { WithBrandConfig } from "../Interface/brandProvider";
import { initializeLocalization } from "./configureLocale";
Expand Down Expand Up @@ -38,9 +38,6 @@ export class LocaleContext extends LocaleContextBase {

setLocale: LocaleSetter;

@state()
userLocale = "";

constructor(code = DEFAULT_LOCALE) {
super();
this.notifyApplication = this.notifyApplication.bind(this);
Expand All @@ -59,30 +56,22 @@ export class LocaleContext extends LocaleContextBase {

connectedCallback() {
super.connectedCallback();
// Commenting out until we can come up with a better way of separating the
// "request user identity" with the session expiration heartbeat.
/*
new CoreApi(DEFAULT_CONFIG)
.coreUsersMeRetrieve()
.then((user) => (this.userLocale = user?.user?.settings?.locale ?? ""))
.catch(() => {});
*/
this.updateLocale();
window.addEventListener(EVENT_LOCALE_REQUEST, this.updateLocaleHandler);
window.addEventListener(EVENT_LOCALE_REQUEST, this.updateLocaleHandler as EventListener);
}

disconnectedCallback() {
window.removeEventListener(EVENT_LOCALE_REQUEST, this.updateLocaleHandler);
window.removeEventListener(EVENT_LOCALE_REQUEST, this.updateLocaleHandler as EventListener);
super.disconnectedCallback();
}

updateLocaleHandler(_ev: Event) {
updateLocaleHandler(ev: CustomEvent<{ locale: string }>) {
console.debug("authentik/locale: Locale update request received.");
this.updateLocale();
this.updateLocale(ev.detail.locale);
}

updateLocale() {
const localeRequest = autoDetectLanguage(this.userLocale, this.brand?.defaultLocale);
updateLocale(requestedLocale: string | undefined = undefined) {
const localeRequest = autoDetectLanguage(requestedLocale, this.brand?.defaultLocale);
const locale = getBestMatchLocale(localeRequest);
if (!locale) {
console.warn(`authentik/locale: failed to find locale for code ${localeRequest}`);
Expand Down

0 comments on commit 8d37e83

Please sign in to comment.