Nearby lessons
29 of 35Spring - Internationalization (I18N)
- Resolve and remember a user's locale across requests
- Let the user change language with a request parameter
- Move all display text into per-locale properties files and read it from a JSP
Serving the same application in more than one language. SessionLocaleResolver to remember the user's locale, LocaleChangeInterceptor to switch it from a link, and ResourceBundleMessageSource to pull every label out of a properties file.
Internationalization[I18N]
Internationalization is the process of designing Java application on the basis of Locality is called as Internationalization.
Java is providing very good Internationalization support in Java applications due to UNICODE representations and the predefined library like java.util.Locale and some of the predefined classes from java.text package.
To provide Internationalization in Spring WEB MVC applications , Spring has provided very good environment with the following two beans.
- SessionLocaleResolver
- LocaleChangeInterceptor
SessionLocaleResolver
SessionLocaleResolver resolves locales by inspecting a predefined attribute in a user‘s session. If the session attribute doesn‘t exist, this locale resolver determines the default locale from the accept-language HTTP header. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" /> </bean>
LocaleChangeInterceptor
LocaleChangeInterceptor interceptor detects if a special parameter is present in the current HTTP request. The parameter name can be customized with the paramName property of this interceptor. If such a parameter is present in the current request, this interceptor changes the user‘s locale according to the parameter value.
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" /> </bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors"> <list> <ref bean="localeChangeInterceptor" /> </list>
</property> </bean> To implement Internationalization in Spring WEB MVC application then we have to use the following steps.
- Prepare properties file for each and every local to which we want to support.
In Internationalization , we have to prepare a seperate properties file with the locale respective messages in the form of Key-Value pairs, where keys must be in English and the values must be in the respective language letters in the form of UNOCODE.
In general, the name format of the properties file is "baseName_lang.properties".
EX:
messages_en.properties
uname = User Name upwd = User Password
messages_it.properties
uname = Usere Nameo upwd = Usere Passwordo
Prepare User forms by getting messages from the properties file — registrationform.jsp
To get messages from properties files we have to use the following tag from Spring tag library with the URI "http://www.springframework.org/tags".
<spring:message code="--"/> Where code attribute will take key of the message defined in properties file.
Prepare User forms by getting messages from the properties file
- Configure "SessionLocaleResolver", "LocaleChangeInterceptor" and
"ResourceBundleMessageSource" in Spring Configuration File — ds-servlet.xml
Where the purpose of SessionLocaleResolver is to resolves locales by inspecting a predefined attribute in a user‘s session. If the session attribute doesn‘t exist, this locale resolver determines the default locale from the accept-language HTTP header.
Where the purpose of "LocaleChangeInterceptor" is to detects if a special parameter is present in the current HTTP request. The parameter name can be customized with the paramName property of this interceptor. If such a parameter is present in the current request, this interceptor changes the user‘s locale according to the parameter value.
Where the purpose of "ResourceBundleMessageSource" is to resolve the properties file on the basis of the provided "basename" property inorder to submit values to the User forms.
"ResourceBundleMessageSource" in Spring Configuration File — index.jsp
Example:
messages_en.properties
title1 = Durga Software Solutions title2 = User Registration Form uname = User Name upwd = User Password uemail = User Email Id umobile = User Mobile No
messages_it.properties
title1 = Durga Software Solutions title2 = Usere Registratione Formo uname = Usero Nameo upwd = Passwordo uemail = Usere Emailo umobile = Usero Mobileo
messages_hi.properties
title1 = Durga Software Solutions title2 = User Registration Form uname = Upayuktha Naam upwd = Upayuktha Rahsya Naam uemail = Upayuktha Vidyuth Naam umobile = Upayuktha Charavani Sakya
"ResourceBundleMessageSource" in Spring Configuration File — registrationform.jsp
"ResourceBundleMessageSource" in Spring Configuration File — registrationdetails.jsp
"ResourceBundleMessageSource" in Spring Configuration File — I18NController.java
"ResourceBundleMessageSource" in Spring Configuration File — User.java
"ResourceBundleMessageSource" in Spring Configuration File — ds-servlet.xml
"ResourceBundleMessageSource" in Spring Configuration File — web.xml
- The locale resolver decides the language; the interceptor is what lets the user change it
- ResourceBundleMessageSource keys the same message across one file per locale
- Once text lives in properties files, adding a language is a new file, not a code change