Java调用Native2ASCII来解析properties文件
2007-11-05 13:01
337 查看
近来在实施Liferay的时候发现Liferay找的这个翻译可真差劲,很多东西翻译的简直狗屁不通,比如把论坛话题翻译成“螺纹”,把virtual host翻译为“真正的主人”,或者就根本没有翻译。一看就是一个没有程序开发经验的人做的,可能还是个台湾人。
没办法只能自己替Liferay做汉化工作了。
Liferay的资源文件做的还是不错的,基本上界面里的所有消息都放到了资源文件中。具体中文的就是:portal-impl/content下面的Language_zh_CN.properties和Language_zh_CN.properties.native。前一个是unicode字符串,后一个是中文。先将哪个native文件内容翻译过来,再把汉字转化为unicode替换前一个文件就OK了。
可native文件中居然有3000多条,如果全手工通过native2asccii来转换的话,简直是恶梦。因为我是懒人嘛,当然要用懒办法。写了个Java程序调用native2ascii搞定。
代码如下:
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();
}
}
}
////////////////////////
后来发现其实上面的做法是脱了裤子放屁,native2ascii本身就能完美的完成这个工作。其详细用法如下:
native2ascii [options] [inputfile [outputfile]
在options中指定-encoding UTF8就OK了。
呵呵,不过上面程序也不能说完全没有用,权当作为一个用java调用操作系统进程的一个练习吧。
没办法只能自己替Liferay做汉化工作了。
Liferay的资源文件做的还是不错的,基本上界面里的所有消息都放到了资源文件中。具体中文的就是:portal-impl/content下面的Language_zh_CN.properties和Language_zh_CN.properties.native。前一个是unicode字符串,后一个是中文。先将哪个native文件内容翻译过来,再把汉字转化为unicode替换前一个文件就OK了。
可native文件中居然有3000多条,如果全手工通过native2asccii来转换的话,简直是恶梦。因为我是懒人嘛,当然要用懒办法。写了个Java程序调用native2ascii搞定。
代码如下:
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();
}
}
}
////////////////////////
后来发现其实上面的做法是脱了裤子放屁,native2ascii本身就能完美的完成这个工作。其详细用法如下:
native2ascii [options] [inputfile [outputfile]
在options中指定-encoding UTF8就OK了。
呵呵,不过上面程序也不能说完全没有用,权当作为一个用java调用操作系统进程的一个练习吧。
相关文章推荐
- properties文件调用方法-java
- 通过读取Properties文件,Java动态实例化和 调用set方法于实体
- java 解析properties文件的两种方法
- 普通Java工程中调用Hibernate的8个执行步骤(properties做配置文件)
- 用native2ascii将Java的Properties文件用UTF-8编码
- java调用文件(xx.properties)
- log4j.properties(java日志文件)语法语句解析,什么意思,使用方法举例
- java解析properties文件的几种方法及用法
- java 解析properties 文件
- Java解析Properties文件
- Java工具类---资源文件解析类 PropertiesUtil
- java 解析properties文件的两种方法
- Java解析多个ClassPath中的properties文件, 相同的属性将会覆盖之前的载入
- java解析properties文件
- java解析properties文件
- java解析properties文件的几种方法及用法
- java.util.ResourceBundle使用详解及如何调用properties文件
- java如何解析\*.properties文件
- 普通Java工程中调用Hibernate的8个执行步骤(properties做配置文件)
- java解析properties文件