覆写ResourceBundleMessage.
Spring留接口,其设计给力啊,很容易扩展。
package org.frame.base.message;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.context.support.ResourceBundleMessageSource;
/**
*
* 扩展Spring的resourceBundleMessageSource
* 使其支持中文,因为properties文件天生不支持中文,如果要其支持,需要转码,麻烦!
*
* 于是扩展一下,实现自动转码
*/
public class ResourceBundleMessageSourceExtend extends
ResourceBundleMessageSource {
private static final String ENCODING = "GBK";// 注意属性文件使用GBK编码
private static final String NULL = "null";
/** cache the encoding key value * */
Map<String, String> encodingCache = new ConcurrentHashMap<String, String>(
20);
/**
* resolve no argus
*/
protected String resolveCodeWithoutArguments(String code, Locale locale) {
String message = super.resolveCodeWithoutArguments(code, locale);
return decodeString(message, ENCODING);
}
/**
* resolve args
* @see resolveCode(String code, Locale locale)
*/
protected MessageFormat createMessageFormat(String msg, Locale locale) {
if (logger.isDebugEnabled()) {
logger.debug("Creating MessageFormat for pattern [" + msg
+ "] and locale '" + locale + "'");
}
msg = decodeString(msg, ENCODING);
return new MessageFormat((msg != null ? msg : ""), locale);
}
/**
* 转码
* @param msg
* @param encode
* @return
*/
private String decodeString(String message, String encode) {
String encodMessage = encodingCache.get(message);
if (encodMessage == null) {
try {
encodMessage = new String(message.getBytes("ISO8859-1"), encode);
if (message != null) {
encodingCache.put(message, encodMessage);
} else {
encodingCache.put(message, NULL);
// log the code is not exist in properties
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return encodMessage;
}
}
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<beans default-autowire="byName">
<!-- 国际化资源文件 -->
<bean id="messageSource" class="org.frame.base.message.ResourceBundleMessageSourceExtend">
<property name="basenames">
<list>
<value>message/message</value>
<value>message/error</value>
</list>
</property>
</bean>
</beans>
配置文件内容
message.user.username = 用户名 !
message.user.password = 密码 !
message.user.context = 内容{0}真好 !
message.user.username = user name !
message.user.password = password !
package org.frame.base.message;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageSourceTest {
public static void main(String[] args) {
MessageSource messageSource = new ClassPathXmlApplicationContext("message/testMessage.xml");
String message1 = messageSource.getMessage("message.user.username", null, Locale.CHINESE);
//String message3 = messageSource.getMessage("message.user.username", null, Locale.CHINESE);
String message4 = messageSource.getMessage("message.user.context", new Object[]{"ycl"}, Locale.CHINESE);
String message2 = messageSource.getMessage("error.user.validName", null, Locale.CHINESE);
System.out.println(message1);
System.out.println(message2);
System.out.println(message4);
}
}
配置文件使用GBK编码,不需要转码,就能够自动转码了
分享到:
相关推荐
### Spring 实现国际化 (i18n) 的关键技术点 #### 一、国际化(i18n)在Spring中的重要性及应用场景 国际化(Internationalization,简称i18n)是指设计和开发软件产品时,使得产品能在各种语言和区域环境中进行...
在Spring框架中,实现国际化(Internationalization,简称i18n)是常见的需求,它使得应用程序可以根据用户所在的地区提供不同的语言版本。以下是一个详细的步骤,解释如何在Spring应用中实现国际化。 1. **理解...
要实现Spring MVC的国际化,通常需要以下几个步骤: 1. 创建国际化资源文件:在Spring MVC项目中,创建一个或多个属性文件(通常以messages开头),这些文件包含了键值对,键是应用中需要翻译的文本的引用,而值是...
在 Spring MVC 框架中,实现国际化是一项非常重要的任务。国际化可以让我们的应用程序适应不同语言和地区,提高应用程序的可读性和可用性。本文将详细介绍如何使用 Spring MVC 实现国际化。 国际化的重要性 在全球...
这个“Spring Boot 国际化(i18n)配置demo.zip”包含了一个演示如何在Spring Boot项目中实现i18n的示例。通过这个例子,你可以了解到如何创建资源文件、配置消息源以及如何在控制器和视图中使用这些资源。 首先,...
本示例将详细介绍如何在Spring应用中实现国际化。 首先,我们需要了解国际化的基本概念。国际化是一个过程,通过这个过程,应用程序可以适应不同地区的文化、语言和习惯。这通常涉及到资源文件的使用,其中包含了...
本案例将深入探讨如何在Spring应用中实现国际化。 首先,我们需要了解i18n的基本概念。"i18n"这个术语来源于英文单词"internationalization",由于单词中有18个字母,因此得名。国际化的核心是将应用中的文本、日期...
在Spring框架中,实现国际化主要依赖于两个关键组件:`ResourceBundle`和`MessageSource`。`ResourceBundle`是Java标准库中的类,用于管理不同语言和地区的文本资源。`MessageSource`则是Spring提供的接口,它允许你...
3. **使用注解实现国际化** Spring MVC提供了`@MessageSource`注解,可以方便地在控制器方法中获取本地化消息。首先,你需要在控制器类上添加`@Controller`注解,然后在方法上使用`@RequestMapping`注解来处理请求...
阅读本文之前,你应该了解过SpringBoot的国际化实现与原理,在这里简单介绍下: 1. 国际化 国际化(internationalization),又称为i18n(因为这个单词从i到n有18个英文字母,因此命名)。对于某些应用系统而言,它...
在Spring MVC中实现国际化,主要涉及以下几个关键步骤和概念: 1. **资源文件**:首先,我们需要创建资源文件,这些文件通常以`.properties`为扩展名,如`messages.properties`。每个文件对应一种语言或地区,例如`...
本示例将指导你如何在Spring Boot中自定义国际化实现,包括从文件夹中动态加载多个国际化文件、根据用户请求动态设置前端显示的语言,并通过拦截器和注解自动化处理。 首先,让我们回顾一下项目的初始化步骤。在...
在本项目"spring国际化项目"中,我们将深入探讨如何在Spring环境中实现国际化。 首先,我们需要理解国际化的基本概念。国际化不仅仅是翻译文本,它涉及将应用程序中的所有文化特定元素,如日期格式、货币符号、数字...
在Spring框架中,ApplicationContext是IoC容器的核心,它不仅负责管理Bean,还提供了实现国际化(i18n)的功能。国际化使应用程序能够适应不同语言和地区的用户,无需修改代码即可提供多语言支持。Spring通过实现...
通过以上步骤,你可以为Spring Boot应用实现完整的国际化功能,包括页面的中英文切换,并确保在页面跳转时,语言设置得以保持。记得在实际项目中,还要考虑如何优雅地处理未找到对应语言资源的情况,以及优化语言...
在Spring MVC 3中,国际化是一项重要的功能,它允许我们为不同的地区和语言提供定制的显示内容。在“spring mvc 3 国际化(下)——简单自定义操作”这一主题中,我们将深入探讨如何自定义国际化过程,以满足特定的...
在本文中,我们将深入探讨如何在Spring Boot应用中整合MyBatis,实现MySQL数据库连接,以及如何利用Spring MVC和拦截器来实现国际化(i18n)功能。此外,我们还将提及IIS 12作为可能的Web服务器选项。 首先,Spring...
SpringBoot 消息国际化配置实现过程解析 SpringBoot 消息国际化配置实现过程解析是指在 SpringBoot 项目中实现消息国际化配置的过程。该过程主要涉及到消息配置文件的管理、语言信息的设置、LocaleContext 的使用、...
在Spring Boot应用中,配置国际化是一项重要的任务,它允许我们为不同的地区和...以上就是Spring Boot配置国际化的基本知识和实现方式,通过这个源码项目,你可以深入理解并实践这些概念,为你的应用提供全球化支持。
在SSM框架上实现国际化和多语言功能,可以使得应用程序能够适应不同国家和地区用户的语言需求,提升用户体验。以下将详细介绍如何在SSM框架中实现这一功能。 首先,我们需要理解“国际化”(Internationalization)...