标签:
Spring的i18n配置:<!-- conf:i18n --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="fallbackToSystemLocale" value="false" /> <property name="useCodeAsDefaultMessage" value="true" /> <property name="defaultEncoding" value="UTF-8" /> <property name="cacheSeconds" value="60" /> <property name="basenames"> <list> <value>classpath:com/hupu/nac/i18n/message</value> </list> </property> </bean>
Spring的i18n使用:
package com.joyce.enums; import java.util.Locale; /** * 语言枚举 * @author Joyce.Luo * @date 2016-1-5 上午09:38:27 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public enum EnumLang { ZH_CN("zh-CN", Locale.SIMPLIFIED_CHINESE), ZH_TW("zh-TW", Locale.TRADITIONAL_CHINESE), EN("en", Locale.ENGLISH), JA("ja", Locale.JAPAN); private String lang; private Locale locale; public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public Locale getLocale() { return locale; } public void setLocale(Locale locale) { this.locale = locale; } private EnumLang(String lang, Locale locale) { this.lang = lang; this.locale = locale; } /** * 根据ID获取枚举值 * @param lang ID * @return ID对应的枚举值 * @author Joyce.Luo * @date 2015-5-8 上午11:02:45 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ public static Locale getLocale(String lang) { for (EnumLang enumLang : EnumLang.values()) { if (enumLang.getLang().equals(lang)) { return enumLang.getLocale(); } } return Locale.ENGLISH; } }
package com.joyce.util.i18n; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.springframework.context.MessageSource; import com.hupu.nac.enums.EnumLang; import com.hupu.nac.util.CookieUtil; import com.hupu.nac.util.SpringUtil; /** * Spring i18n * @author Joyce.Luo * @date 2016-1-5 上午09:26:03 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public class I18NUtil { private static final Logger logger = Logger.getLogger(I18NUtil.class); private static MessageSource messageSource = (MessageSource) SpringUtil.getApplicationContext().getBean("messageSource"); private static final Object[] objs = null; /** * 获取国际化信息 * @param key 对应键 * @param locale 语言 * @param objects 参数 * @return 国际化信息 * @author Joyce.Luo * @date 2016-1-5 上午10:19:00 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public static String getMessage(String key, Locale locale, Object...objects){ if (StringUtils.isEmpty(key)) { return StringUtils.EMPTY; } if (null == locale) { HttpServletRequest request = ServletActionContext.getRequest(); if (null == request) { locale = Locale.ENGLISH; } else { String lang = CookieUtil.getCookieByName(request, "language").getValue(); if (StringUtils.isEmpty(lang)){ locale = Locale.ENGLISH; } else { locale = EnumLang.getLocale(lang); } } } logger.info("Spring I18 ---> key:[" + key + "],lang:[" + locale.getLanguage() + "],params:[" + objects + "]"); return messageSource.getMessage(key, objects, locale); } /** * 获取国际化信息 * @param key 对应键 * @param locale 语言 * @return 国际化信息 * @author Joyce.Luo * @date 2016-1-5 上午10:19:00 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public static String getMessage(String key, Locale locale){ return getMessage(key, locale, objs); } /** * 获取国际化信息 * @param key 对应键 * @param objects 参数 * @return 国际化信息 * @author Joyce.Luo * @date 2016-1-5 上午10:19:00 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public static String getMessage(String key, Object...objects){ return getMessage(key, null, objects); } /** * 获取国际化信息 * @param key 对应键 * @param lang 语言 * @return 国际化信息 * @author Joyce.Luo * @date 2016-1-6 上午11:30:03 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public static String getMessage(String key, String lang){ return getMessage(key, EnumLang.getLocale(lang)); } /** * 获取国际化信息 * @param key 对应键 * @return 国际化信息 * @author Joyce.Luo * @date 2016-1-5 上午10:19:00 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2016 */ public static String getMessage(String key){ return getMessage(key, null, objs); } }
标签:
原文地址:http://blog.csdn.net/luo201227/article/details/50468243