Spring MVC: Internationalization & localization

来源:互联网 发布:java random 1到100 编辑:程序博客网 时间:2024/05/19 14:39

The post presents how this is configured under the hood with the help of Spring  MVC. In Spring MVC application, comes with few “LocaleResolver” to support the internationalization or multiple languages features. In this tutorial, it shows a simple welcome page, display the message from properties file, and change the locale based on the selected language link.

1. Project Folder


2. Application Context Configuration

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.npf.controller"/><mvc:annotation-driven /><mvc:resources location="/resources/" mapping="/resources/**"/><bean 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><bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  <property name="basename" value="classpath:messages/messages" />  <property name="defaultEncoding" value="UTF-8"/></bean><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"><property name="defaultLocale" value="en" /></bean><mvc:interceptors>   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">      <property name="paramName" value="language"/>   </bean></mvc:interceptors></beans>
Normally in the Java world, the locale-specific data is stored in message resource files. In Spring you configure it by adding the following beanorg.springframework.context.support.ReloadableResourceBundleMessageSource to the application context:

The configuration specifies that the message resource files should be named messages_xx.properties (xx is the shortcut of the locale), are stored in themessages folder in the classpath, and that the default encoding for the files is UTF-8.

Message resource files in classpath

Note: If the message resources files change often and you don’t want to restart the JVM, you should useorg.springframework.context.support.ReloadableResourceBundleMessageSource

Spring’s DispatcherServlet enables you to automatically resolve messages using the client’s locale. This is done with LocaleResolver objects. You can select between

  • an AcceptHeaderLocaleResolver, which inspects the accept-language header in the request that was sent by the client (e.g., a web browser). Usually this header field contains the locale of the client’s operating system.
  • CookieLocaleResolver,
  • and a SessionLocaleResolver, which allows you to retrieve locales from the session that might be associated with the user’s reques

LocaleChangeInterceptor

The LocaleResolver is normally used in combination with the LocaleChangeInterceptor, which allows you to change of the current locale by using a defined parameter in the request (in this case thelanguage parameter)
<mvc:interceptors>   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">      <property name="paramName" value="language"/>   </bean></mvc:interceptors>

JSP

A JSP page, contains two hyperlinks to change the locale manually, and use the spring:message to display the message from the corresponds properties file by checking the current user’s locale.
3. Controller
package com.npf.controller;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.npf.model.Employee;@Controllerpublic class WelcomeController {@RequestMapping("/welcome")public ModelAndView welcome() throws Exception {ModelAndView model = new ModelAndView("WelcomePage");Employee e1 = new Employee();e1.setId("1");e1.setFirstName("steven");e1.setLastName("zhou");List<Employee> employees = new ArrayList<Employee>();employees.add(e1);model.addObject("employees", employees);return model;}}
4. Test
Access it via : http://localhost:8080/springmvcInternationalization/welcome



click Chinese link:


source code in Github: springmvcInternationalization


ref link :

1.  http://www.codingpedia.org/ama/spring-3-mvc-internationalization-localization-of-podcastpedia-org/

2. http://www.journaldev.com/2610/spring-mvc-internationalization-i18n-and-localization-l10n-example

3. https://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/




0 0
原创粉丝点击