Spring internationalization example

Source: Internet
Author: User



/WEB-INF/ages/test/messages




 

 

Create two properties files for messages under/WEB-INF/ages/test/messages. One is messages_zh_CN.properties, and the other is messages_en_US.properties, which correspond to China and the United States in internationalization respectively.

Create a userinfo attribute in the two Property Files.
China: userinfo = Current Login User [{0}] login time [{1}]
In the United States: userinfo = current login user: [{0}] login time: [{1}]

 

Next, you can write a code segment to test. Create classes and write a test Main method.

 

Public class MessageTest {
Public static void main (String [] args ){
ApplicationContext ctx = new ClassPathXmlApplicationContext (spring. xml );
Object [] arg = new Object [] {Zhang San, Calendar. getInstance (). getTime ()};
String msg = ctx. getMessage (userinfo, arg, Locale. CHINA );
System. out. println (Message is ==>+ msg );
}
}

 

The final output result is: Message is ==> current logon User: [Zhang San] Logon Time: [14-3-23]

Ctx. getMessage (userinfo, arg, Locale. getDefault (); In this method, the first parameter is the name of the properties file. Arg is an array of objects. We place two variables in properties, [{0}] and [{1}]. Spring will assign values to them. In the end, a Local is required. Locale. CHINA stands for CHINA.

 

If we use Locale. US, the output will be changed to: Message is ==> current login user: [Erica] login time: [3/23/14 AM]

 

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.