SPRINGMVC internationalization Settings-cannot change HTTP accept Header-use A different locale resolution strategy

Source: Internet
Author: User
Tags i18n locale

Using SpringMVC3 to configure internationalization, recently encountered "cannot change HTTP accept Header-use A different locale resolution strategy" Such an exception prompt, finally resolved. Now say about the SPRINGMVC configuration internationalization of the steps and points of attention, and finally combined with the spring source parsing principle.

The most common problems to be solved in internationalization

    1. The page can dynamically display text, values, time, etc. according to the language of the browser setting;
    2. Set locale information can be obtained from the bean;
    3. You can toggle locale information by dynamically linking it.

The following is a demo to illustrate the above three problems commonly used solutions.

Problem 1, the solution is as follows:

Configure the Resourcebundlemessagesource parser with the following configuration springmvc.xml:

<!--internationalization--><bean id= "Messagesource" class= " Org.springframework.context.support.ResourceBundleMessageSource "><property name=" basename "value=" i18n " ></property></bean>

Add i18n.properties configuration file, demo-like configuration of a Chinese, an English, such as:

i18n.properties Content:

I18n.username = \u7528\u6237\u540di18n.password = \u5bc6\u7801
i18n_en_us.properties content:
I18n.username = Usernamei18n.password = Passsword

The page shows the FMT tag with Jstl:

<fmt:message key= "I18n.username" ></fmt:message>

The above configuration can be implemented according to browser settings to show "User name" or "username" in English。


Issue 2, get locale settings information in the Bean in Java

This is relatively simple, injected into the bean resourcebundlemessagesource, can be obtained, the relevant Java code is as follows:


@Autowiredprivate Resourcebundlemessagesource messagesource;/** * 1. Verify that the internationalization configuration dynamically displays * 2 with FMT tags based on local language configuration dynamics. Verify that you get the information for the internationalized resource file Locale in the bean */@RequestMapping ("/i18n") public String testi18n (locale locale) {String user = Messagesource.getmessage ("I18n.username", null, locale); SYSTEM.OUT.PRINTLN ("internationalized resource file locale configuration (username):" +user); return "i18n";}

question 3, a simple description is to dynamically set the language information on the page, that is, to dynamically set the locale information to achieve the purpose of switching display , such as:


In the implementation of this problem encountered a header written in the error, The reason for the single-step source discovery is because localeresovler default injection is Org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver, we look at this class setlocale method will find out The cause of the anomaly.

public void SetLocale (HttpServletRequest request, httpservletresponse response, locale locale) {/*/*/     throw new Uns Upportedoperationexception ("Cannot change HTTP accept Header-use A different locale resolution strategy");/*    */
   
    }
   

Then we can inject org.springframework.web.servlet.i18n.SessionLocaleResolver this parser to solve, the parser is mainly to get the post-locale object in the session for subsequent acquisition.

The Springmvc.xml configuration is as follows:

<!--configuration Sessionlocaleresolver is used to store locale objects in session for subsequent use--><bean id= "Localeresolver" class= " Org.springframework.web.servlet.i18n.SessionLocaleResolver "></bean><!--configuration Localechangeinterceptor Used primarily to get locale information from the request, to convert the period to the locale image, get Localeresolver object--<mvc:interceptors> <bean class= "Org.springfram Ework.web.servlet.i18n.LocaleChangeInterceptor "></bean> </mvc:interceptors>

there is a particular need to note the Bean ID configuration, the value must be localeresolver otherwise, the error in the header will still be reported.

According to the above operation, you can not go to the browser manually set the language, you can directly through the "Chinese", "English" to switch, I believe some friends will encounter such needs.

The process of combining single-step source code is understood here with the following internal principles:




SPRINGMVC internationalization Settings-cannot change HTTP accept Header-use A different locale resolution strategy

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.