<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);
}
}