java使用i18n实现国际化

概述:国际化也称作i18n,其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数。由于软件发行可能面向多个国家,对于不同国家的用户,软件显示不同语言的过程就是国际化。通常来讲,软件中的国际化是通过配置文件来实现的,假设要支撑两种语言,那么就需要两个版本的配置文件。

Java国际化

(1)Java自身是支持国际化的,java.util.Locale用于指定当前用户所属的语言环境等信息,java.util.ResourceBundle用于查找绑定对应的资源文件。Locale包含了language信息和country信息,Locale创建默认locale对象时使用的静态方法:

    /**
     * 只能在创建区域设置时调用此方法*
     * 由于制作快捷方式而导致的常量。
     */
    private static Locale createConstant(String lang, String country) {
        BaseLocale base = BaseLocale.createInstance(lang, country);
        return getInstance(base, null);
    }

配置文件命名规则: basename_language_country.properties 必须遵循以上的命名规则,java才会识别。其中,basename是必须的,语言和国家是可选的。这里存在一个优先级概念,如果同时提供了messages.properties和messages_zh_CN.propertes两个配置文件,如果提供的locale符合en_CN,那么优先查找messages_en_CN.propertes配置文件,如果没查找到,再查找messages.properties配置文件。最后,提示下,所有的配置文件必须放在classpath中,一般放在resources目录下


演示案例:

1、在resource目录下创建两个配置文件:messages_zh_CN.propertes和messages_en_GB.propertes

在zh_CN中写:test=ZH test

在en_GB中写:test=GB test

 最后进行测试:

    @Test
    public void test() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("messages",
                new Locale("en", "GB"));
        System.out.println(resourceBundle.getString("test"));

        System.out.println(ResourceBundle.getBundle("messages",
                new Locale("zh", "CN")).getString("test"));
    }

MessageSource接口

spring中国际化是通过MessageSource这个接口来支持的

常见实现类

ResourceBundleMessageSource

这个是基于Java的ResourceBundle基础类实现,允许仅通过资源名加载国际化资源

ReloadableResourceBundleMessageSource

这个功能和第一个类的功能类似,多了定时刷新功能,允许在不重启系统的情况下,更新资源的信息。

StaticMessageSource

它允许通过编程的方式提供国际化信息,一会我们可以通过这个来实现db中存储国际化信息的功能。

场景一:动态参数

创建 cj_en_US.properties 和 cj_zh_CN.properties

en_US:www.cj.com=welcome {0},时间:{1}

zh_CN:www.cj.com=欢迎 {0},时间:{1}

创建spring配置文件,配置MessageSource

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>cj</value>
            </list>
        </property>
        <property name="defaultEncoding">
            <value>utf-8</value>
        </property>
    </bean>

</beans>

最后测试:

@Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        // 传递动态参数,使用数组形式对应{0} {1}顺序
        Object[] objs = new Object[]{"cj", new Date().toString()};

        // www.cj.com为资源文件的key值,
        // objs为资源文件value值所需要的参数,Local.CHINA为国际化为语言
        String str1 = context.getMessage("www.cj.com", objs, Locale.CHINA);
        String str2 = context.getMessage("www.cj.com", objs, Locale.US);
        System.out.println(str1);
        System.out.println(str2);

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值