标签:lin img oca 传参数 ring ges coding locale 图片
(一) xml配置-config.xml line11-19
重点是id="messageSource"
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 7 <bean id="user" class="com.vi.springbean.aotutag.User"></bean> 8 9 <bean id="userFactoryBean" class="com.vi.springbean.aotutag.UserFactoryBean"></bean> 10 11 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 12 <property name="basenames"> 13 <array> 14 <value>messages</value> 15 </array> 16 17 </property> 18 <property name="defaultEncoding" value="UTF-8"/> 19 </bean> 20 </beans>
(二)properties文件定义
messages_en_US.properties
1 hello_word=hello Spring 2 msg_1=hello Spring,{0} 3 msg_2=hello Spring,{0},{1}
messages_en_US.properties
1 hello_word=初识,Spring 2 msg_1=初识,Spring,{0} 3 msg_2=初识,Spring,{1},{0}
(三)测试类
必须是 ApplicationContext ctx = new MyClassPathXMLApplicationContext(path);
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(path)); 不支持
1 public class AppTest { 2 3 @Test 4 public void test() { 5 String path = "config.xml"; 6 ApplicationContext ctx = new MyClassPathXMLApplicationContext(path); 7 String strUS= ctx.getMessage("hello_word",new Object[0], Locale.US); 8 String strCN= ctx.getMessage("hello_word",new Object[1], Locale.SIMPLIFIED_CHINESE); 9 System.out.println(strUS); 10 System.out.println(strCN); 11 System.out.println(); 12 System.out.println(ctx.getMessage("msg_2",new Object[]{new Date(),"加油 !"},Locale.US)); 13 System.out.println(ctx.getMessage("msg_2",new Object[]{"Come on !",new Date()},Locale.SIMPLIFIED_CHINESE)); 14 15 } 16 17 18 }
(四)测试结果
hello Spring
初识,Spring
hello Spring,1/9/20 11:52 AM,加油 !
初识,Spring,20-1-9 上午11:52,Come on !
(五)其他问题
(1)出现乱码,修改文件编码格式,
idea:
(2)调用的语言,请与定义的properties命名一致,
properties的命名格式{XX}_{语言}_{国家}.properties
(3)传参与properties中的占位符是由关联的,推测传参数组下标与占位符一致,具体有待看源码
加油加油 小菜????
标签:lin img oca 传参数 ring ges coding locale 图片
原文地址:https://www.cnblogs.com/VVII/p/12170717.html