Spring MVC internationalization settings

Source: Internet
Author: User
Tags i18n

I. browser-based internationalization Configuration
Use Spring MVC and configure the Resource file.
Xml Code
 
<! -- Resource file binder -->
<Bean id = "messageSource" class = "org. springframework. context. support. ResourceBundleMessageSource">
<Property name = "basename" value = "message-info"/>
<Property name = "useCodeAsDefaultMessage" value = "true"/>
</Bean>
 
<! -- Resource file binder --> <bean id = "messageSource" class = "org. springframework. context. support. resourceBundleMessageSource "> <property name =" basename "value =" message-info "/> <property name =" useCodeAsDefaultMessage "value =" true "/> </bean>
Here, message-info is the common name of your properties file. For example: My configuration file is called message-info.properties, message-info_zh_CN.properties and so on, as long as there is this configuration, and then configure the JSP Renderer for JSTL support, then you can use the fmt tag in your JSP file to internationalize your browser language.
For example: <fmt: message key = "info. login. title"/>
The info. login. title corresponds to your resource file.
Another method is to use the labels provided by spring to display international information, such:
<Spring: message code = "main. title"/> <br>
<Input type = "button" value = "<spring: message code =" main. title "/>"/> <br>
Ii. Dynamic Loading-based internationalization Configuration
1. Request-based internationalization Configuration
Request-based international configuration means that the international configuration takes effect in the current request. Otherwise, the browser is automatically used.
The configuration method is as follows:
First configure the interceptor
<! -- The interceptor must be configured for international operations. It can be used in other international ways. -->
<Bean id = "localeChangeInterceptor" class = "org. springframework. web. servlet. i18n. LocaleChangeInterceptor"/>
In this configuration, whether the request level is internationalized, the Cookie level is internationalized, or the Session level is internationalized, this interceptor must be configured, otherwise it will not be available.
After the interceptor is configured, inject the interceptor into your UrlHandlerMapping. For example:
Xml Code
 
<Bean id = "defaultUrlMapping" class = "org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping">
<Property name = "interceptors" ref = "localeChangeInterceptor"/>
<Property name = "order">
<Value> 1 </value>
</Property>
</Bean>
 
<Bean id = "defaultUrlMapping" class = "org. springframework. web. servlet. handler. beanNameUrlHandlerMapping "> <property name =" interceptors "ref =" localeChangeInterceptor "/> <property name =" order "> <value> 1 </value> </property> </bean>
At this time, whenever there is a request that meets the UrlMapping, the request will be intercepted and the international parameters will be configured.
<Bean id = "localeResolver" class = "org. springframework. web. servlet. i18n. AcceptHeaderLocaleResolver"> </bean>
The default parameter name is locale. It contains your submission parameters. For example, en_US, zh_CN, and so on. At this time, you can add <a href = "? Locale = zh_CN "> Simplified Chinese </a>
If your resources are full of recommended Chinese configurations, it will become a simplified Chinese pull.
2. Session-based internationalization Configuration
The Interceptor is the same as the request-based one.
The Session configuration is as follows:
<Bean id = "localeResolver" class = "org. springframework. web. servlet. i18n. SessionLocaleResolver"> </bean>
In the Controller you process, generate the submitted locale field information to a real Locale object, and save the object in the Session. The default saved ID is SessionLocaleResolver. LOCALE_SESSION_ATTRIBUTE_NAME.
In this way, when your Session does not expire, the language type remains correct. I have been using it like this all the time. I think it is better to use the Session, and I am very satisfied with it.
3. Cookie-based international configuration
I will not talk about this. I will not use much of it anyway. At least I will not use cookies for my projects. Therefore, I will not elaborate on the Cookie-based internationalization configuration, if you want to know how to configure it, download a Spring. The example program is configured with cookies, and you can read the code on your own.
Iii. Notes
If you do not use the default browser language internationalization method, you must configure the Interceptor. If you have multiple UrlMapping, each of them must be configured with an interceptor.
As for the name of the configured LocaleResolver, you must use the name localeResolver in the configuration above. Of course, this is the default name. You can set it to something else, but it is troublesome, anyway, I feel good when I use the default one.
Solution:
A few days ago, I referenced "Spring MVC I18N-configuration related to internationalization" and tested it. I found a problem. The program runs and throws an exception.
"Cannot change HTTP accept header-use a different locale resolution strategy", the root cause is that spring source has made restrictions. The source code is as follows:
Java code
 
Public class AcceptHeaderLocaleResolver implements LocaleResolver {
Public Locale resolveLocale (HttpServletRequest request ){
Return request. getLocale ();
}
Public void setLocale (HttpServletRequest request, HttpServletResponse response, Locale locale ){
Throw new UnsupportedOperationException (
"Cannot change HTTP accept header-use a different locale resolution strategy ");
}
}
 
Public class AcceptHeaderLocaleResolver implements LocaleResolver {public Locale resolveLocale (HttpServletRequest request) {return request. getLocale ();} public void setLocale (HttpServletRequest request, HttpServletResponse response, Locale locale) {throw new UnsupportedOperationException ("Cannot change HTTP accept header-use a different locale resolution strategy ");}}
Note that the above class allows inheritance, so you need to rewrite the setLocale method. The source code example is as follows:
Java code
 
Package org. springframework. web. servlet. i18n;
Import java. util. Locale;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import org. springframework. web. servlet. DispatcherServlet;
Import org. springframework. web. servlet. LocaleResolver;
Public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {
Private Locale myLocal;
Public Locale resolveLocale (HttpServletRequest request ){
Return myLocal;
}
Public void setLocale (HttpServletRequest request, HttpServletResponse response, Locale locale ){
MyLocal = locale;
}

}
 
Package org. springframework. web. servlet. i18n; import java. util. locale; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. dispatcherServlet; import org. springframework. web. servlet. localeResolver; public class extends {private Locale myLocal; public Locale resolveLocale (HttpServletRequest request) {return myLocal;} public void setLocale (HttpServletRequest request, HttpServletResponse response, Locale) {myLocal = locale ;}}
And then in the action-servlet.xml set
Xml Code
 
<Bean id = "messageSource" class = "org. springframework. context. support. ResourceBundleMessageSource">
<Property name = "basename" value = "message"/>
</Bean>
<Bean id = "myViewController" class = "org. springframework. web. servlet. mvc. UrlFilenameViewController"/>
<Bean id = "filenameController" class = "org. springframework. web. servlet. mvc. UrlFilenameViewController"/>
<Bean id = "urlMapping" class = "org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">
<Property name = "interceptors" ref = "localeChangeInterceptor"/>
<Property name = "mappings">
<Value>
Chinese. do = filenameController
Us. do = filenameController
</Value>
</Property>
</Bean>
<Bean id = "defaultHandlerMapping" class = "org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping"/>
<Bean id = "localeChangeInterceptor" class = "org. springframework. web. servlet. i18n. LocaleChangeInterceptor"/>
<Bean id = "localeResolver" class = "org. springframework. web. servlet. i18n. MyAcceptHeaderLocaleResolver"/>
<Bean id = "viewResolver"
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass"
Value = "org. springframework. web. servlet. view. JstlView"/>
<Property name = "prefix" value = "/WEB-INF/jsp/"/>
<Property name = "suffix" value = ". jsp"/>
</Bean>

 

This article comes from "Wings your dreams"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.