Spring国际化 Spring(3)
废话少说,直接上代码
package com.application.i18n; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import org.springframework.context.MessageSource; public class I18N { private static Map<String , Locale> locales; private static final String DEFAULT_LOCALE = "zh_CN";//System.getProperty("user.language", "zh")+"_"+System.getProperty("user.country","CN"); private MessageSource messageSource ; static{ //注册支持的语言环境 locales = new HashMap<String , Locale>(); locales.put("zh_CN",Locale.CHINA); locales.put("zh_TW",Locale.TAIWAN); } /** × 私有构造函数 */ private I18N(MessageSource messageSource){ this.messageSource = messageSource; } /** * 从国际化消息文件中根据key获取value,然后用参数填充value里面的占位符 * @param code * message key * @param locale * 语言国家代码zh_CN,zh_TW(etc..),如果为null,采用用户默认的语言国家代码获取消息文件 * @param arguments * 占位符填充参数,没有占位符,传null * @return */ public String getMessage(String code, String locale , Object[] arguments) { // if(null == locale){ // ConnectionUser connectedUser = ApplicationAdapter.getConnectionUser(null); // if(connectedUser != null){ // locale = connectedUser.getSessionValue("locale").toString(); // }else{ locale = DEFAULT_LOCALE; // } // } return messageSource.getMessage(code, arguments , locales.get(locale)); } /** * 使用指定locale,从国际化消息文件中根据key获取value * @param locale * 语言国家代码zh_CN,zh_TW(etc..),如果为null,采用用户默认的语言国家代码获取消息文件 * @param code * message key * @return */ public String getMessage(String locale,String code) { return getMessage(code, locale , null); } /** * 使用用户默认语言国家代码,从国际化消息文件中根据key获取value * @param code * message key * @return */ public String getMessage(String code) { return getMessage(code,null,null); } /** * 根据消息key数组获取消息值列表 * @param codeList * @return */ public List<String> getMessageList(String... codeList){ if(null != codeList){ List<String> messageList = new ArrayList<String>(); for(String code : codeList){ messageList.add(getMessage(code)); } return messageList; } return null; } /** * 使用用户默认语言国家代码,从国际化消息文件中根据key获取value * @param code * message key * @param arguments * 占位符填充参数,没有占位符,传null * @return */ public String getMessage(String code, Object[] arguments) { return getMessage(code, null, arguments); } /** * 注册locale对象,才能支持对应的语言环境 * @param locale */ public void registerLocale(Locale locale){ locales.put(locale.toString(), locale); } }
spring配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <import resource="config/applicationDataSource.xml"/> <!-- <import resource="config/memcached.xml"/> --> <!-- 国际化 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" singleton="true"> <property name="basenames"> <list> <value>message</value> </list> </property> <property name="useCodeAsDefaultMessage" value="true"/> </bean> <bean id="I18N" class="com.application.i18n.I18N" singleton="true"> <constructor-arg index="0" ref="messageSource"></constructor-arg> </bean> </beans>