"Spring" internationalization support

Source: Internet
Author: User

"Spring" Internationalization support
first, the overall structure:
Content in two international resources:
Second, the procedure
2.1 Configuring the Spring context Beans.xml file
  
 
  1. <?xml version="1.0" encoding="GBK"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <bean id="messageSource"
  7. class="org.springframework.context.support.ResourceBundleMessageSource">
  8. <!-- 驱动Spring调用messageSource Bean的setBasenames()方法,
  9. 该方法需要一个数组参数,使用list元素配置多个数组元素 -->
  10. <property name="basenames">
  11. <list>
  12. <value>message1</value>
  13. <!-- 如果有多个资源文件,全部列在此处 -->
  14. </list>
  15. </property>
  16. </bean>
  17. </beans>
Description

2.2 Test procedure
  
 
  1. package lee;import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.*;
  3. public class SpringTest
  4. {
  5. public static void main(String[] args)throws Exception
  6. {
  7. // 实例化ApplicationContext
  8. ApplicationContext ctx = new
  9. ClassPathXmlApplicationContext("beans.xml");
  10. // 使用getMessage()方法获取本地化消息。
  11. // Locale的getDefault方法返回计算机环境的默认Locale
  12. String hello = ctx.getMessage("hello" , new String[]{"孙悟空"}
  13. , Locale.getDefault());
  14. String now = ctx.getMessage("now" , new Object[]{new Date()}
  15. , Locale.getDefault());
  16. // 打印出两条本地化消息
  17. System.out.println(hello);
  18. System.out.println(now);
  19. }
  20. }

2.3 Operation Result:







From for notes (Wiz)

"Spring" internationalization support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.