spring的学习历程之利用MessageSource实现国际化占位符直接输出原因(一)
来源:互联网 发布:万捷网络验证系统 编辑:程序博客网 时间:2024/06/02 00:11
利用MessageSource实现国际化其中有一个着眼于传递参数给查找的消息,这些参数将被转换为字符串并插入到已查找到的消息中的占位符(译注:资源文件中花括号里的数字即为占位符)。#其中'{0}'会直接输出{0} 这是因为这个单号的原因使得占位符直接输出。
下面是一个MessageSource的例子:
bean的配置:
<beans> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>format</value> <value>exceptions</value> <value>windows</value> </list> </property> </bean></beans>这段配置假定在你的classpath中有三个资源文件(resource bundle),它们是
format
,exceptions
和windows
。通过ResourceBundle,使用JDK中解析消息的标准方式,来处理任何解析消息的请求。# in 'format.properties'message=Alligators rock!
# in 'exceptions.properties' <span style="color:#ff0000;">这里的{0}要写上单引号时要注意了,如果是英文中的单引号,那么就是直接输出占位符来的</span>argument.required=The<span style="color:#ff6666;"> '{0}'</span> argument is required.
下面是测试代码。因为ApplicationContext
实现也都实现了MessageSource
接口,所以能被转型为MessageSource
接口
public static void main(String[] args) { MessageSource resources = new ClassPathXmlApplicationContext("beans.xml"); String message = resources.getMessage("message", null, "Default", null); System.out.println(message);String eMessage = mr.getMessage("argument.required",new Object[]{"exception"},"Required",null);System.out.println(eMessage);}
上述程输出的结果:
Alligators rock!The ‘exception’ argument is required //如果是英文中的会输出:<span style="font-family: Arial, Helvetica, sans-serif;">The {0} argument is required</span>
0 0
- spring的学习历程之利用MessageSource实现国际化占位符直接输出原因(一)
- Spring 利用MessageSource实现国际化
- Spring 利用MessageSource实现国际化
- 【Spring】Spring 利用MessageSource实现国际化
- 利用MessageSource实现国际化[I18N]
- spring中MessageSource实现国际化i18n
- Spring之国际化信息MessageSource源码阅读
- 复习struts2之输出带有占位符的国际化信息
- Spring(21)——国际化MessageSource
- 输出带有占位符的国际化信息
- Java for Web学习笔记(七三):国际化i18n(1)使用Spring框架MessageSource
- struts国际化和国际化输出占位符
- Spring 国际化 bean的id必须是messageSource
- struts2输出带有占位符的国际化信息
- struts2输出带有占位符的国际化信息
- Struts2 输出带有占位符的国际化信息
- Struts2_输出带有占位符的国际化信息
- Spring国际化--从数据库读取messageSource
- POJ 1195 解题报告
- Javascript高级程序设计第20章(JSON)
- poj 1466 Girls and Boys
- Vollery 框架之StringRequest二次封装
- pointer on C_1 基本概念
- spring的学习历程之利用MessageSource实现国际化占位符直接输出原因(一)
- 给定一个数组,返回最大子数组的累加和并且返回该子数组
- HDU - 1059 Dividing(完全背包+多重背包)
- C和指针(学习之旅)
- 蓝桥杯-Playfair密码
- C语言条件编译及编译预处理阶段
- the file opeartion in python(一)
- 究竟什么是中间件
- test