Liferay的資源文件做的還是不錯的,基本上界面里的所有消息都放到了資源文件中。具體中文的就是:portal-impl/content下面的Language_zh_CN.properties和Language_zh_CN.properties.native。前一個是unicode字符串,后一個是中文。先將哪個native文件內容翻譯過來,再把漢字轉化為unicode替換前一個文件就OK了。
可native文件中居然有3000多條,如果全手工通過native2asccii來轉換的話,簡直是惡夢。因為我是懶人嘛,當然要用懶辦法。寫了個Java程序調用native2ascii搞定。
native2ascii這個工具主要用來把本地編碼(比如gbk)的文件轉換成標準的Properties屬性文件。
屬性文件中,除字母數(shù)字外的字符要用\轉義,具體的標準參考java文檔Properties類的說明。
那這個轉換的原理是什么呢?自己如何實現(xiàn)呢?
只能概括下原理。
替換掉本地編碼的文本文件中所有的非ascii字符:
比如漢字,先從本地編碼GBK轉換成對應的unicode字符,
再把這個字符的字符碼,以\u21342的形式寫回。
char a='你';
System.out.println(a+" -> \\u"+(int)a);
===========
你 -> \u20320
(int)a 出來的結果不太對嘛!
你應該對應\u4f60
只是我忽略了進制,要求是16進制,我給的是10進制。
"原理"是對的
你的基礎差到連一點變通都沒法嗎?
char a='你';
System.out.println(a+" -> \\u"+Integer.toHexString((int)a));
===========
你 -> \u4f60
首先要安裝JDK(不是jre),安裝好后將jdk的bin目錄添加到系統(tǒng)變量path中,然后就可以使用native2ascii命令在控制臺(cmd)中進行轉碼了
native2ascii a.properties b.properties
當前目錄下要有a.properties這個文件,如果沒有就要寫全路徑
如果你用eclipse做開發(fā)工具,還是下載一個propedit的插件吧
直接打開運行,輸入cmd,就可以轉碼了,如圖
代碼如下:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Properties;
/**
* @author smilingleo E-mail:liuwei.dt@gmail.com
* @version created time:2007-11-2 上午11:37:55 類說明:
*/
public class Native2Ascii {
static String java_bin_path = "C:/Java/jdk1.5/bin";
public Native2Ascii() {
}
public Properties getProperties(String filename) throws IOException {
Properties p = new Properties();
ClassLoader cl = this.getClass().getClassLoader();
FileInputStream input;
input = new FileInputStream(filename);
p.load(input);
return p;
}
public String getUnicodeString(String value) {
StringBuffer tempSb = new StringBuffer();
try {
Process pro = Runtime.getRuntime().exec(
java_bin_path + "/native2ascii.exe ");
OutputStream out = pro.getOutputStream();
out.write(value.getBytes());
out.flush();
out.close();
InputStreamReader child_in = new InputStreamReader(pro.getInputStream());
int c;
while ((c = child_in.read()) != -1) {
tempSb.append((char) c);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return tempSb.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String sourceFile = "Language_zh_CN.properties";
String targetFile = "target.properties";
if (args.length != 2) {
System.out.println("Usage: java Native2Ascii <source properties filename> <target filename>" +
" Author:Smilingleo" +
" Blog:blog.csdn.net/smilingleo");
// System.exit(0);
}else{
sourceFile = args[0];
targetFile = args[1];
}
Native2Ascii parser = new Native2Ascii();
StringBuffer sb = new StringBuffer();
try {
//Convert the source file into unicode first.
Properties p = parser.getProperties(sourceFile);
Iterator iterator = p.keySet().iterator();
while (iterator.hasNext()){
Object key = iterator.next();
String value = p.get(key).toString();
value= new String(value.getBytes("ISO-8859-1"),"UTF-8");
value = parser.getUnicodeString(value);
// System.out.println(key + ":" + value);
p.setProperty(key.toString(), value);
sb.append(key.toString() + "=" + value);
}
//write the target file.
FileWriter out = new FileWriter(targetFile);
out.write(sb.toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////
后來發(fā)現(xiàn)其實上面的做法是脫了褲子放屁,native2ascii本身就能完美的完成這個工作。其詳細用法如下:
native2ascii [options] [inputfile [outputfile]
在options中指定-encoding UTF8就OK了。
呵呵,不過上面程序也不能說完全沒有用,權當作為一個用java調用操作系統(tǒng)進程的一個練習吧。