前两篇介绍了应用国际化的注意事项和提示语国际化的简单实现。后来阅读spring源码发现,spring对国际化的支持已经很到位了,很多地方都不需要在程序中处理。比如语言切换的拦截器,spring已经自带了。本文介绍更简洁的基于spring的提示语国际化实现方案。
1、web.xml配置, web.xml中需要配置ContextLoaderListener和DispatcherServlet,这个就不多讲了。
<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等层使用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); } }
相关推荐
国际化版本应用,国际化语言切换及系统配置应用。SPRINGMVC国际化代码应用。
java web应用国际化与分页 java web应用国际化与分页 java web应用国际化与分页
【eoeAndroid特刊】第五期:资源与应用国际化布 (其余《eoeAndroid特刊》请查阅我的资源列表) 1.源和资产(Resources and Assets) 2.Android 中可用的资源类型 3.Resources and Internationalization(资源和国际化)...
对于某些应用系统而言,它需要发布到不同的国家地区,因此需要特殊的做法来支持,也即是国际化。通过国际化的方式,实现界面信息,各种提示信息等内容根据不同国家地区灵活展示的效果。比如在中国,系统以简体中文...
Qt应用程序国际化示例代码,包含静态加载和动态切换语言两种方式。
ios应用程序国际化,能实现语言切换,非常好用。
文档中介绍了Struts2中国际化的实现原理和使用方法,同时介绍了应用程序的国际化的实现思路和方法以及示例!
Struts 2 国际化应用
iOS国际化 应用内切换语言更新版
国际化与本地化的概念,Web应用的中文本地化,Struts框架对国际化的支持
设计Java EE应用程序-- J2EE应用程序国际化和本地化.doc
论人脸识别技术的国际化区域诉讼证据应用.pdf
Go 需要支持 i18n (国际化) 工具,但是现有的 Go 程序的国际化支持都比较冗长繁杂。i18n4go提供一个应用工作流进程 —— 从 Go 应用中提取字符串,合并他们到单个包(消除冗余),准备翻译,使底层代码容易接收他们...
android应用内和系统设置,两种设置多语言国际化。代码完整,包括切换简体,繁体中文和英文
Qt应用程序国际化工程示例
# 国际化(应用名称 文字 图片的国际化适应)附带参考链接在README.md中
本地化与国际化.zipIOS应用例子源码下载本地化与国际化.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
iOS应用程序国际化 博客文章:http://blog.csdn.net/z251257144/article/details/8288028
能够支持多个国家或者地区的本地化应用,国际化的应用
为全面提高华北科技学院本科教育教学质量,推动教育信息化和教育国际化的建设,提出了"一体两翼双引擎"的教学发展思路,"一体"即全面提高本科教育教学质量,推进教育现代化;"两翼"即信息化、国际化;"双引擎"即抓基层、强...