Internationalization Overview
To enable Web projects to support internationalization, you need to identify each user's preferred region and display content based on that region. In a spring MVC application, the user's region is identified by a zone parser, which must implement the Localeresolver interface.
Spring MVC provides several localeresolver implementations where we can parse the region according to different requirements. If the parser provided by SPRINGMVC does not meet the requirements, we can implement the Localeresolver interface and create a custom zone parser.
To define a zone resolver, you simply register a localeresolver type Bean in the context of the Web application. The bean name of the zone parser must be set to Localeresolver so that Dispatcherservlet can detect it automatically. Note that only one zone resolver can be registered per Dispatcherservlet.
The default zone parser used by spring is acceptheaderlocaleresolver. It parses the zone by verifying the Accept-language property of the HTTP request header.
example of the default zone resolver Acceptheaderlocaleresolver
The following two examples show the effect of the default zone resolver Acceptheaderlocaleresolver:
Example one: You can localize text (not content), time, and value on the page based on the browser language settings.
To achieve these functions, use the JSTL fmt tag. (If the request does not go through the controller, jump directly to the JSP page)
Create an internationalized resource file
I18n_zh_cn.properties:
i18n.username=\u7528\u6237\u540d
i18n.password=\u5bc6\u7801
Note: \u7528\u6237\u540d is "user name", \u5bc6\u7801 "password"
I18n_en_us.properties:
I18n.username=username
I18n.password=password
Configuring internationalized Resource Files
Spring configuration file:
<!--Configure internationalized resource Files--
<bean id= "Messagesource"
class= " Org.springframework.context.support.ResourceBundleMessageSource ">
<property name=" basename "value=" i18n "></property>
</bean>
<!--Configure a direct jump to JSP page---
<mvc:view-controller path=" /i18n "view-name=" i18n "/>
<mvc:view-controller path="/i18n2 "view-name=" i18n2 "/>
page
I18N.JSP:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<% @taglib prefix=" FMT "uri=" http://java.sun.com/jsp/jstl/fmt "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
I18N2.JSP:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<% @taglib prefix=" FMT "uri=" http://java.sun.com/jsp/jstl/fmt "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Operation Result:
When the browser language is set to ZH-CN, i18n.jsp displays "user name", i18n2.jsp display "password";
When you set the browser language to en-us, i18n.jsp displays "username" and i18n2.jsp displays "password".
Example Two: Get the internationalized resource file in the bean that corresponds to the Locale information.
To do this, inject an instance of Resourcebundlemessagesource into the bean, using its corresponding GetMessage method: (Create an internationalized resource file, configure an internationalized resource file with example one, but make/i18n map to the controller, Rather than direct jump)
page
INDEX.JSP:
<a href= "i18n" >i18n page</a>
Controller:
@Autowired
private Resourcebundlemessagesource messagesource;
@RequestMapping ("/i18n") public
String testi18n (locale locale) {
string val = Messagesource.getmessage (" I18n.username ", null, locale);
System.out.println (val);
return "i18n";
}
Operation Result:
When setting the browser language to ZH-CN, click the hyperlink in index.jsp, the console output "user name";
When setting the browser language to EN-us, click the hyperlink in index.jsp and the console output "username". using Sessionlocaleresolver to switch locales via hyperlinks
Example Three: Now we want to be able to switch locales with hyperlinks instead of relying on the language settings of the browser
The implementation method is to configure Sessionlocalresolver and Localechangeinterceptor. (Create, configure an internationalized resource file with the example above)
configuring Sessionlocaleresolver and Localechangeinterceptor
Spring configuration file:
<mvc:interceptors>
<!--Configuring the Localechanceinterceptor Interceptor
function: Get name=locale Request Parameters--
<bean class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor" ></bean>
</MVC: Interceptors>
<!--Configure the Sessionlocalresolver
effect: Set the Locale object to the session's properties; Get the locale object from the session--
<bean id= "localeresolver"
class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver" > </bean>
page:
<a href= "I18N?LOCALE=ZH_CN" >Chinese</a>
<br><br>
<a href= "I18n?locale=en_us" >English</a>
Controller: (same Example II)
@Autowired
private Resourcebundlemessagesource messagesource;
@RequestMapping ("/i18n") public
String testi18n (locale locale) {
string val = Messagesource.getmessage (" I18n.username ", null, locale);
System.out.println (val);
return "i18n";
}
Operation Result:
When you click on the Chinese of the page, the console outputs "user name";
When you click on the page's 中文版, the console outputs "username".
How the Sessionlocaleresolver&localechangeinterceptor works
By looking at the source code, I learned that the workflow for implementing the example three functions with Sessionlocaleresolver and Localechangeinterceptor is:
1. Before the target method executes, The Prehandle method of the Localechangeinterceptor interceptor is called first, which gets the locale property in the request parameter, if the locale is not empty, Gets to the current zone resolver (at this point, Sessionlocaleresolver), and then calls the SetLocale method of the Sessionlocaleresolver object, which generates the locale object based on the locale value Passed as a parameter to the method.
2. In the SetLocale method of the Sessionlocaleresolver object, the locale object is set to the session.
3. Dispatchservlet Gets the locale object in the session through the Sessionlocaleresolver Zone parser, passes it to the target method, and then uses the locale object in the target method.