Liferay的資源文件做的還是不錯(cuò)的,基本上界面里的所有消息都放到了資源文件中。具體中文的就是:portal-impl/content下面的Language_zh_CN.properties和Language_zh_CN.properties.native。前一個(gè)是unicode字符串,后一個(gè)是中文。先將哪個(gè)native文件內(nèi)容翻譯過來,再把漢字轉(zhuǎn)化為unicode替換前一個(gè)文件就OK了。
可native文件中居然有3000多條,如果全手工通過native2asccii來轉(zhuǎn)換的話,簡直是惡夢。因?yàn)槲沂菓腥寺,?dāng)然要用懶辦法。寫了個(gè)Java程序調(diào)用native2ascii搞定。
native2ascii這個(gè)工具主要用來把本地編碼(比如gbk)的文件轉(zhuǎn)換成標(biāo)準(zhǔn)的Properties屬性文件。
屬性文件中,除字母數(shù)字外的字符要用\轉(zhuǎn)義,具體的標(biāo)準(zhǔn)參考java文檔Properties類的說明。
那這個(gè)轉(zhuǎn)換的原理是什么呢?自己如何實(shí)現(xiàn)呢?
只能概括下原理。
替換掉本地編碼的文本文件中所有的非ascii字符:
比如漢字,先從本地編碼GBK轉(zhuǎn)換成對應(yīng)的unicode字符,
再把這個(gè)字符的字符碼,以\u21342的形式寫回。
char a='你';
System.out.println(a+" -> \\u"+(int)a);
===========
你 -> \u20320
(int)a 出來的結(jié)果不太對嘛!
你應(yīng)該對應(yīng)\u4f60
只是我忽略了進(jìn)制,要求是16進(jìn)制,我給的是10進(jìn)制。
"原理"是對的
你的基礎(chǔ)差到連一點(diǎn)變通都沒法嗎?
char a='你';
System.out.println(a+" -> \\u"+Integer.toHexString((int)a));
===========
你 -> \u4f60
首先要安裝JDK(不是jre),安裝好后將jdk的bin目錄添加到系統(tǒng)變量path中,然后就可以使用native2ascii命令在控制臺(tái)(cmd)中進(jìn)行轉(zhuǎn)碼了
native2ascii a.properties b.properties
當(dāng)前目錄下要有a.properties這個(gè)文件,如果沒有就要寫全路徑
如果你用eclipse做開發(fā)工具,還是下載一個(gè)propedit的插件吧
直接打開運(yùn)行,輸入cmd,就可以轉(zhuǎn)碼了,如圖
代碼如下:
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)其實(shí)上面的做法是脫了褲子放屁,native2ascii本身就能完美的完成這個(gè)工作。其詳細(xì)用法如下:
native2ascii [options] [inputfile [outputfile]
在options中指定-encoding UTF8就OK了。
呵呵,不過上面程序也不能說完全沒有用,權(quán)當(dāng)作為一個(gè)用java調(diào)用操作系統(tǒng)進(jìn)程的一個(gè)練習(xí)吧。