A simple example of an internationalized message in Spring (ApplicationContext) is not a version that is integrated with the framework

Source: Internet
Author: User
Tags getmessage

First, create an XML file that describes the message, named Messages.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<!--resource Internationalization test--
<bean id= "Messagesource" class= "Org.springframework.context.support.ReloadableResourceBundleMessageSource" > <property name= "Basenames" >
<list>
<value>org/rjstudio/spring/properties/messages</value>
</list>
</property>
</bean>
</beans>

This bean's ID is dead and can only be "Messagesource". The class here needs to fill in the implementation of the Messagesource interface. Among them, in the book I read only mentioned two classes, one is: Resourcebundlemessagesource, the other is Reloadableresourcebundlemessagesource. The latter provides features that can reload the new configuration without rebooting.

The body value "Org/rjstudio/spring/properties/messages" in the Value child node of the list node, Refers to the properties file under the Org.rjstudio.spring.proerties package with messages as the main name. For example, with locale as ZH_CN, Spring automatically searches the classpath for configuration files and loads under the Org.rjstudio.spring.properties package in the following order:



Next, let's set up two messages properties files under Org.rjstudio.spring.properties. One is named Messages_zh_cn.properties and the other is messages_en_us.properties, which corresponds to the internationalization of China and the United States.

Create a UserInfo property in both of these properties files.
China: userinfo= Current Login user [{0}] login time [{1}]
United States: Userinfo=current login user:[{0}] login Time:[{1}]

OK, everything is ready, then you can write a code to test it. To build a class, write a test main method.




public class Messagetest {
public static void Main (string[] args) {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Messages.xml");
object[] arg = new object[] {"Erica", Calendar.getinstance (). GetTime ()};
String msg = ctx.getmessage ("userinfo", Arg,locale.china);
System.out.println ("Message is ===>" + msg);
}
}

The results of the final output are: Message is ===> currently logged in User: [Erica] logon time: [07-6-8 10:20]

Ctx.getmessage ("UserInfo", Arg,locale.getdefault ()); This method, the three parameters passed in, the first one is the corresponding name in the properties file. Arg is an array of objects, we put two variables in the properties, [{0}] and [{1}],spring] will assign values to them. In the end, you need to pass in a local. Here we use Locale.china to represent China. If we use locale.us, the output will change to:

Message is ===> current login User:[erica] login time:[6/8/07 10:59 AM]

OK, here it is.

A simple example of an internationalized message in Spring (ApplicationContext) is not a version that is integrated with the framework

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.