Always set lang attribute in login templates (#35361)

Closes #35103

Signed-off-by: Andreas Blaettlinger <bln1imb@bosch.com>
This commit is contained in:
Andreas Blättlinger 2024-12-04 19:24:42 +01:00 committed by GitHub
parent 6f5077ddcb
commit a1f5234571
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 8 deletions

View File

@ -140,6 +140,8 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
protected UserModel user;
protected String lang;
protected final Map<String, Object> attributes = new HashMap<>();
private Function<Map<String, Object>, Map<String, Object>> attributeMapper;
@ -150,6 +152,7 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
this.realm = session.getContext().getRealm();
this.client = session.getContext().getClient();
this.uriInfo = session.getContext().getUri();
this.lang = Locale.ENGLISH.toLanguageTag();
}
@SuppressWarnings("unchecked")
@ -544,7 +547,10 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
b.queryParam(Constants.KEY, authenticationSession.getAuthNote(Constants.KEY));
}
attributes.put("locale", new LocaleBean(realm, locale, b, messagesBundle));
final var localeBean = new LocaleBean(realm, locale, b, messagesBundle);
attributes.put("locale", localeBean);
lang = localeBean.getCurrentLanguageTag();
}
if (Profile.isFeatureEnabled(Feature.ORGANIZATION)) {
@ -564,6 +570,8 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
&& !Boolean.TRUE.toString().equals(authenticationSession.getClientNote(Constants.KC_ACTION_ENFORCED))) {
attributes.put("isAppInitiatedAction", true);
}
attributes.put("lang", lang);
}
/**
@ -601,8 +609,6 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
return createResponse(LoginFormsPages.LOGIN_USERNAME);
}
;
public Response createLoginPassword() {
return createResponse(LoginFormsPages.LOGIN_PASSWORD);
}

View File

@ -184,12 +184,15 @@ public class KeycloakErrorHandler implements ExceptionMapper<Throwable> {
Map<String, Object> attributes = new HashMap<>();
Properties messagesBundle = theme.getMessages(locale);
final var localeBean = new LocaleBean(realm, locale, session.getContext().getUri().getRequestUriBuilder(), messagesBundle);
final var lang = realm.isInternationalizationEnabled() ? localeBean.getCurrentLanguageTag() : Locale.ENGLISH.toLanguageTag();
attributes.put("statusCode", responseStatus.getStatusCode());
attributes.put("realm", realm);
attributes.put("url", new UrlBean(realm, theme, session.getContext().getUri().getBaseUri(), null));
attributes.put("locale", new LocaleBean(realm, locale, session.getContext().getUri().getRequestUriBuilder(), messagesBundle));
attributes.put("locale", localeBean);
attributes.put("lang", lang);
String errorKey = responseStatus == Response.Status.NOT_FOUND ? Messages.PAGE_NOT_FOUND : Messages.INTERNAL_SERVER_ERROR;
String errorMessage = messagesBundle.getProperty(errorKey);

View File

@ -24,6 +24,7 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.OAuth2Constants;
@ -83,6 +84,11 @@ public class LoginPageTest extends AbstractI18NTest {
@Rule
public AssertEvents events = new AssertEvents(this);
@Before
public void before() {
setRealmInternationalization(true);
}
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
testRealm.addIdentityProvider(IdentityProviderBuilder.create()
@ -135,7 +141,7 @@ public class LoginPageTest extends AbstractI18NTest {
}
@Test
public void htmlLangAttribute() {
public void htmlLangAttributeWithInternationalizationEnabled() {
loginPage.open();
assertEquals("en", loginPage.getHtmlLanguage());
@ -144,6 +150,14 @@ public class LoginPageTest extends AbstractI18NTest {
assertEquals("de", loginPage.getHtmlLanguage());
}
@Test
public void htmlLangAttributeWithInternationalizationDisabled() {
setRealmInternationalization(false);
loginPage.open();
assertEquals("en", loginPage.getHtmlLanguage());
}
@Test
public void acceptLanguageHeader() throws IOException {
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
@ -381,4 +395,11 @@ public class LoginPageTest extends AbstractI18NTest {
assertThat(pageSource, containsString(expectedEnglishMessage));
assertThat(pageSource, not(containsString(expectedGermanMessage)));
}
private void setRealmInternationalization(final boolean enabled) {
final var realmResource = testRealm();
RealmRepresentation realm = realmResource.toRepresentation();
realm.setInternationalizationEnabled(enabled);
realmResource.update(realm);
}
}

View File

@ -1,7 +1,7 @@
<#import "footer.ftl" as loginFooter>
<#macro registrationLayout bodyClass="" displayInfo=false displayMessage=true displayRequiredFields=false>
<!DOCTYPE html>
<html class="${properties.kcHtmlClass!}"<#if realm.internationalizationEnabled> lang="${locale.currentLanguageTag}" dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
<html class="${properties.kcHtmlClass!}" lang="${lang}"<#if realm.internationalizationEnabled> dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
<head>
<meta charset="utf-8">

View File

@ -23,7 +23,7 @@
<#macro registrationLayout bodyClass="" displayInfo=false displayMessage=true displayRequiredFields=false>
<!DOCTYPE html>
<html class="${properties.kcHtmlClass!}"<#if realm.internationalizationEnabled> lang="${locale.currentLanguageTag}" dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
<html class="${properties.kcHtmlClass!}" lang="${lang}"<#if realm.internationalizationEnabled> dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
<head>
<meta charset="utf-8">