使用spring实现资源国际化
来源:互联网 发布:沧州管家婆软件总代理 编辑:程序博客网 时间:2024/05/19 00:36
有时候需要使用资源国际化来定义和处理一些字段。本文是一个简单的使用例子用于展示在基于java注解的配置中配置spring资源国际化的类并使用其获取prop文件中的属性。
1、创建资源文件
本文示例使用的资源文件为一个属性文件,默认的属性文件名称为i18n.properties,存放在工程的类路径下的i18n目录下。实际使用中可以针对需要添加针对不同语音的属性文件,如:
i18n_en_US.properties、i18n_zh_CN.properties等。本例中的默认属性配置文件中的内容如下:
munu.btn1.title=\u5B89\u9632menu.btn2.title=\u670D\u52A1menu.btn3.title=\u6211\u7684munu.btn3.sub1.title=\u4E3B\u9875
2、配置bean
本文使用给予java注解的配置。主要时创建一个org.springframework.context.support.ResourceBundleMessageSource的bean实例。源码如下:
package api.landsem.base.configuration;import org.springframework.context.MessageSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;import org.springframework.context.support.ResourceBundleMessageSource;import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration@EnableWebMvc @ComponentScan({"api.landsem.base.bean"})@ImportResource("classpath:applicationContext-*.xml")public class RootConfiguration {@Bean("messageSource")public MessageSource getResourceBundleMessageSourc() {ResourceBundleMessageSource resource = new ResourceBundleMessageSource();resource.setBasename("i18n/i18n");return resource;}}
3、读取资源属性文件
在java中使用创建的MessageSource bean来读取属性文件,如下为测试源码:
package api.landsem.base.test;import java.util.Locale;import org.apache.log4j.Logger;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.MessageSource;public class PropertiesTest extends BaseTest{private static final Logger logger = Logger.getLogger(PropertiesTest.class);@Autowiredprivate MessageSource mMessageSource;@Testpublic void test() {String msg = mMessageSource.getMessage("munu.btn1.title", null, Locale.getDefault());logger.info(msg);}}
1 0
- 使用spring实现资源国际化
- spring mvc国际化资源实现
- struts、spring国际化资源文件使用简介
- 使用Spring MVC 实现 国际化
- 使用资源文件实现java的国际化
- Spring 国际化资源
- java使用资源文件实现程序的国际化
- Spring实现国际化
- Spring国际化实现
- Spring国际化实现
- Spring国际化实现
- Spring boot 实现国际化
- 国际化资源文件的实现
- 简单实现SpringMVC资源国际化
- spring国际化的使用
- java国际化资源文件使用
- 使用Spring中的IoC功能来实现系统的国际化
- 使用spring boot 实现返回信息国际化记录
- 快速排序
- 简单解析的几种方式:
- 跟着中国最权威研究机构,学习制造业转型新思路
- tomcat多开、建立虚拟目录和虚拟站点
- IDEA第三章----idea常用配置
- 使用spring实现资源国际化
- 【考试技巧】输入输出优化
- 中小企业网站建设必须要说的那些事儿
- HTTPS 客户端验证 服务端证书流程
- 中企动力2017年产品专家晋级大赛
- 嵌入式个人收藏
- 图的邻接矩阵存储下的广度优先遍历
- OVER(PARTITION BY)函数介绍
- html5的canvas绘制迷宫地图