打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring实现国际化
覆写ResourceBundleMessage.
Spring留接口,其设计给力啊,很容易扩展。
Java代码  
  1. package org.frame.base.message;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.text.MessageFormat;  
  5. import java.util.Locale;  
  6. import java.util.Map;  
  7. import java.util.concurrent.ConcurrentHashMap;  
  8.   
  9. import org.springframework.context.support.ResourceBundleMessageSource;  
  10.   
  11. /** 
  12.  *  
  13.  * 扩展Spring的resourceBundleMessageSource 
  14.  * 使其支持中文,因为properties文件天生不支持中文,如果要其支持,需要转码,麻烦! 
  15.  *  
  16.  * 于是扩展一下,实现自动转码 
  17.  */  
  18. public class ResourceBundleMessageSourceExtend extends  
  19.         ResourceBundleMessageSource {  
  20.   
  21.     private static final String ENCODING = "GBK";// 注意属性文件使用GBK编码  
  22.     private static final String NULL = "null";  
  23.   
  24.     /** cache the encoding key value * */  
  25.     Map<String, String> encodingCache = new ConcurrentHashMap<String, String>(  
  26.             20);  
  27.   
  28.     /** 
  29.      * resolve no argus 
  30.      */  
  31.     protected String resolveCodeWithoutArguments(String code, Locale locale) {  
  32.         String message = super.resolveCodeWithoutArguments(code, locale);  
  33.         return decodeString(message, ENCODING);  
  34.   
  35.     }  
  36.   
  37.     /** 
  38.      * resolve args 
  39.      * @see resolveCode(String code, Locale locale) 
  40.      */  
  41.     protected MessageFormat createMessageFormat(String msg, Locale locale) {  
  42.         if (logger.isDebugEnabled()) {  
  43.             logger.debug("Creating MessageFormat for pattern [" + msg  
  44.                     + "] and locale '" + locale + "'");  
  45.         }  
  46.         msg = decodeString(msg, ENCODING);  
  47.         return new MessageFormat((msg != null ? msg : ""), locale);  
  48.     }  
  49.   
  50.     /** 
  51.      * 转码  
  52.      * @param msg 
  53.      * @param encode 
  54.      * @return 
  55.      */  
  56.     private String decodeString(String message, String encode) {  
  57.         String encodMessage = encodingCache.get(message);  
  58.         if (encodMessage == null) {  
  59.             try {  
  60.                 encodMessage = new String(message.getBytes("ISO8859-1"), encode);  
  61.                 if (message != null) {  
  62.                     encodingCache.put(message, encodMessage);  
  63.                 } else {  
  64.                     encodingCache.put(message, NULL);  
  65.                     // log the code is not exist in properties  
  66.                 }  
  67.             } catch (UnsupportedEncodingException e) {  
  68.                 e.printStackTrace();  
  69.             }  
  70.         }  
  71.         return encodMessage;  
  72.     }  
  73.   
  74. }  


配置文件如下:
Java代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">  
  3. <beans default-autowire="byName">  
  4.    
  5.       
  6.     <!-- 国际化资源文件 -->  
  7.     <bean id="messageSource" class="org.frame.base.message.ResourceBundleMessageSourceExtend">    
  8.            <property name="basenames">    
  9.                 <list>    
  10.                    <value>message/message</value>    
  11.                    <value>message/error</value>     
  12.                </list>    
  13.            </property>    
  14.     </bean>    
  15.        
  16. </beans>  


配置文件内容
Java代码  
  1. message.user.username = 用户名 !  
  2. message.user.password = 密码 !  
  3.   
  4. message.user.context = 内容{0}真好 !  


Java代码  
  1. message.user.username = user name !  
  2. message.user.password = password !  


Java代码  
  1. package org.frame.base.message;  
  2.   
  3. import java.util.Locale;  
  4.   
  5. import org.springframework.context.MessageSource;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class MessageSourceTest {  
  9.    
  10.     public static void main(String[] args) {   
  11.            MessageSource messageSource = new ClassPathXmlApplicationContext("message/testMessage.xml");    
  12.            String message1 = messageSource.getMessage("message.user.username"null, Locale.CHINESE);  
  13.            //String message3 = messageSource.getMessage("message.user.username", null, Locale.CHINESE);  
  14.            String message4 = messageSource.getMessage("message.user.context"new Object[]{"ycl"}, Locale.CHINESE);  
  15.            String message2 = messageSource.getMessage("error.user.validName"null, Locale.CHINESE);  
  16.            System.out.println(message1);  
  17.            System.out.println(message2);  
  18.            System.out.println(message4);  
  19.     }  
  20.   
  21. }  


配置文件使用GBK编码,不需要转码,就能够自动转码了
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c#winform线程间操作UI的五种方法
RabbitMQ (九) 消息的参数详解
JavaMail 深入浅出
jQuery无刷新聊天室一例[分析]
flex+blazeds+java+spring后台消息推送,有界面维护
简单聊天程序java socket
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服