From e962b37948d15fba0cf73177968187eb6ae951c3 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Tue, 14 May 2024 02:32:03 +0200 Subject: [PATCH] Fix language menu select in templates --- src/account/Template.tsx | 7 +- src/account/i18n/i18n.tsx | 13 ++- src/login/Template.tsx | 28 ++----- src/login/i18n/i18n.tsx | 13 ++- src/login/template.ftl | 169 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 43 deletions(-) create mode 100644 src/login/template.ftl diff --git a/src/account/Template.tsx b/src/account/Template.tsx index a989a4ef..0d0560c9 100644 --- a/src/account/Template.tsx +++ b/src/account/Template.tsx @@ -15,7 +15,7 @@ export default function Template(props: TemplateProps) { const { getClassName } = useGetClassName({ doUseDefaultCss, classes }); - const { msg, msgStr, changeLocale, labelBySupportedLanguageTag, currentLanguageTag } = i18n; + const { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n; const { locale, url, features, realm, message, referrer } = kcContext; @@ -81,10 +81,7 @@ export default function Template(props: TemplateProps) { diff --git a/src/account/i18n/i18n.tsx b/src/account/i18n/i18n.tsx index 2183515e..cf3fce03 100644 --- a/src/account/i18n/i18n.tsx +++ b/src/account/i18n/i18n.tsx @@ -28,11 +28,10 @@ export type GenericI18n = { */ currentLanguageTag: string; /** - * To call when the user switch language. - * This will cause the page to be reloaded, - * on next load currentLanguageTag === newLanguageTag + * Redirect to this url to change the language. + * After reload currentLanguageTag === newLanguageTag */ - changeLocale: (newLanguageTag: string) => never; + getChangeLocalUrl: (newLanguageTag: string) => string; /** * e.g. "en" => "English", "fr" => "Français", ... * @@ -104,7 +103,7 @@ export function createUseI18n(extraMessa } as any }), currentLanguageTag, - "changeLocale": newLanguageTag => { + "getChangeLocalUrl": newLanguageTag => { const { locale } = kcContext; assert(locale !== undefined, "Internationalization not enabled"); @@ -113,9 +112,7 @@ export function createUseI18n(extraMessa assert(targetSupportedLocale !== undefined, `${newLanguageTag} need to be enabled in Keycloak admin`); - window.location.href = targetSupportedLocale.url; - - assert(false, "never"); + return targetSupportedLocale.url; }, "labelBySupportedLanguageTag": Object.fromEntries( (kcContext.locale?.supported ?? []).map(({ languageTag, label }) => [languageTag, label]) diff --git a/src/login/Template.tsx b/src/login/Template.tsx index 09492792..0d3a61a4 100644 --- a/src/login/Template.tsx +++ b/src/login/Template.tsx @@ -32,7 +32,7 @@ export default function Template(props: TemplateProps) { const { getClassName } = useGetClassName({ doUseDefaultCss, classes }); - const { msg, msgStr, changeLocale, labelBySupportedLanguageTag, currentLanguageTag } = i18n; + const { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n; const { realm, locale, auth, url, message, isAppInitiatedAction, authenticationSession, scripts } = kcContext; @@ -126,31 +126,17 @@ export default function Template(props: TemplateProps) {
- {realm.internationalizationEnabled && (assert(locale !== undefined), true) && locale.supported.length > 1 && ( + {realm.internationalizationEnabled && (assert(locale !== undefined), locale.supported.length > 1) && (
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */} - - {labelBySupportedLanguageTag[currentLanguageTag]} - - - + +
+
+
+ + <#if !(auth?has_content && auth.showUsername() && !auth.showResetCredentials())> + <#if displayRequiredFields> +
+
+ * ${msg("requiredFields")} +
+
+

<#nested "header">

+
+
+ <#else> +

<#nested "header">

+ + <#else> + <#if displayRequiredFields> +
+
+ * ${msg("requiredFields")} +
+
+ <#nested "show-username"> +
+ + + + +
+
+
+ <#else> + <#nested "show-username"> +
+ + + + +
+ + +
+
+
+ + <#-- App-initiated actions should not see warning messages about the need to complete the action --> + <#-- during login. --> + <#if displayMessage && message?has_content && (message.type != 'warning' || !isAppInitiatedAction??)> +
+
+ <#if message.type = 'success'> + <#if message.type = 'warning'> + <#if message.type = 'error'> + <#if message.type = 'info'> +
+ ${kcSanitize(message.summary)?no_esc} +
+ + + <#nested "form"> + + <#if auth?has_content && auth.showTryAnotherWayLink()> +
+ +
+ + + <#nested "socialProviders"> + + <#if displayInfo> +
+
+ <#nested "info"> +
+
+ +
+
+ +
+ + + +