(B/S与C/S)实现国际化配置
来源:互联网 发布:雅乐简谱打谱软件 编辑:程序博客网 时间:2024/04/29 21:15
根据项目的业务需求:实现软件的一个语言切换.涉及到一个国际化语言的配置问题,所以在网上各方查找资料,个人觉得此篇文章关于springmvc实现国际化配置讲的不错,可以去看看http://blog.csdn.net/u010251897/article/details/52411166
关于国际化配置,大家是需要先了解下下面两个基础知识
国际化编程常用类:使用Java语言进行国际化编程,经常要用到Locale类及ResourceBundl e类,它们都在包java.util中.
1.Locale类
Locale类包含对主要地理区域的地域化特征的封装。通过设定Locale,我们可以为特定的国家或地区提供符合当地文化习惯的语言、表达格式等。一个Locale代码 可由语言代码和地区代码组合而成。常用的两种Locale代码如下:
语言代码 地区代码 Locale代码 说明
en US en_US 美国英语
zh CN zh_CN 简体中文
2.ResoureeBundle类
ResourceBundle类是一个抽象类,需要通过getBundle(String baseName,Locale locale)方法加载扩展名为properties的资源文件。资源文件的名称由一个基本名称和Locale代码组成,而基本名称则可以在Java程序 中调用getBundle(StringbaseName,Locale locale)方法时传人参数指定。例如,我们在程序中指定基本名称为messages,Locale代码为zh_CN, 则对应的资源文件应为messages_zh_CN.properties
本人所做的项目因为涉及到两个模块,所以配置是分开做的; 下面给大家分享下
关于B/S模块的国际化配置
/** * utils of i18n * * @author * */public class MessageResource{ /** * the base name of resource bundle */ private static final String BASE_NAME = "tyris.util.i18n.messages"; /** * resource bundles */ private static final ResourceBundle RESOURCES_CN = ResourceBundle.getBundle(BASE_NAME, Locale.SIMPLIFIED_CHINESE); private static final ResourceBundle RESOURCES_US = ResourceBundle.getBundle(BASE_NAME, Locale.US); /** * get corresponding resource from resource bundle * @param key * @param locale * @return */ public static String getMessage(String key, Locale locale){ if (Locale.SIMPLIFIED_CHINESE == locale) return RESOURCES_CN.getString(key); else if (Locale.US == locale) return RESOURCES_US.getString(key); else return key; }}
根据语言风格通过getMessage方法来获取对应的key
messages_en_US.properties
messages_zh_CN.properties
关于C/S模块的国际化配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!--spring 组件扫描配置 --> <context:component-scan base-package="controllers"/> <!-- 配置国际化资源文件路径 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> <property name="useCodeAsDefaultMessage" value="true" /> </bean> <!-- 基于Cookie的本地化解析器 --> <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieMaxAge" value="604800"/> <property name="defaultLocale" value="zh_CN"/> <property name="defaultLocale" value="en_US"/> <property name="cookieName" value="Language"></property> </bean> --> <!-- 基于Session的本地化解析器 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> <!-- 基于请求 --> <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/> --> <!-- 获取本地 --> <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> <mvc:interceptors> <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="locale" /> </bean> </mvc:interceptors> </beans>
控制器关键代码
modelMap.addAttribute("language", ConfigUtils.getValue("language"));
页面关键代码
action="patient_add.do?locale=${language }<spring:message code="name"/>
至此软件的国际化语言实现完成,另外我在实现过程中遇到的一个问题也跟大家提一下,就是web项目中配置的一个xsd版本号的冲突问题 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
- (B/S与C/S)实现国际化配置
- c/s与b/s
- c/s与b/s
- C/S与B/S
- C/S与B/S
- C/S 与 B/S
- C/S 与 B/S
- B/S 与C/S
- C/S与B/S
- C/S与B/S
- c/s与b/s
- C/S与B/S
- C/S与B/S
- C/S与B/S
- C/S与B/S
- C/S与B/S
- c/s与b/s
- C/S与B/S
- Minimum Depth of Binary Tree
- Pjax 小试一把! 参数以及事件说明
- 毕业了
- system("color5E");
- Java线程学习经典例子-读写者演示
- (B/S与C/S)实现国际化配置
- Ubuntu下Android Studio运行中的坑
- vim go
- php生成excel表格
- word转htm,excel转htm, doc转pdf
- 微信小程序传递参数(字符串、数组、对象)
- 能量泛函和变分法
- 2、Kafka设计原理
- WCF手动搭建 HelloWorld