使用Spring-MessageSource实现国际化-实操版

一、首先在spring启动xml文件中添加:

    <property name="basenames">

            <value>prop.msg</value>

    <property name="defaultEncoding" value="UTF-8"/>

</bean>

messageSource的使用请自行查看API 。

二、创建多语言版本配置文件

我这里的demo有两种语言的本地化文件:

msg_en_US.properties

zk001=hello,i am ok.

msg_zh_cn.properties

zk001=是我

三、启动测试

public static void main(String[] args) {

try {

ClassPathXmlApplicationContext classPathXmlApplicationContext =new ClassPathXmlApplicationContext("spring/spring-config.xml");

        MessageSource messageSource = classPathXmlApplicationContext.getBean("messageSource", MessageSource.class);

        String message = messageSource.getMessage("zk001", new String[0], Locale.US);

        String message1 = messageSource.getMessage("zk001", new String[0], Locale.getDefault());

        System.out.println("英文 = " + message);

        System.out.println("中文 = " + message1);

    }catch (Exception e) {

logger.error("start failed .", e);

    }

}

四、运行结果

英文 = hello,i am ok.

中文 = 是我

这里奉上 ResourceBundleMessageSource Diagram 有兴趣者可自行研究 。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容