diff --git a/js/apps/account-ui/pom.xml b/js/apps/account-ui/pom.xml index 4c0e60a432..e2cbb8e115 100644 --- a/js/apps/account-ui/pom.xml +++ b/js/apps/account-ui/pom.xml @@ -116,6 +116,7 @@ "resourceUrl": "${resourceUrl}", "logo": "${properties.logo!""}", "logoUrl": "${properties.logoUrl!""}", + "locale": "${locale}", "features": { "isRegistrationEmailAsUsername": ${realm.registrationEmailAsUsername?c}, "isEditUserNameAllowed": ${realm.editUsernameAllowed?c}, diff --git a/js/apps/account-ui/src/environment.ts b/js/apps/account-ui/src/environment.ts index 8aaf77e276..425e0c26d9 100644 --- a/js/apps/account-ui/src/environment.ts +++ b/js/apps/account-ui/src/environment.ts @@ -11,6 +11,8 @@ export type Environment = { logo: string; /** Indicates the url to be followed when Brand image is clicked */ logoUrl: string; + /** The locale of the user */ + locale: string; /** Feature flags */ features: { isRegistrationEmailAsUsername: boolean; @@ -36,6 +38,7 @@ const defaultEnvironment: Environment = { resourceUrl: "http://localhost:8080", logo: "/logo.svg", logoUrl: "/", + locale: "en", features: { isRegistrationEmailAsUsername: false, isEditUserNameAllowed: true, diff --git a/js/apps/account-ui/src/i18n.ts b/js/apps/account-ui/src/i18n.ts index e305f17f64..b8c42565fa 100644 --- a/js/apps/account-ui/src/i18n.ts +++ b/js/apps/account-ui/src/i18n.ts @@ -1,4 +1,4 @@ -import { createInstance } from "i18next"; +import { LanguageDetectorModule, createInstance } from "i18next"; import HttpBackend from "i18next-http-backend"; import { initReactI18next } from "react-i18next"; @@ -14,6 +14,14 @@ type KeyValue = { key: string; value: string }; // that we can have a proper type-safe translation function. export type TFuncKey = any; +export const keycloakLanguageDetector: LanguageDetectorModule = { + type: "languageDetector", + + detect() { + return environment.locale; + }, +}; + export const i18n = createInstance({ fallbackLng: DEFAULT_LOCALE, interpolation: { @@ -35,4 +43,5 @@ export const i18n = createInstance({ }); i18n.use(HttpBackend); +i18n.use(keycloakLanguageDetector); i18n.use(initReactI18next); diff --git a/js/apps/account-ui/src/personal-info/PersonalInfo.tsx b/js/apps/account-ui/src/personal-info/PersonalInfo.tsx index c7923a74c2..eec3b80b55 100644 --- a/js/apps/account-ui/src/personal-info/PersonalInfo.tsx +++ b/js/apps/account-ui/src/personal-info/PersonalInfo.tsx @@ -29,7 +29,7 @@ import { } from "../api/representations"; import { Page } from "../components/page/Page"; import { environment } from "../environment"; -import { TFuncKey } from "../i18n"; +import { TFuncKey, i18n } from "../i18n"; import { usePromise } from "../utils/usePromise"; const PersonalInfo = () => { @@ -58,6 +58,12 @@ const PersonalInfo = () => { const onSubmit = async (user: UserRepresentation) => { try { await savePersonalInfo(user); + const locale = user.attributes?.["locale"]?.toString(); + i18n.changeLanguage(locale, (error) => { + if (error) { + console.warn("Error(s) loading locale", locale, error); + } + }); keycloak?.updateToken(); addAlert(t("accountUpdatedMessage")); } catch (error) {