Spring internationalization simple example

Spring internationalization simple example

首先,建立一个描述message的XML文件,名为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>
    <!-- 资源国际化测试 -->
    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames">
            <list>
                <value>org/rjstudio/spring/properties/messages</value>
            </list>
        </property>
    </bean>
</beans>
    
    The Bean's id is set to die, only to "messageSource". Class here need to fill achieve MessageSource interface. Which, in my book it refers only to see two classes, one is: ResourceBundleMessageSource, the other is ReloadableResourceBundleMessageSource. Among them, the latter can be provided without having to reboot to reload the new configuration features.
    
    body child node value value in the node list "org / rjstudio / spring / properties / messages", refers to messages main properties file name under org.rjstudio.spring.proerties package. For example, in order to zh_CN Locale for example, Spring will automatically search for the configuration file in the following order under org.rjstudio.spring.properties package in the class path and loads:
    
    
    
    Next, let us org.rjstudio.spring.properties , the establishment of two messages properties file. Named messages_zh_CN.properties, the other is messages_en_US.properties, respectively internationalization of China and the United States.
    
    Establish a userinfo property respectively in these two properties file.
        China is: userinfo = Current User [{0}] landing time [{1}]
        United States: userinfo = current login user: [ {0}] login time: [{1}]
    
    Well, everything is ready, the next you can write code to test the segment. . Build a class, write a test method Main.
    
    
    
    
    public class MessageTest {
        static void main public (String [] args) {
            the ApplicationContext the ClassPathXmlApplicationContext new new CTX = ( "messages.xml");
            Object [] = new new Arg Object [] { "Erica", Calendar.getInstance () the getTime ()};.
            String = ctx.getMessage MSG ( "UserInfo", Arg, Locale.CHINA);
            System.out.println ( "the Message iS ===>" + MSG);
        }
    }
    
    last output is: Message is ===> current Login user: [Erica] connected: [07-6-8 10:20 am]
    
    ctx.getMessage ( "UserInfo", Arg, Locale.getDefault ()); this method, passing three parameters, the first is the name of the corresponding properties file. arg is an array of objects, we placed two variable properties which, [{0}] and [{1}], Spring will assign them to us. And finally we need to pass a Local. Here with Locale.CHINA on behalf of China. If we use Locale.US, the output will be:
    
    the Message IS ===> Current the Login the User:

Guess you like

Origin blog.csdn.net/songxiugongwang/article/details/90983399