i18n (Internationalization)
When developing web applications, it's always good practice to enable i18n in the early stage. The main
work is to externalize the user interface text and messages into properties files.
Even though you won't have i18n requirements on day one, it's good to externalize the language-
related settings so that it will be easier later when you need to support more languages.
With Spring MVC, enabling i18n is very simple. First, externalize the language-related user interface
settings into various properties files within the /WEB-INF/i18n folder, as described in Table 17-3. Because
we will support both English (US) and Chinese (HK), we will need four files. The application.properties
and message.properties files store the settings for the default locale, which in our case is English (US). In
addition, the application_zh_HK.properties message_zh_HK.properties file stores the settings in the
Chinese (HK) language.
Configure i18n in DispatcherServlet Configuration
Having the language settings in place, the next step is to configure the DispatcherServlet's
WebApplicationContext for i18n support.
Listing 17-13 shows the revised configuration file with i18n support (servlet-context.xml).
Listing 17-13. Revised Servlet Context Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<annotation-driven />
<resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
<default-servlet-handler/>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jspx" />
</beans:bean>
<context:component-scan
base-package="com.apress.prospring3.ch17.web.controller" />
<interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang"/>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home