Spring uses internationalized message MessageSource

Citation reference:
--The basic knowledge of Java that Spring must learn (8)----Internationalization information
http://stamen.iteye.com/blog/1541732
--Spring uses MessageSource to achieve internationalization
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");//Load spring configuration file
		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's xml configuration file commonBean.xml--internationalization

<bean id="crm.messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list>
			<value>crm.baseConfig.crmErrorCodes</value>
		</list>
	</property>
</bean>
---------------------------------------------------------------------------------
Chinese error code configuration file
crmErrorCodes_zh_CN.properties
English error code configuration file
crmErrorCodes_en_US.properties
---------------------------------------------------------------------------------
use:
String msg = DemoResourceBundle.getMessage(e.getCode(), e.getTextMessage());
---------------------------------------------------------------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326115905&siteId=291194637