第一步 创建资源文件
国际化文件命名格式:基本名称 _ 语言 _ 国家.properties
bean.xml spring配置文件
<?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>
messages
</value>
</list>
</property>
<property name="defaultEncoding">
<value>
utf-8
</value>
</property>
</bean>
</beans>
第三步 创建测试类
public class TestI18n {
@Test
public void t_test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
String str=context.getMessage("ch", null, Locale.CHINA);
System.out.println(str);
}
}
然后我进行了测试,运行一下,报了个错误org.springframework.context.NoSuchMessageException: No message found under code 'ch' for locale 'zh_CN'.
那么遇到这个错误,大家先检查下看下是不是自定义文件目录配置错误,配置中没有写文件名前缀messages。
当我把设置默认字符集的属性注入改成写在一行
<property name="defaultEncoding">
<value>utf-8</value>
</property>
那么问题就完美的解决了!
var code = "d32227e0-e366-45bd-9aff-d53cee54824b"