Spring + ExtJs4.x 国际化 (附:中文代码过滤Demo)

Spring+ExtJs4.x国际化教程
本文介绍如何实现Spring框架和ExtJs4.x的国际化功能。包括代码中中文的提取、Spring国际化配置、资源文件的创建及使用,以及ExtJs4.x中语言设置的方法。

Spring + ExtJs4.x 国际化 (附:中文代码过滤Demo)


一、最近系统要增加国际化的功能,首先要把代码中所有的中文copy出来,交付翻译;

以下是自己写的过滤中文Demo:


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">public static void main(String[] args) {  
  2.         Scanner.scanning("D:\\test");  
  3.     }</span>  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">public class Scanner {  
  2.   
  3.     /** 
  4.      * 递归遍历文件 
  5.      */  
  6.     public static List<File> getFile(File f) {  
  7.         File[] allfiles = f.listFiles();  
  8.         List<File> filelist = new ArrayList<File>();  
  9.         for (int i = 0; i < allfiles.length; i++) {  
  10.             if (allfiles[i].isDirectory()) {  
  11.                 filelist.addAll(getFile(allfiles[i]));  
  12.             } else {  
  13.                 filelist.add(allfiles[i]);  
  14.             }  
  15.         }  
  16.         return filelist;  
  17.     }  
  18.   
  19.     /** 
  20.      * 开始解析 
  21.      */  
  22.     public static void scanning(String path) {  
  23.         System.out.println("*************开始解析:" + path + "*****************");  
  24.         List<File> filelist = getFile(new File(path));  
  25.         for (File file : filelist) {  
  26.             Analyzer.analyze(file);  
  27.         }  
  28.         System.out.println("************路径:" + path + "下的文件解析完毕**********");  
  29.     }  
  30.   
  31. }  
  32. </span>  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">public class Analyzer {  
  2.   
  3.     private static String encoding = "UTF-8";  
  4.   
  5.     public static void analyze(File f) {  
  6.         if (f.isFile() && f.exists()) {  
  7.             String pstr = "\"([^\"|[\u4e00-\u9fa5]]+)\"";  
  8.             if (f.getName().endsWith(".js")) {  
  9.                 pstr = "\'([[\u4e00-\u9fa5]]+)\'";// 匹配 ''之间的中文内容  
  10.             } else if (f.getName().endsWith(".java")) {  
  11.                 pstr = "\"([[\u4e00-\u9fa5]]+)\"";// 匹配 " " 之间的中文内容  
  12.             } else {  
  13.                 return;  
  14.             }  
  15.             try {  
  16.                 FileInputStream fileInStr = new FileInputStream(f);  
  17.                 InputStreamReader read = new InputStreamReader(fileInStr,  
  18.                         encoding);  
  19.                 BufferedReader bufferedReader = new BufferedReader(read);  
  20.                 while (bufferedReader.ready()) {  
  21.                     String rowStr = bufferedReader.readLine();  
  22.                     if (rowStr.startsWith("//"))  
  23.                         continue;  
  24.                     Pattern p = Pattern.compile(pstr);  
  25.                     Matcher m = p.matcher(rowStr);  
  26.                     if (m.find()) {  
  27.                         for (int i = 0; i < m.groupCount(); i++) {  
  28.                             System.out.println(m.group(i));  
  29.                         }  
  30.                     }  
  31.                 }  
  32.             } catch (Exception e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.     }  
  37. }</span>  

正则表达式不是很懂,摸索着写出来的,测试无误,高手可能会有更好的写法,呵呵!


翻译好后,开始建语言资源文件

二、Spring的国际化

1.配置applicationContext.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">    
  3.     
  4. <beans>    
  5.      
  6.        <bean id="messageSource"  class="org.springframework.context.support.ResourceBundleMessageSource">    
  7.      
  8.       <property name="basename" value="messages"/>    
  9.     
  10.       </bean>    
  11.      
  12.        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>    
  13.        
  14. </beans>  </span>  

2.在src根目录下面创建4个资源文件,分别是:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">messages_zh.properties  
  2. main.title=你好  
  3.   
  4. messages_en.properties  
  5. main.title=Hello World!  
  6.   
  7. messages_ja.properties  
  8. main.title=こんにちは  
  9.   
  10. messages_ko.properties  
  11. main.title=안녕하십니까</span>  

3.JSP页面的写法

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">    <%@ page language="java"  pageEncoding="UTF-8"%>    
  2.     <%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>    
  3.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
  4.     <html>    
  5.       <head>    
  6.         <title>Spring国际化</title>    
  7.       </head>    
  8.       <body>    
  9.          
  10.         <spring:message code="main.title" /><br>    
  11.         
  12.         <input type="button" value="<spring:message code="main.title" />"/><br>    
  13.         
  14.       </body>     
  15.         
  16.     </html>  </span>  

4.在web.xml加入

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">    <context-param>    
  2.       <param-name>contextConfigLocation</param-name>    
  3.       <param-value>    
  4.        classpath*:/applicationContext*,classpath*:META-INF/applicationContext*.xml    
  5.       </param-value>    
  6.      </context-param>    
  7.      <listener>    
  8.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  9.      </listener>  </span>  

注意事项:

1:用hibernate3.0,连接Mysql5.0数据库。

如果用hibernate.properties配置文件
hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

如果用hibernate.cfg.xml配置文件
jdbc:mysql://localhost:3306/test?useUnicode=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8

2:页面的编码方式,应该选用utf-8
<%@ page language="java"  pageEncoding="UTF-8"%>

3:创建的数据库的编码方式也应该选用utf-8,以及表,字段的编码方式都应选用utf-8

注意以上3点就可以解决国际化时,所出现的页面显示乱码问题,以及插入韩语时,出现的data too long for column问题.
这样用Spring国际化的jsp页面就做好了,此种方法是自动默认当前用户的语言,比如客户端是日语系统,就自动寻找messages_ja.properties资源文件,是英语系统,就自动寻找messages_en.properties资源文件。


三、ExtJs4.X的国际化

1、取得浏览器语言设定
  request.setAttribute("browserLang", request.getLocale().toString());
2、引入国际化文件(注意:在ext-all.js文件引入后再引入国际化文件才有效)
  <script type="text/javascript" src="ext/source/locale/ext-lang-<%=browserLang %>.js"></script>  
3、指定正确的页面字符集
  <%@ page contentType="text/html; charset=UTF-8" language="java"%>

ext-lang-zh_CN.js的内容如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var myProject = {};    
  2. myProject.string = {};    
  3. myProject.string.title = '我的表单';    
  4. myProject.string.lable = '本地化';   

MyForm.js内容如下:

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Ext.define('MyApp.view.MyForm', {    
  2.     extend: 'Ext.form.Panel',    
  3.     
  4.     height: 250,    
  5.     width: 400,    
  6.     bodyPadding: 10,    
  7.     title: myProject.string.title,  // 这里是重点  
  8.     
  9.     initComponent: function() {    
  10.         var me = this;    
  11.     
  12.         Ext.applyIf(me, {    
  13.             items: [    
  14.                 {    
  15.                     xtype: 'label',    
  16.                     text: myProject.string.lable  // 这里是重点  
  17.                 }    
  18.             ]    
  19.         });    
  20.     
  21.         me.callParent(arguments);    
  22.     }    
  23.     
  24. });    

其他的语言文件也这样写就可以了。


至此,国际化的工作OK!

更多 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值