Skip to content

Commit

Permalink
Always set lang attribute in login templates (keycloak#35361)
Browse files Browse the repository at this point in the history
Closes keycloak#35103

Signed-off-by: Andreas Blaettlinger <[email protected]>
  • Loading branch information
andreas-blaettlinger authored Dec 4, 2024
1 parent 6f5077d commit a1f5234
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -150,6 +152,7 @@ public FreeMarkerLoginFormsProvider(KeycloakSession session) {
this.realm = session.getContext().getRealm();
this.client = session.getContext().getClient();
this.uriInfo = session.getContext().getUri();
this.lang = Locale.ENGLISH.toLanguageTag();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -544,7 +547,10 @@ protected void createCommonAttributes(Theme theme, Locale locale, Properties mes
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)) {
Expand All @@ -564,6 +570,8 @@ protected void createCommonAttributes(Theme theme, Locale locale, Properties mes
&& !Boolean.TRUE.toString().equals(authenticationSession.getClientNote(Constants.KC_ACTION_ENFORCED))) {
attributes.put("isAppInitiatedAction", true);
}

attributes.put("lang", lang);
}

/**
Expand Down Expand Up @@ -601,8 +609,6 @@ public Response createLoginUsername() {
return createResponse(LoginFormsPages.LOGIN_USERNAME);
}

;

public Response createLoginPassword() {
return createResponse(LoginFormsPages.LOGIN_PASSWORD);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,15 @@ private static Map<String, Object> initAttributes(KeycloakSession session, Realm
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -135,7 +141,7 @@ public void uiLocalesParameter() {
}

@Test
public void htmlLangAttribute() {
public void htmlLangAttributeWithInternationalizationEnabled() {
loginPage.open();
assertEquals("en", loginPage.getHtmlLanguage());

Expand All @@ -144,6 +150,14 @@ public void htmlLangAttribute() {
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()) {
Expand Down Expand Up @@ -381,4 +395,11 @@ private void switchLanguageToGermanAndBack(String expectedEnglishMessage, String
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);
}
}
2 changes: 1 addition & 1 deletion themes/src/main/resources/theme/base/login/template.ftl
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit a1f5234

Please sign in to comment.