Java Internationalization (i18n)

Source: Internet
Author: User
Tags i18n locale

Java Internationalization (i18n)

Recently in the function of doing a website internationalization. Use Java for development, using spring+velocity.

Java provides support for i18n, which is integrated by spring and can be easily configured. The main idea is to read the different configuration files according to the language to display the corresponding text. The main steps are as follows:

1. Use the two properties file to save the "symbol" to the corresponding language mapping. such as Messages_en.properties and messages_zh.properties, put them under the classpath of the project.

#messages_en. Propertiestitle=service Introduction

#messages_cn. Properties

title=\u670d\u52a1\u8bf4\u660e

Note Chinese to use Unicode encoding

2. Configure the spring XML file:

Spring offers a variety of international support options, such as browser-based, session, and cookies. How to use cookies in your project

<bean id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.CookieLocaleResolver" >       < Property Name= "Cookiemaxage" value= "604800"/>       <property name= "Defaultlocale" value= "Zh_cn"/>       < Property Name= "CookieName" value= "lang"/>     </bean>

resource files in Declaration 1

<bean id= "Messagesource" class= "Org.springframework.context.support.ResourceBundleMessageSource" >        < Property Name= "basename" value= "Messages"/>        <property name= "Usecodeasdefaultmessage" value= "true"/>    </bean>

Use interceptors to process requests to internationalize the corresponding pages

<bean id= "Localechangeinterceptor" class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> ... <property name= "interceptors" ref= "Localechangeinterceptor" ></property>
...

Or

<mvc:interceptors><bean class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>< Mvc:interceptor>...</mvc:interceptor></mvc:interceptors>

 

You can then call the placeholder on the VM page.

#springMessage ("title")

3. You can also write a controller to switch languages

@RequestMapping ("/lang") public String Lang (    throws   Exception {     = Request.getparameter ("Langtype");     if ("en". Equals (Langtype)) {        Cookielocaleresolver.setlocale (request, Response, Locale.english);     Else {        Cookielocaleresolver.setlocale (request, response, Locale.china);    }     returnnull;}

  

Java Internationalization (i18n)

Related Article

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.