Spring 使用国际化信息 MessageSource

Spring国际化配置详解
本文介绍如何在Spring中实现国际化配置,包括使用ResourceBundleMessageSource和ReloadableResourceBundleMessageSource两种方式,并提供了具体的代码示例及配置文件说明。
引用参考:
//第一种
【ResourceBundleMessageSource】--org.springframework.context.support.ResourceBundleMessageSource
--学习Spring必学的Java基础知识(8)----国际化信息
[url]http://stamen.iteye.com/blog/1541732[/url]
--Spring 利用MessageSource实现国际化
[url]http://blog.youkuaiyun.com/moshenglv/article/details/53761959[/url]
//第二种
【ReloadableResourceBundleMessageSource】--org.springframework.context.support.ReloadableResourceBundleMessageSource
[url]https://blog.youkuaiyun.com/qincidong/article/details/40866975[/url]
//

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());
---------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值