Skip to content

Commit 55eb1ba

Browse files
Signed-off-by: Andreas Blaettlinger <bln1imb@bosch.com>
1 parent c429c9a commit 55eb1ba

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

services/src/main/java/org/keycloak/forms/login/freemarker/FreeMarkerLoginFormsProvider.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider {
140140

141141
protected UserModel user;
142142

143+
protected String lang;
144+
143145
protected final Map<String, Object> attributes = new HashMap<>();
144146
private Function<Map<String, Object>, Map<String, Object>> attributeMapper;
145147

@@ -150,6 +152,7 @@ public FreeMarkerLoginFormsProvider(KeycloakSession session) {
150152
this.realm = session.getContext().getRealm();
151153
this.client = session.getContext().getClient();
152154
this.uriInfo = session.getContext().getUri();
155+
this.lang = "en";
153156
}
154157

155158
@SuppressWarnings("unchecked")
@@ -544,7 +547,10 @@ protected void createCommonAttributes(Theme theme, Locale locale, Properties mes
544547
b.queryParam(Constants.KEY, authenticationSession.getAuthNote(Constants.KEY));
545548
}
546549

547-
attributes.put("locale", new LocaleBean(realm, locale, b, messagesBundle));
550+
final var localeBean = new LocaleBean(realm, locale, b, messagesBundle);
551+
attributes.put("locale", localeBean);
552+
553+
lang = localeBean.getCurrentLanguageTag();
548554
}
549555

550556
if (Profile.isFeatureEnabled(Feature.ORGANIZATION)) {
@@ -564,6 +570,8 @@ protected void createCommonAttributes(Theme theme, Locale locale, Properties mes
564570
&& !Boolean.TRUE.toString().equals(authenticationSession.getClientNote(Constants.KC_ACTION_ENFORCED))) {
565571
attributes.put("isAppInitiatedAction", true);
566572
}
573+
574+
attributes.put("lang", lang);
567575
}
568576

569577
/**
@@ -601,8 +609,6 @@ public Response createLoginUsername() {
601609
return createResponse(LoginFormsPages.LOGIN_USERNAME);
602610
}
603611

604-
;
605-
606612
public Response createLoginPassword() {
607613
return createResponse(LoginFormsPages.LOGIN_PASSWORD);
608614
}

testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/i18n/LoginPageTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
2525
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
2626
import org.junit.Assert;
27+
import org.junit.Before;
2728
import org.junit.Rule;
2829
import org.junit.Test;
2930
import org.keycloak.OAuth2Constants;
@@ -83,6 +84,11 @@ public class LoginPageTest extends AbstractI18NTest {
8384
@Rule
8485
public AssertEvents events = new AssertEvents(this);
8586

87+
@Before
88+
public void before() {
89+
setRealmInternationalization(true);
90+
}
91+
8692
@Override
8793
public void configureTestRealm(RealmRepresentation testRealm) {
8894
testRealm.addIdentityProvider(IdentityProviderBuilder.create()
@@ -135,7 +141,7 @@ public void uiLocalesParameter() {
135141
}
136142

137143
@Test
138-
public void htmlLangAttribute() {
144+
public void htmlLangAttributeWithInternationalizationEnabled() {
139145
loginPage.open();
140146
assertEquals("en", loginPage.getHtmlLanguage());
141147

@@ -144,6 +150,14 @@ public void htmlLangAttribute() {
144150
assertEquals("de", loginPage.getHtmlLanguage());
145151
}
146152

153+
@Test
154+
public void htmlLangAttributeWithInternationalizationDisabled() {
155+
setRealmInternationalization(false);
156+
157+
loginPage.open();
158+
assertEquals("en", loginPage.getHtmlLanguage());
159+
}
160+
147161
@Test
148162
public void acceptLanguageHeader() throws IOException {
149163
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
@@ -381,4 +395,11 @@ private void switchLanguageToGermanAndBack(String expectedEnglishMessage, String
381395
assertThat(pageSource, containsString(expectedEnglishMessage));
382396
assertThat(pageSource, not(containsString(expectedGermanMessage)));
383397
}
398+
399+
private void setRealmInternationalization(final boolean enabled) {
400+
final var realmResource = testRealm();
401+
RealmRepresentation realm = realmResource.toRepresentation();
402+
realm.setInternationalizationEnabled(enabled);
403+
realmResource.update(realm);
404+
}
384405
}

themes/src/main/resources/theme/base/login/template.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<#import "footer.ftl" as loginFooter>
22
<#macro registrationLayout bodyClass="" displayInfo=false displayMessage=true displayRequiredFields=false>
33
<!DOCTYPE html>
4-
<html class="${properties.kcHtmlClass!}"<#if realm.internationalizationEnabled> lang="${locale.currentLanguageTag}" dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
4+
<html class="${properties.kcHtmlClass!}" lang="${lang}"<#if realm.internationalizationEnabled> dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
55

66
<head>
77
<meta charset="utf-8">

themes/src/main/resources/theme/keycloak.v2/login/template.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<#macro registrationLayout bodyClass="" displayInfo=false displayMessage=true displayRequiredFields=false>
2525
<!DOCTYPE html>
26-
<html class="${properties.kcHtmlClass!}"<#if realm.internationalizationEnabled> lang="${locale.currentLanguageTag}" dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
26+
<html class="${properties.kcHtmlClass!}" lang="${lang}"<#if realm.internationalizationEnabled> dir="${(locale.rtl)?then('rtl','ltr')}"</#if>>
2727

2828
<head>
2929
<meta charset="utf-8">

0 commit comments

Comments
 (0)