前两篇介绍了应用国际化的注意事项和提示语国际化的简单实现。后来阅读spring源码发现,spring对国际化的支持已经很到位了,很多地方都不需要在程序中处理。比如语言切换的拦截器,spring已经自带了。本文介绍更简洁的基于spring的提示语国际化实现方案。
?
1、web.xml配置,?web.xml中需要配置ContextLoaderListener和DispatcherServlet,这个就不多讲了。
class="java"> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/config/web/web-main.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
?
2、springmvc配置文件中添加i18n支持和语言切换拦截器
<!-- 配置基于Session的locale处理,即将locale放在session中。该配置不是必须的,但是提供的语言不支持时,会遍历系统支持的语言,返回第一个支持的语言环境信息。 其他还有基于cookie、acceptHeader以及固定语言的设置--> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="zh"/> </bean> <!-- 国际化的消息资源文件 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:i18n/message</value> </list> </property> <property name="useCodeAsDefaultMessage" value="false"/> <property name="defaultEncoding" value="UTF-8"/> <property name="cacheSeconds" value="60"/> </bean> <mvc:interceptors> <!-- 切换语言拦截器 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"></property> </bean> </mvc:interceptors>
?
?3、MessageSourceUtils工具类调整
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
/**
* 资源国际化工具类
* @author lh@erongdu.com
* @version 2.0
* @since 2018-12-24
*/
@Component
public class MessageSourceUtils {
// 日志
private static final Logger LOGGER = LoggerFactory.getLogger(MessageSourceUtils.class);
private static MessageSource messageSource;
@Autowired
public void setMessageSource(MessageSource messageSource) {
MessageSourceUtils.messageSource = messageSource;
}
// /**
// * 获取国际化文件中国际化字段信息
// * @param key 国际化参数
// * @param args 动态参数
// * @return
// */
// public static String getMessage(String key, Object[] args) {
// try {
// return messageSource.getMessage(key, args, key, LangUtil.getLocale());
// } catch (Exception e) {
// LOGGER.error(e.getMessage());
// }
// return key;
// }
/**
* 可供后台使用
* 获取国际化文件中中文字段信息
* @param key 国际化参数
* @param args 动态参数
* @return
*/
public static String getChineseMessage(String key, Object[] args) {
try {
return messageSource.getMessage(key, args, key, Locale.CHINESE);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return key;
}
/**
* 获取国际化文件中国际化字段信息
* @param key 国际化key
* @param args 动态参数
* @return
*/
public static String getMessage(String key, Object[] args) {
try {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(key, args, key, locale);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return key;
}
}
?
4、在controller、Service、Interceptor等层使用monospace;">MessageSourceUtils
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* WEB界面 配置action
* @author lh@erongdu.com
* @date 2017年1月20日
*
*/
@Controller
@Scope("prototype")
public class IndexController {
@RequestMapping(value = "/index.htm")
@ResponseBody
public String index(Model model){
return MessageSourceUtils.getMessage("title", null);
}
}
??