Spring 使用国际化信息 MessageSource

引用参考:
--学习Spring必学的Java基础知识(8)----国际化信息
http://stamen.iteye.com/blog/1541732
--Spring 利用MessageSource实现国际化
http://blog.csdn.net/moshenglv/article/details/53761959
//
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoResourceBundle {
	private static ClassPathXmlApplicationContext context = null;
	private static MessageSource messageSource;
	private static String DEFAULT_ERROR_MESSAGE;
	
	static 
	{
		context = new ClassPathXmlApplicationContext("crm/baseConfig/commonBean.xml");//加载spring配置文件
		messageSource = (MessageSource)context.getBean("crm.messageSource");
		DEFAULT_ERROR_MESSAGE = DemoResourceBundle.getMessage(DemoConstant.SYS_ERROR);
	}
	
	public static String getMessage(String code) {
		return messageSource.getMessage(code, null, null, Locale.SIMPLIFIED_CHINESE);
	}
	
	public static String getMessage(String code, String defaultMessage) {
		return messageSource.getMessage(code, null, defaultMessage, Locale.SIMPLIFIED_CHINESE);
	}
	
	public static String getMessage(String code, String defaultMessage, Locale locale) {
		return messageSource.getMessage(code, null, defaultMessage, locale);
	}

	public static String getDefaultErrorMessage() {
		return DEFAULT_ERROR_MESSAGE;
	}
}
---------------------------------------------------------------------------------
spring的xml配置文件 commonBean.xml--国际化

<bean id="crm.messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list>
			<value>crm.baseConfig.crmErrorCodes</value>
		</list>
	</property>
</bean>
---------------------------------------------------------------------------------
中文错误码配置文件
crmErrorCodes_zh_CN.properties
英文错误码配置文件
crmErrorCodes_en_US.properties
---------------------------------------------------------------------------------
使用:
String msg = DemoResourceBundle.getMessage(e.getCode(), e.getTextMessage());
---------------------------------------------------------------------------------

猜你喜欢

转载自franciswmf.iteye.com/blog/2413668
今日推荐