Spring代码案例003-MessageSource

SpringDemo.java

package cn.bigsoldier.spring.messageSource;

import java.util.Calendar;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "cn/bigsoldier/spring/messageSource/beans-config.xml");

  Object[] arguments = new Object[] { "大兵",
    Calendar.getInstance().getTime() };

  System.out.println(context
    .getMessage("userLogin", arguments, Locale.US));
  System.out.println(context.getMessage("userLogin", arguments,
    Locale.TAIWAN));

 }
}

beans-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


 <bean id="messageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename"
   value="cn.bigsoldier.spring.messageSource.message">
  </property>
 </bean>
</beans>

message_en_US.properties

userLogin=User {0} login at {1}

message_en_TW.properties

userLogin=\u4F7F\u7528\u8005 {0} \u65BC {1} \u767B\u5165

你可能感兴趣的:(message)