For a web app to be able to used across languages/regions without much of major changes they must support Internationalization and Localization. In this post we will see example of Spring MVC Internationalization(i18n) and Localization(L10n).
Internationalization is a way of designing software’s so that they can be adapted in various languages and regions without much of changes.
Localization is a way of using the Internationalized software’s for a specific language/region using a specific locale.
What is i18n and L10n?
i18n for Internationalization – 18 stands for the number of letters between the first i and the last n in the word internationalization.
L10n for Localization – 10 stands for the length of the words.
Example : Spring MVC Internationalization and Localization
1. Dispatcher Servlet Configurations
a) localeResolver – is used to store the locale changes. We can either use the org.springframework.web.servlet.i18n.CookieLocaleResolver or org.springframework.web.servlet.i18n.SessionLocaleResolver for this purpose. In our example we will use the CookieLocaleResolver
b) localeChangeInterceptor – is used to intercept any changes in the locale using the parameter configured. You can choose the name of the param
1 2 3 4 |
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang" /> </bean> |
c) messageSource – is configured to load the messages.
Complete dispatcher servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="defaultLocale" value="en" /> <property name="cookieName" value="kscodesCookie" /> <property name="cookieMaxAge" value="3600" /> </bean> <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> </beans> |
2. Message per Locale
The message files for a particluar locale are decided using a pattern.
example – if the messageSource is configured to “messages” and the localeChangeInterceptor uses a lang=fr, then the app will try to locate a message source property of name messages_fr.properties.
we have set the defaultLocale to “en”, so the name of the property file for lang=en can be either messages.properties or messages_en.properties.
We will use en and fr for our example.
messages_en.properties
1 2 3 4 5 |
employee.form.title=Employee Details Form employee.form.firstName=First Name employee.form.lastName=Last Name employee.form.departmentName=Department Name employee.form.submit=Submit |
messages_fr.properties
1 2 3 4 5 |
employee.form.title=Formulaire Détails des employés employee.form.firstName=Prénom employee.form.lastName=Nom de famille employee.form.departmentName=Nom du département employee.form.submit=Soumettre |
3. Changes in JSP
In JSP instead of using the hard coded values we need to use spring tags to display any text.
example instead of using “First Name” we now will use the spring tag and message code from the properties file –
<spring:message code="employee.form.firstName"/>
Complete JSP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <html> <head> </head> <body> <h2><spring:message code="employee.form.title"/></h2> <form:form method="post" action="addEmployee" commandName="employee"> <table> <tr> <td><spring:message code="employee.form.firstName"/></td> <td><input type="text" name="firstName"/></td> </tr> <tr> <td><spring:message code="employee.form.lastName"/></td> <td><input type="text" name="lastName"/></td> </tr> <tr> <td><spring:message code="employee.form.departmentName"/></td> <td><input type="text" name="department"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="<spring:message code="employee.form.submit"/>"/></td> </tr> </table> </form:form> </body> </html> |
Output
Using lang=en – http://localhost:8080/sampleproject/employeeForm?lang=en
Using lang=fr – http://localhost:8080/sampleproject/employeeForm?lang=fr