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 bean
org.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.
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 theaccept-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. - a
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.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
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/
- Spring MVC: Internationalization & localization
- Spring MVC Internationalization (i18n) and Localization (i10n) Example
- Spring MVC Internationalization
- Spring MVC internationalization example
- Internationalization and Localization
- Internationalization and localization testing
- iOS10 Internationalization and Localization
- Internationalization and localization 记忆线索
- 关于iOS App 国际化和本地化 的一些总结:国际化 vs 本地化(Internationalization vs Localization)
- ECMAScript 6 学习系列课程 (ES6 Internationalization & Localization的介绍)
- Localization
- Internationalization testing
- JasperReports - Internationalization
- i18n(Internationalization)
- spring mvc
- spring mvc
- spring mvc
- spring,MVC
- windows bug
- [知其然不知其所以然-33] How to set up mutt
- 国内一线互联网公司内部面试题库
- 【Android】Android Studio配置ndk开发环境
- linux 基本操作
- Spring MVC: Internationalization & localization
- spark 集群搭建 详细步骤
- Toolbar的背景虚化加listView头部布局置顶
- createStatement()的用法
- yaml-cpp安装
- SVM
- 关于MEMSET的初始最大最小值
- Javascript刷新页面
- warSourceExcludes和packagingExcludes参数的区别