Spring 3 MVC Internationalization
Spring 3 MVC Internationalization
9.
10. label.footer= www.dineshonjava.com
resources/messages_de.properties
view plainprint?
1. emp.label.id=Impelyee Id
2. emp.label.name=Impelyee Vorname
3. emp.label.age=Impelyee iage
4. emp.label.salary=shalery
5. emp.label.address=Adrrezz
6.
7. label.menu=Men
8. label.title=Impelyee Managemenot Sistom
9.
10. label.footer= www.dineshonjava.com
Step 2: Configure i18n & l10n to Spring3MVC
we need to declare these files in spring configuration file. We will use class
org.springframework.context.support.ReloadableResourceBundleMessageSource to define the
message resources. Also, note that we will provide a feature where user will be able to select
language for the application. This is implemented by using
org.springframework.web.servlet.i18n.LocaleChangeInterceptor class. The
LocaleChangeInterceptor class will intercept any changes in the locale. These changes are then
saved in cookies for future request.
org.springframework.web.servlet.i18n.CookieLocaleResolverclass will be used to store the
locale changes in cookies.
Add following code in the sdnext-servlet.xml file.
view plainprint?
1. <bean class="org.springframework.context.support.ReloadableResourceBundleMessageS
ource" id="messageSource">
2.
3.
4. </bean>
5.
6. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="local
eChangeInterceptor">
7.
8. </bean>
9.
10. <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeR
esolver">
11.
12. </bean>
13.
14. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle
rMapping" id="handlerMapping">
15.
16.
17.
<property name="interceptors">
<ref bean="localeChangeInterceptor"></ref>
</property>
18. </bean>
Note that in above configuration we have defined basename property in messageSource bean to
classpath:messages. By this, spring will identify that the message resource message_ will be
used in this application.
Change in the JSPs Template:
WebRoot/WEB-INF/views/header.jsp
view plainprint?
1. <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2.
<a href="?lang=en">en</a>
5.
6.
<a href="?lang=de">de</a>
7. </span>
WebContent/WEB-INF/views/menu.jsp
view plainprint?
1. <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2.
3. <spring:message code="emp.label.menu"></spring:message>