您的位置:首页 > 编程语言 > Java开发

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息