国际化(i18n)

国际化(i18n)

概述

i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在信息技术领域,国际化与本地化(英文:internationalization and localization)是指修改软件使之能适应目标市场的语言、地区差异以及技术需要。

java国际化

java自身支持国际化

java.util.Local用于指定当前用户所属的语言环境等信息,Local包含了language信息和country信息。
java.util.ReourceBundle用于查找绑定对应的资源文件。

配置文件命名规则

basenaem_language_country.properties
必须遵循命名规则,java才会识别

basename是必须的,语言和国家是可选的。
如果同时提供了context.propertiescontext_zh_CN.propertes两个配置文件,提供的locale符合en_CN,那么优先查找messages_en_CN.propertes配置文件,如果没查找到,再查找messages.properties配置文件。
所有的配置文件必须放在classpath中,一般放在resources目录下。

实例

在这里插入图片描述

测试
@Test
public void testCN() {
    ResourceBundle bundle = ResourceBundle.getBundle("local", new Locale("zh", "CN"));
    String value = bundle.getString("localName");
    System.out.println("value = " + value);
    /*value = China*/
}
@Test
public void testOther() {
    //"local"就是配置文件的basename
    ResourceBundle bundle1 = ResourceBundle.getBundle("local", new Locale("en", "US"));
    String value = bundle1.getString("localName");
    System.out.println("value = " + value);
    /*value = USA*/
}

Spring6国际化

Spring国际化是通过MessageSource接口来支持的。

MessageSource接口

常见实现类

ResourceBundleMessageSource:这是基于javaResourceBundle基础类实现,仅允许通过资源名加载国际化资源。
ReloadadabelResourceBundleMessageSource:它的功能和上面的类似,多了定时刷新功能,允许在不重启的情况下,更新资源。
StaticMessageSource:允许通过编程的方式提供国际化信息。

Spring6国际化使用

国际化文件命名格式:基本名称_语言_国家.properties。

步骤
1、创建资源文件

在这里插入图片描述

message_zh_CN.properties

test=你好 {0}, 时间:{1}

message_en_US.properties

test=welcome {0}, time:{1}

{0}、{1}表示的是通过动态方式传递。

2、创建配置文件
<?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>message</value>
                </list>
            </property>
            <property name="defaultEncoding">
                <value>utf-8</value>
            </property>
        </bean>
</beans>
3、测试
@Test
public void testCN() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Object[] obj = new Object[]{"khan", new Date().toString()};
    String value = context.getMessage("test", obj, Locale.CHINA);
    System.out.println("value = " + value);
    /*value = 你好 khan, 时间:Tue Jul 25 18:55:09 CST 2023*/
}
@Test
public void testUS() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Object[] obj = new Object[]{"louie", new Date().toString()};
    String value = context.getMessage("test", obj, Locale.US);
    System.out.println("value = " + value);
    /*value = welcome louie, time:Tue Jul 25 18:55:09 CST 2023*/
}
输出乱码解决

在这里插入图片描述

同时还需要将配置文件中的内容做修改,不然会因为修改编码后导致文件中变为乱码格式保留。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值