SpringMVC-ResourceBundleMessageSource使用

来源:互联网 发布:狄尼定理知乎 编辑:程序博客网 时间:2024/06/17 02:53

ResourceBundleMessageSource: 提供国际化的类。说的简单点,这个类的作用就是读取资源属性文件(.properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。

新建国际化资源文件
这里写图片描述

i18n_en_US.propertiesmessage=welcome:{0}i18n_zh_CN.propertiesmessage=欢迎:{0}

Bean的配置

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">    <property name="basename" value="i18n" /></bean>

使用:

@Test    public void test() {        ApplicationContext context = new ClassPathXmlApplicationContext("SpringMVC.xml");        System.out.println(context.getMessage("message", new Object[]{"lgh"}, Locale.SIMPLIFIED_CHINESE));        System.out.println(context.getMessage("message", new Object[]{"lgh"}, Locale.US));    }
@Controllerpublic class TestController {    @Autowired    private ResourceBundleMessageSource messageSource;    @RequestMapping(value = "/test4", method = RequestMethod.GET)    public String test4(Locale locale) {        System.out.println(messageSource.getMessage("message", null, locale));        return "i18n";    }}

ResourceBundleMessageSource 的底层实现依赖于java.util.ResourceBundle

参考:
http://lavasoft.blog.51cto.com/62575/184605/

原创粉丝点击