---
title: "rcp 中 实现 i18n"
author: "streamfly"
date: "2007-12-28T11:19:58Z"
tags: []
slug: "rcp-i18n"
draft: false
lastmod: "2007-12-28T11:19:58Z"
id: 151441
url: "https://streamfly.iteye.com/blog/151441"
---

最近在用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 代码

 

01. import java.net.URL;
02. import org.eclipse.core.runtime.Platform;
03. import org.eclipse.osgi.service.datalocation.Location;
04. import org.eclipse.ui.internal.util.BundleUtility;
05. import org.osgi.framework.Bundle;
06. public class FilePath {
07. private static URL getAbsUrlFromPlugIn(String plugin, String name) {
08. try {
09. try {
10. Bundle bundle = Platform.getBundle(plugin);
11. if (!BundleUtility.isReady(bundle)) {
12. return null;
13. }
14. URL url = BundleUtility.find(bundle, name);
15. return url;
16. } catch (Throwable e) {
17. }
18. } catch (Throwable e) {
19. }
20. return null;
21. }
22. public static URL getAbsUrl(String name) {//获得文件绝对路径
23. try {
24. try {
25. URL url = getAbsUrlFromPlugIn(IAppContents.APPLICATION\_ID,name);
26. return url;
27. } catch (Throwable e) {
28. e.printStackTrace();
29. }
30. } catch (Throwable e) {
31. e.printStackTrace();
32. }
33. return null;
34. }
35. public static URL getLocalUrl() {//发布前获得eclipse所在安装路径，发布后是发布的绝对路径
36. Location location = Platform.getInstallLocation();
37. return location.getURL();
38. }
39. }

Lang 类 代码:

java 代码

 

01. import java.util.Properties;
02. import java.io.File;
03. import java.io.FileInputStream;
04. import java.net.URL;
05. import (包名).IFiles;
06. public class Lang {
07. private static Resources res = null;
08. private static String fPath = "";
09. private static String fCode = "";
10. public static Resources getinstance() {
11. if(res==null) {
12. getLangProps();
13. res = new Resources(fPath,fCode);
14. }
15. return res;
16. }
17. public static void delRes() {
18. Lang.res = null;
19. }
20. private static void getLangProps() {
21. Properties props = new Properties();
22. try {
23. URL url = FilePath.getLocalUrl();//这里用的是获得绝对路径，而不是相对于工程的路径
24. String path = url.getFile()+IFiles.SYS\_PROP\_FILE;
25. File file = new File(path);
26. FileInputStream input = new FileInputStream(file);
27. props.load(input);
28. fPath = props.getProperty("LANG\_FILE");
29. fCode = props.getProperty("LANG\_CODE");
30. }catch(Exception e) {
31. e.printStackTrace();
32. }
33. }
34. }

Resource 类 代码:

java 代码

 

01. import java.util.Properties;
02. import java.net.URL;
03. public class Resources {
04. private Properties prop;
05. private String code;
06. private String fPath;
07. public Resources(String fPath,String code) {
08. this.prop = new Properties();
09. this.fPath = fPath;
10. this.code = code;
11. init();
12. }
13. public void init() {
14. try {
15. fPath = fPath+"i18n\_"+code+".properties";
16. URL url = FilePath.getAbsUrl(fPath);//这里就获得是相对于工程的绝对路径。因为文件放在工程下了
17. prop.load(url.openConnection().getInputStream());
18. }catch(Exception e) {
19. e.printStackTrace();
20. }
21. }
22. public String get(String key) {
23. String value = "";
24. try {
25. if(prop.containsKey(key)) {
26. value = prop.getProperty(key);
27. return new String(value.getBytes("iso-8859-1"),code);
28. }
29. }catch(Exception e) {
30. return "!wrong!";
31. }
32. return value;
33. }
34. public Properties getProperties() {
35. return prop;
36. }
37. public void clear() {
38. prop.clear();
39. }
40. }

使用格式要简单的多，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 这样使用时可以只需要更换字符编码就可以，方便使用。