Java调用Native2ASCII来解析properties文件

近来在实施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调用操作系统进程的一个练习吧。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值