From 764960a4cad246282b606decdb339e8d19cbfb90 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Mon, 23 Feb 2015 11:29:45 +0100 Subject: [PATCH] added locale support to ClassLoaderTheme and FolderTheme --- .../freemarker/FreeMarkerAccountProvider.java | 7 +- .../freemarker/ExtendingThemeManager.java | 13 +- .../java/org/keycloak/freemarker/Theme.java | 3 +- .../org/keycloak/theme/ClassLoaderTheme.java | 19 ++- .../java/org/keycloak/theme/FolderTheme.java | 13 +- .../base/messages/messages_de.properties | 118 ++++++++++++++++++ .../base/messages/messages_en.properties | 118 ++++++++++++++++++ .../freemarker/FreeMarkerEmailProvider.java | 7 +- .../FreeMarkerLoginFormsProvider.java | 7 +- 9 files changed, 273 insertions(+), 32 deletions(-) create mode 100755 forms/common-themes/src/main/resources/theme/login/base/messages/messages_de.properties create mode 100755 forms/common-themes/src/main/resources/theme/login/base/messages/messages_en.properties diff --git a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java index 368b4591e37..0c9718b38bd 100755 --- a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java +++ b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java @@ -31,10 +31,7 @@ import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import java.io.IOException; import java.net.URI; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; /** * @author Stian Thorgersen @@ -96,7 +93,7 @@ public class FreeMarkerAccountProvider implements AccountProvider { Properties messages; try { - messages = theme.getMessages(); + messages = theme.getMessages(Locale.GERMAN); attributes.put("rb", messages); } catch (IOException e) { logger.warn("Failed to load messages", e); diff --git a/forms/common-freemarker/src/main/java/org/keycloak/freemarker/ExtendingThemeManager.java b/forms/common-freemarker/src/main/java/org/keycloak/freemarker/ExtendingThemeManager.java index 533b63120d9..1fefce1921b 100644 --- a/forms/common-freemarker/src/main/java/org/keycloak/freemarker/ExtendingThemeManager.java +++ b/forms/common-freemarker/src/main/java/org/keycloak/freemarker/ExtendingThemeManager.java @@ -7,14 +7,7 @@ import org.keycloak.models.KeycloakSession; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.ListIterator; -import java.util.Properties; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** @@ -228,11 +221,11 @@ public class ExtendingThemeManager implements ThemeProvider { } @Override - public Properties getMessages() throws IOException { + public Properties getMessages(Locale locale) throws IOException { Properties messages = new Properties(); ListIterator itr = themes.listIterator(themes.size()); while (itr.hasPrevious()) { - Properties m = itr.previous().getMessages(); + Properties m = itr.previous().getMessages(locale); if (m != null) { messages.putAll(m); } diff --git a/forms/common-freemarker/src/main/java/org/keycloak/freemarker/Theme.java b/forms/common-freemarker/src/main/java/org/keycloak/freemarker/Theme.java index dff92ee861c..6a12a49f00d 100644 --- a/forms/common-freemarker/src/main/java/org/keycloak/freemarker/Theme.java +++ b/forms/common-freemarker/src/main/java/org/keycloak/freemarker/Theme.java @@ -3,6 +3,7 @@ package org.keycloak.freemarker; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Locale; import java.util.Properties; /** @@ -28,7 +29,7 @@ public interface Theme { public InputStream getResourceAsStream(String path) throws IOException; - public Properties getMessages() throws IOException; + public Properties getMessages(Locale locale) throws IOException; public Properties getProperties() throws IOException; diff --git a/forms/common-themes/src/main/java/org/keycloak/theme/ClassLoaderTheme.java b/forms/common-themes/src/main/java/org/keycloak/theme/ClassLoaderTheme.java index 1a793eee7c6..3ad511c1315 100755 --- a/forms/common-themes/src/main/java/org/keycloak/theme/ClassLoaderTheme.java +++ b/forms/common-themes/src/main/java/org/keycloak/theme/ClassLoaderTheme.java @@ -2,10 +2,13 @@ package org.keycloak.theme; import org.keycloak.freemarker.Theme; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Locale; import java.util.Properties; +import java.util.ResourceBundle; /** * @author Stian Thorgersen @@ -26,7 +29,7 @@ public class ClassLoaderTheme implements Theme { private String resourceRoot; - private String messages; + private String messageRoot; private Properties properties; @@ -43,7 +46,7 @@ public class ClassLoaderTheme implements Theme { this.templateRoot = themeRoot; this.resourceRoot = themeRoot + "resources/"; - this.messages = themeRoot + "messages/messages.properties"; + this.messageRoot = themeRoot + "messages/"; this.properties = new Properties(); URL p = classLoader.getResource(themeRoot + "theme.properties"); @@ -102,9 +105,17 @@ public class ClassLoaderTheme implements Theme { } @Override - public Properties getMessages() throws IOException { + public Properties getMessages(Locale locale) throws IOException { Properties m = new Properties(); - URL url = classLoader.getResource(this.messages); + + String message = null; + if(locale != null){ + message = this.messageRoot + "messages_" + locale.toString() + ".properties"; + }else{ + message = this.messageRoot + "messages.properties"; + } + + URL url = classLoader.getResource(message); if (url != null) { m.load(url.openStream()); } diff --git a/forms/common-themes/src/main/java/org/keycloak/theme/FolderTheme.java b/forms/common-themes/src/main/java/org/keycloak/theme/FolderTheme.java index aa2312e7072..dbba9d9dc98 100644 --- a/forms/common-themes/src/main/java/org/keycloak/theme/FolderTheme.java +++ b/forms/common-themes/src/main/java/org/keycloak/theme/FolderTheme.java @@ -7,6 +7,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Locale; import java.util.Properties; /** @@ -81,9 +82,17 @@ public class FolderTheme implements Theme { } @Override - public Properties getMessages() throws IOException { + public Properties getMessages(Locale locale) throws IOException { Properties m = new Properties(); - File file = new File(themeDir, "messages" + File.separator + "messages.properties"); + + String message = null; + if(locale != null){ + message = "messages_" + locale.toString() + ".properties"; + }else{ + message = "messages.properties"; + } + + File file = new File(themeDir, "messages" + File.separator + message); if (file.isFile()) { m.load(new FileInputStream(file)); } diff --git a/forms/common-themes/src/main/resources/theme/login/base/messages/messages_de.properties b/forms/common-themes/src/main/resources/theme/login/base/messages/messages_de.properties new file mode 100755 index 00000000000..69fea6b0106 --- /dev/null +++ b/forms/common-themes/src/main/resources/theme/login/base/messages/messages_de.properties @@ -0,0 +1,118 @@ +logIn=Anmelden +logInTo=Anmelden bei +logInWith=Anmelden mit +noAccount=Neuer Benutzer? +register=Anmelden +registerWith=Anmelden mit +allRequired=Alle Felder sind erforderlich +alreadyHaveAccount=Haben Sie bereits ein Konto? + +poweredByKeycloak=Powered by Keycloak + +username=Benutzername +usernameOrEmail=Benutzername oder E-Mail +fullName=Voller Name +firstName=Vorname +lastName=Nachname +email=E-Mail +password=Passwort +rememberMe=Angemeldet bleiben +passwordConfirm=Passwort Bestätigen +passwordNew=Neues Passwort +passwordNewConfirm=Neues Passwort bestätigen +cancel=Abbrechen +accept=Akzeptieren +submit=Absenden +yes=Ja +no=Nein + +authenticatorCode=Einmaliger Code +clientCertificate=Client Zertifikat + +invalidUser=Ungültiger Benutzername oder Passwort. +invalidPassword=Ungültiger Benutzername oder Passwort. +invalidEmail=Invalid email address +accountDisabled=Account is disabled, contact admin +accountTemporarilyDisabled=Account is temporarily disabled, contact admin or try again later +expiredCode=Login timeout. Please login again + +missingFirstName=Please specify first name +missingLastName=Please specify last name +missingEmail=Please specify email +missingUsername=Please specify username +missingPassword=Please specify password. +notMatchPassword=Passwords don't match +missingTotp=Please specify authenticator code + +invalidPasswordExisting=Invalid existing password +invalidPasswordConfirm=Password confirmation doesn't match +invalidTotp=Invalid authenticator code + +successTotp=Mobile authenticator configured. +successTotpRemoved=Mobile authenticator removed. + +usernameExists=Username already exists +emailExists=Email already exists + +federatedIdentityEmailExists=User with email already exists. Please login to account management to link the account. +federatedIdentityUsernameExists=User with username already exists. Please login to account management to link the account. +federatedIdentityDisabledRegistration=Registration of new users is not allowed. Please ask admin to register you and login to account management to link the account. + +loginTitle=Log in to +loginOauthTitle=Temporary access. +loginOauthTitleHtml=Temporary access requested. Login to grant access. +loginForgot=Forgot + +loginTotpTitle=Mobile Authenticator Setup +loginTotpStep1=Install FreeOTP or Google Authenticator on your mobile +loginTotpStep2=Open the application and scan the barcode or enter the key +loginTotpStep3=Enter the one-time code provided by the application and click Submit to finish the setup +loginTotpOneTime=One-time code + +loginProfileTitle=Update Account Information +loginProfileWarning=Your account is not enabled because you need to update your account information. +loginProfileWarningFollow=Please follow the steps below. +loginProfileError=Some required fields are empty or incorrect. +loginProfileErrorSteps=Please correct the fields in red. + +oauthGrantTitle=OAuth Grant +oauthGrantTitleHtml=Temporary access requested +oauthGrantTerms=Keycloak Central Login and Google will use this information in accordance with their respective terms of service and privacy policies. +oauthGrantRequest=Do you grant these access privileges? +oauthGrantLoginRequest=Do you grant access? + +emailVerifyTitle=Email verification +emailVerifyInstr=An email with instructions to verify your email address has been sent to you. +emailVerifyInstrQ=Haven't received a verification code in your email? +emailVerifyClick=Click here +emailVerifyResend=to re-send the email. + +error=A system error has occured, contact admin +errorTitle=We're sorry... +errorTitleHtml=We're sorry ... +errorGenericMsg=Something happened and we could not process your request. +actionWarningHeader=Your account is not enabled. +actionTotpWarning=You need to set up Mobile Authenticator to activate your account. +actionProfileWarning=You need to update your user profile to activate your account. +actionPasswordWarning=You need to change your password to activate your account. +actionEmailWarning=You need to verify your email address to activate your account. +actionFollow=Please fill in the fields below. + +errorKerberosLogin=Kerberos ticket not available. Authenticate with password. + +successHeader=Success! +errorHeader=Error! + +# Forgot password part + +emailForgotHeader=Forgot Your Password? +backToLogin=« Back to Login +emailUpdateHeader=Update password +emailSent=You should receive an email shortly with further instructions. +emailSendError=Failed to send email, please try again later +emailError=Invalid email. +emailErrorInfo=Please, fill in the fields again. +emailInstruction=Enter your username or email address and we will send you instructions on how to create a new password. + +accountUpdated=Your account has been updated +accountPasswordUpdated=Your password has been updated \ No newline at end of file diff --git a/forms/common-themes/src/main/resources/theme/login/base/messages/messages_en.properties b/forms/common-themes/src/main/resources/theme/login/base/messages/messages_en.properties new file mode 100755 index 00000000000..5c5d1f7e928 --- /dev/null +++ b/forms/common-themes/src/main/resources/theme/login/base/messages/messages_en.properties @@ -0,0 +1,118 @@ +logIn=Log in +logInTo=Log in to +logInWith=Log in with +noAccount=New user? +register=Register +registerWith=Register with +allRequired=All fields are required +alreadyHaveAccount=Already have an account? + +poweredByKeycloak=Powered by Keycloak + +username=Username +usernameOrEmail=Username or email +fullName=Full name +firstName=First name +lastName=Last name +email=Email +password=Password +rememberMe=Remember me +passwordConfirm=Confirm password +passwordNew=New Password +passwordNewConfirm=New Password confirmation +cancel=Cancel +accept=Accept +submit=Submit +yes=Yes +no=No + +authenticatorCode=One-time code +clientCertificate=Client Certificate + +invalidUser=Invalid username or password. +invalidPassword=Invalid username or password. +invalidEmail=Invalid email address +accountDisabled=Account is disabled, contact admin +accountTemporarilyDisabled=Account is temporarily disabled, contact admin or try again later +expiredCode=Login timeout. Please login again + +missingFirstName=Please specify first name +missingLastName=Please specify last name +missingEmail=Please specify email +missingUsername=Please specify username +missingPassword=Please specify password. +notMatchPassword=Passwords don't match +missingTotp=Please specify authenticator code + +invalidPasswordExisting=Invalid existing password +invalidPasswordConfirm=Password confirmation doesn't match +invalidTotp=Invalid authenticator code + +successTotp=Mobile authenticator configured. +successTotpRemoved=Mobile authenticator removed. + +usernameExists=Username already exists +emailExists=Email already exists + +federatedIdentityEmailExists=User with email already exists. Please login to account management to link the account. +federatedIdentityUsernameExists=User with username already exists. Please login to account management to link the account. +federatedIdentityDisabledRegistration=Registration of new users is not allowed. Please ask admin to register you and login to account management to link the account. + +loginTitle=Log in to +loginOauthTitle=Temporary access. +loginOauthTitleHtml=Temporary access requested. Login to grant access. +loginForgot=Forgot + +loginTotpTitle=Mobile Authenticator Setup +loginTotpStep1=Install FreeOTP or Google Authenticator on your mobile +loginTotpStep2=Open the application and scan the barcode or enter the key +loginTotpStep3=Enter the one-time code provided by the application and click Submit to finish the setup +loginTotpOneTime=One-time code + +loginProfileTitle=Update Account Information +loginProfileWarning=Your account is not enabled because you need to update your account information. +loginProfileWarningFollow=Please follow the steps below. +loginProfileError=Some required fields are empty or incorrect. +loginProfileErrorSteps=Please correct the fields in red. + +oauthGrantTitle=OAuth Grant +oauthGrantTitleHtml=Temporary access requested +oauthGrantTerms=Keycloak Central Login and Google will use this information in accordance with their respective terms of service and privacy policies. +oauthGrantRequest=Do you grant these access privileges? +oauthGrantLoginRequest=Do you grant access? + +emailVerifyTitle=Email verification +emailVerifyInstr=An email with instructions to verify your email address has been sent to you. +emailVerifyInstrQ=Haven't received a verification code in your email? +emailVerifyClick=Click here +emailVerifyResend=to re-send the email. + +error=A system error has occured, contact admin +errorTitle=We're sorry... +errorTitleHtml=We're sorry ... +errorGenericMsg=Something happened and we could not process your request. +actionWarningHeader=Your account is not enabled. +actionTotpWarning=You need to set up Mobile Authenticator to activate your account. +actionProfileWarning=You need to update your user profile to activate your account. +actionPasswordWarning=You need to change your password to activate your account. +actionEmailWarning=You need to verify your email address to activate your account. +actionFollow=Please fill in the fields below. + +errorKerberosLogin=Kerberos ticket not available. Authenticate with password. + +successHeader=Success! +errorHeader=Error! + +# Forgot password part + +emailForgotHeader=Forgot Your Password? +backToLogin=« Back to Login +emailUpdateHeader=Update password +emailSent=You should receive an email shortly with further instructions. +emailSendError=Failed to send email, please try again later +emailError=Invalid email. +emailErrorInfo=Please, fill in the fields again. +emailInstruction=Enter your username or email address and we will send you instructions on how to create a new password. + +accountUpdated=Your account has been updated +accountPasswordUpdated=Your password has been updated \ No newline at end of file diff --git a/forms/email-freemarker/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailProvider.java b/forms/email-freemarker/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailProvider.java index df43d16fb24..2526adbd9a2 100755 --- a/forms/email-freemarker/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailProvider.java +++ b/forms/email-freemarker/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailProvider.java @@ -17,10 +17,7 @@ import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; +import java.util.*; /** * @author Stian Thorgersen @@ -82,7 +79,7 @@ public class FreeMarkerEmailProvider implements EmailProvider { ThemeProvider themeProvider = session.getProvider(ThemeProvider.class, "extending"); Theme theme = themeProvider.getTheme(realm.getEmailTheme(), Theme.Type.EMAIL); - String subject = theme.getMessages().getProperty(subjectKey); + String subject = theme.getMessages(Locale.GERMAN).getProperty(subjectKey); String body = freeMarker.processTemplate(attributes, template, theme); send(subject, body); diff --git a/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/FreeMarkerLoginFormsProvider.java b/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/FreeMarkerLoginFormsProvider.java index 44a7585004d..d97ac0d8155 100755 --- a/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/FreeMarkerLoginFormsProvider.java +++ b/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/FreeMarkerLoginFormsProvider.java @@ -38,10 +38,7 @@ import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import java.io.IOException; import java.net.URI; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.concurrent.TimeUnit; /** @@ -173,7 +170,7 @@ public class FreeMarkerLoginFormsProvider implements LoginFormsProvider { Properties messages; try { - messages = theme.getMessages(); + messages = theme.getMessages(Locale.GERMAN); attributes.put("rb", messages); } catch (IOException e) { logger.warn("Failed to load messages", e);