spring-国际化

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

我们先来看下java中的国际化

public class A {

    public static void main(String[] args) throws IOException {
        //获取en_GB的文件中的内容信息
        ResourceBundle resourceBundle=ResourceBundle.getBundle("message", Locale.UK);
        String aa = resourceBundle.getString("aa");
        System.out.println(aa);
        //获取zh_CN的文件中的内容信息
        ResourceBundle resourceBundle1=ResourceBundle.getBundle("message", Locale.CHINESE);
        String aa1 = resourceBundle1.getString("aa");
        System.out.println(aa1);
    }
}

创建message_en_GB.properties

aa=ni hao

创建message_zh_CN.properties

aa=123456

 我们在来看下spring中的国际化

我们对配置文件进行改造

aa=ni hao {0}
aa=123456 {0}

创建bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--messageSource这个名字必须这样写 不能改-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <!--指向message_en,message_zh文件-->
        <property name="basename" value="message"></property>
        <property name="defaultEncoding" value="utf-8"></property>
    </bean>
</beans>
public class A {

    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        //动态给{0}赋值   en_GB文件
        Object[]ob=new Object[]{"my aa"};
        String aa = applicationContext.getMessage("aa", ob, Locale.UK);
        System.out.println(aa);
        
        //动态给{0}赋值   zh_CN文件
        Object[]ob1=new Object[]{"哈哈哈"};
        String aa1 = applicationContext.getMessage("aa", ob1, Locale.CHINESE);
        System.out.println(aa1);

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值