`
streamfly
  • 浏览: 88532 次
社区版块
存档分类
最新评论

rcp 中 实现 i18n

阅读更多
最近在用rcp 做公司产品的控制端,正好要用到i18n,虽然java有resouceBundle,但资源文件只能放到classes目录或lib目录下,很不方便,所以就写了一个新的,记在这里!!

这里主要有4个类,关键的有3个。

FilePath 类,获得相对文件路径
Lang 类 和 Resource类 真正实现 i18n.
IFiles 类,单纯方便的记录文件的相对路径。

IFiles  代码:

java 代码
 
  1. public interface IFiles {  
  2.     public static final String SYS_PATH = "sys/";  
  3.     public static final String LOGIN_FILE = SYS_PATH+"global_login.xml";  
  4.     public static final String SYS_PROP_FILE = SYS_PATH+"global.properties";  
  5. }  

FilePath 类 代码:

java 代码
 
  1. import java.net.URL;  
  2. import org.eclipse.core.runtime.Platform;  
  3. import org.eclipse.osgi.service.datalocation.Location;  
  4. import org.eclipse.ui.internal.util.BundleUtility;  
  5. import org.osgi.framework.Bundle;  
  6.   
  7. public class FilePath {  
  8.       
  9.     private static URL getAbsUrlFromPlugIn(String plugin, String name) {  
  10.         try {  
  11.             try {  
  12.                 Bundle bundle = Platform.getBundle(plugin);  
  13.                 if (!BundleUtility.isReady(bundle)) {  
  14.                     return null;  
  15.                 }  
  16.                 URL url = BundleUtility.find(bundle, name);  
  17.                 return url;  
  18.             } catch (Throwable e) {  
  19.                   
  20.             }  
  21.         } catch (Throwable e) {  
  22.               
  23.         }  
  24.         return null;  
  25.     }  
  26.       
  27.     public static URL getAbsUrl(String name) {//获得文件绝对路径  
  28.         try {  
  29.             try {  
  30.                 URL url = getAbsUrlFromPlugIn(IAppContents.APPLICATION_ID,name);  
  31.                 return url;  
  32.             } catch (Throwable e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         } catch (Throwable e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         return null;  
  39.     }  
  40.   
  41.     public static URL getLocalUrl() {//发布前获得eclipse所在安装路径,发布后是发布的绝对路径  
  42.         Location location = Platform.getInstallLocation();  
  43.         return location.getURL();  
  44.     }  
  45.       
  46. }  

Lang 类 代码:

java 代码
 
  1. import java.util.Properties;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.net.URL;  
  5. import (包名).IFiles;  
  6.   
  7. public class Lang {  
  8.       
  9.     private static Resources res = null;  
  10.     private static String fPath = "";  
  11.     private static String fCode = "";  
  12.       
  13.     public static Resources getinstance() {  
  14.         if(res==null) {  
  15.             getLangProps();  
  16.             res = new Resources(fPath,fCode);  
  17.         }  
  18.         return res;  
  19.     }  
  20.       
  21.     public static void delRes() {  
  22.         Lang.res = null;  
  23.     }  
  24.       
  25.     private static void getLangProps() {  
  26.         Properties props = new Properties();  
  27.         try {  
  28.             URL url = FilePath.getLocalUrl();//这里用的是获得绝对路径,而不是相对于工程的路径  
  29.             String path = url.getFile()+IFiles.SYS_PROP_FILE;  
  30.              File file = new File(path);  
  31.              FileInputStream input = new FileInputStream(file);  
  32.             props.load(input);  
  33.             fPath = props.getProperty("LANG_FILE");  
  34.             fCode = props.getProperty("LANG_CODE");           
  35.         }catch(Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.       
  40. }  


Resource 类 代码:

java 代码
 
  1. import java.util.Properties;  
  2. import java.net.URL;  
  3.   
  4. public class Resources {  
  5.       
  6.     private Properties prop;  
  7.     private String code;  
  8.     private String fPath;  
  9.       
  10.     public Resources(String fPath,String code) {  
  11.         this.prop = new Properties();  
  12.         this.fPath = fPath;  
  13.         this.code = code;  
  14.         init();  
  15.     }  
  16.       
  17.     public void init() {  
  18.         try {  
  19.             fPath = fPath+"i18n_"+code+".properties";  
  20.             URL url = FilePath.getAbsUrl(fPath);//这里就获得是相对于工程的绝对路径。因为文件放在工程下了  
  21.             prop.load(url.openConnection().getInputStream());  
  22.         }catch(Exception e) {  
  23.             e.printStackTrace();  
  24.         }         
  25.     }  
  26.       
  27.     public String get(String key) {  
  28.         String value = "";  
  29.         try {  
  30.             if(prop.containsKey(key)) {  
  31.                 value = prop.getProperty(key);  
  32.                   
  33.                 return new String(value.getBytes("iso-8859-1"),code);  
  34.             }  
  35.         }catch(Exception e) {  
  36.             return "!wrong!";  
  37.         }  
  38.         return value;  
  39.     }  
  40.       
  41.     public Properties getProperties() {  
  42.         return prop;  
  43.     }  
  44.       
  45.     public void clear() {  
  46.         prop.clear();  
  47.     }  
  48.       
  49. }  


使用格式要简单的多,Lang.getInstance().get("key value");
当然,每次更换语言还是要刷新界面的,而且刷新前要调用  Lang.delRes();方法,但我做的时候是在登录界面直接选择语言环境,所以可以只需要刷新登录界面就可以了,而不需要重新启动程序。

另外使用方法上也有 规则。
1。有一个总的properties 文件 来记录当前使用的语言。格式如下:
java 代码
  1. LANG_CODE=GBK  
  2. LANG_FILE=/res/i18n/  

LANG_CODE 必须 是正确的字符编码,因为这个数值 是和

java 代码
 
  1. return new String(value.getBytes("iso-8859-1"),code);  

code 的数值一致,实际上就是同一个数值。

除次之外,资源文件名有格式,程序里是拼接起来的。
i18n_[LANG_CODE].properties 这样使用时可以只需要更换字符编码就可以,方便使用。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics