mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-01 16:38:52 +00:00
feat(login): dynamically configure languages based on settings (#11372)
Closes #11199 # Which Problems Are Solved This PR refactors the login app to load allowed languages and the default language directly from the Zitadel API equivalents (`getGeneralSettings`), rather than relying on a hardcoded list. This ensures that the available languages in the UI and the locale selection logic match the instance's configuration. # How the Problems Are Solved - Fetches allowed languages server-side and passes them to the LanguageSwitcher - The NEXT_LOCALE cookie and Accept-Language headers are now strictly validated against the API-provided allowed languages. - Fallback: If a cookie requests an unsupported language, the system now falls back to the API's defaultLanguage (instead of valid but unconfigured defaults). # Additional Changes Helper: Added `getLanguage(code)` to map API language codes to display names (utilizing Intl.DisplayNames if the code isn't in our static mapping). --------- Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
co-authored by
Livio Spring
parent
d27833e1bc
commit
ab0f045499
@@ -1,6 +1,6 @@
|
||||
import { LANGS, LANGUAGE_COOKIE_NAME, LANGUAGE_HEADER_NAME } from "@/lib/i18n";
|
||||
import { getServiceConfig } from "@/lib/service-url";
|
||||
import { getHostedLoginTranslation } from "@/lib/zitadel";
|
||||
import { getHostedLoginTranslation, getAllowedLanguages } from "@/lib/zitadel";
|
||||
import { JsonObject } from "@zitadel/client";
|
||||
import deepmerge from "deepmerge";
|
||||
import { getRequestConfig } from "next-intl/server";
|
||||
@@ -10,23 +10,43 @@ export default getRequestConfig(async () => {
|
||||
const fallback = "en";
|
||||
const cookiesList = await cookies();
|
||||
|
||||
let locale: string = fallback;
|
||||
|
||||
const _headers = await headers();
|
||||
const { serviceConfig } = getServiceConfig(_headers);
|
||||
|
||||
let allowedLanguages = LANGS.map((l) => l.code);
|
||||
let defaultLanguage = fallback;
|
||||
|
||||
try {
|
||||
const settings = await getAllowedLanguages({ serviceConfig });
|
||||
if (settings.allowedLanguages?.length) {
|
||||
const localLanguageCodes = LANGS.map((l) => l.code);
|
||||
allowedLanguages = settings.allowedLanguages.filter((l) => localLanguageCodes.includes(l));
|
||||
}
|
||||
if (settings.defaultLanguage) {
|
||||
defaultLanguage = settings.defaultLanguage;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Failed to load global settings", e);
|
||||
}
|
||||
|
||||
let locale: string = defaultLanguage;
|
||||
|
||||
const languageHeader = await (await headers()).get(LANGUAGE_HEADER_NAME);
|
||||
if (languageHeader) {
|
||||
const headerLocale = languageHeader.split(",")[0].split("-")[0]; // Extract the language code
|
||||
if (LANGS.map((l) => l.code).includes(headerLocale)) {
|
||||
// splits "en-US,en;q=0.9" to ["en", "US"] or ["en"]
|
||||
const headerLocale = languageHeader.split(",")[0].split("-")[0];
|
||||
if (allowedLanguages.includes(headerLocale)) {
|
||||
locale = headerLocale;
|
||||
}
|
||||
}
|
||||
|
||||
const languageCookie = cookiesList?.get(LANGUAGE_COOKIE_NAME);
|
||||
if (languageCookie && languageCookie.value) {
|
||||
if (LANGS.map((l) => l.code).includes(languageCookie.value)) {
|
||||
if (allowedLanguages.includes(languageCookie.value)) {
|
||||
locale = languageCookie.value;
|
||||
} else {
|
||||
// If the cookie tells a language that is other than the supported ones, fall back to the default.
|
||||
locale = defaultLanguage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +54,9 @@ export default getRequestConfig(async () => {
|
||||
|
||||
let translations: JsonObject | {} = {};
|
||||
try {
|
||||
const i18nJSON = await getHostedLoginTranslation({ serviceConfig, locale,
|
||||
const i18nJSON = await getHostedLoginTranslation({
|
||||
serviceConfig,
|
||||
locale,
|
||||
organization: i18nOrganization,
|
||||
});
|
||||
|
||||
@@ -46,16 +68,23 @@ export default getRequestConfig(async () => {
|
||||
}
|
||||
|
||||
const customMessages = translations;
|
||||
const localeMessages = (await import(`../../locales/${locale}.json`)).default;
|
||||
const fallbackMessages = (await import(`../../locales/${fallback}.json`))
|
||||
.default;
|
||||
|
||||
// Load locale messages, fall back to default language messages if locale not found
|
||||
let localeMessages;
|
||||
try {
|
||||
localeMessages = (await import(`../../locales/${locale}.json`)).default;
|
||||
} catch {
|
||||
try {
|
||||
localeMessages = (await import(`../../locales/${defaultLanguage}.json`)).default;
|
||||
} catch {
|
||||
localeMessages = (await import(`../../locales/${fallback}.json`)).default;
|
||||
}
|
||||
}
|
||||
|
||||
const fallbackMessages = (await import(`../../locales/${fallback}.json`)).default;
|
||||
|
||||
return {
|
||||
locale,
|
||||
messages: deepmerge.all([
|
||||
fallbackMessages,
|
||||
localeMessages,
|
||||
customMessages,
|
||||
]) as Record<string, string>,
|
||||
messages: deepmerge.all([fallbackMessages, localeMessages, customMessages]) as Record<string, string>,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user