native2ascii & ascii2native

在开发时,经常会用到多语言支持的问题,常用的解决方案是将各个语言文字放到properties文件中,但中文是需要转为ascii的,一般来说常用的解决方案有以下几种:

一.jdk的native2ascii

举例:

1.转换一行文字

进入jdk的bin目录,双击native2ascii.exe,输入“你好”,【回车】,屏幕上显示\u4f60\u597d

2.转换一个文件

cmd命令行中执行(注意配置好path环境变量)
d:\>native2acsii aa.properties bb.properties
如此一来就aa.properties 中的中文字符转换后保存进bb.properties 文件中。注意如果其中含有英文字符,则原样输出。

二.ant

    可以一次就转换n多个文件

<target name="i18n">   
          <native2ascii encoding="GBK" src="${src.dir}" dest="${build.dir}" includes="*.properties" />  
</target>  
三.eclipse插件

如果是MyEclipse,安装插件方式如下:

安装步骤:
1、展开Eclipse的Help菜单,将鼠标移到Software Update子项,在出现的子菜单中点击Find and Install;
2、在Install/Update对话框中选择Search for new features to install,点击Next;
3、在Install对话框中点击New Remote Site;
4、在New Update Site对话框的Name填入任意字符串中文也可以,在URL中填入http://propedit.sourceforge.jp/eclipse/updates/;然后可能需要先点击finish,不过依然会出现后面的部分(根据版本的不同而定)
5、在Site to include to search列表中,除上一步加入的site外的其它选项去掉,点击Finsih;
6、在弹出的Updates对话框中的Select the features to install列表中将所有结尾为“3.1.x”的选项去掉(适用于Eclipse 3.2版本的朋友);
7、点击Finish关闭对话框;
8、在下载后,同意安装,再按提示重启Eclipse,在工具条看到形似vi的按钮表示安装成功,插件可用。此时,Eclpise中所有properties文件的文件名前有绿色的P的图标作为标识。

9、properties文件使用PropertiesEditor(右键,openwith,一般来说安装插件后默认就会使用这个编辑器)打开,输入中文,编辑器会自动将其转换为ascii码

如果是Eclipse,展开help,选择Install new software,add,输入地址,确定后eclipse会进行自动搜索,不过安装插件时会出现无法安装的情况,此时可以下载我提供的安装包,

下载地址 :http://download.csdn.net/source/1488797

可以直接将插件包解压后的features和plugins两个文件夹复制到eclipse的home目录下,重启eclipse即可。

ps:个人感觉插件比较方便,当然你要使用eclipse。

另外提供一个将ascii码转换为中文的java代码。

package cn.jit.text.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ascii2Native {
  public static void main(String[] args) {
         File f=new File("E:\\Workspaces\\TextChange\\text\\application.properties");
             if (f.exists() && f.isFile()) {
                 // convert param-file
                 BufferedReader br = null;
                 String line;

                 try {
                     br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "JISAutoDetect"));

                     while ((line = br.readLine()) != null) {
                         System.out.println(ascii2native(line));
                     }
                 } catch (FileNotFoundException e) {
                     System.err.println("file not found - " + f);
                 } catch (IOException e) {
                     System.err.println("read error - " + f);
                 } finally {
                     try {
                         if (br != null)
                             br.close();
                     } catch (Exception e) {
                     }
                 }
             } else {
             // // convert param-data
             // System.out.print(ascii2native(args[i]));
             // if (i + 1 < args.length)
              System.out.print("wen");
             }
     }

     /**
     * core routine
     */
     public static String ascii2native(String str) {
         String hex = "0123456789ABCDEF";
         StringBuffer buf = new StringBuffer();
         int ptn = 0;

         for (int i = 0; i < str.length(); i++) {
             char c = str.charAt(i);
             if (c == '\\' && i + 1 <= str.length() && str.charAt(i + 1) == '\\') {
                 buf.append("\\\\");
                 i += 1;
             } else if (c == '\\' && i + 6 <= str.length() && str.charAt(i + 1) == 'u') {
                 String sub = str.substring(i + 2, i + 6).toUpperCase();
                 int i0 = hex.indexOf(sub.charAt(0));
                 int i1 = hex.indexOf(sub.charAt(1));
                 int i2 = hex.indexOf(sub.charAt(2));
                 int i3 = hex.indexOf(sub.charAt(3));

                 if (i0 < 0 || i1 < 0 || i2 < 0 || i3 < 0) {
                     buf.append("\\u");
                     i += 1;
                 } else {
                     byte[] data = new byte[2];
                     data[0] = i2b(i1 + i0 * 16);
                     data[1] = i2b(i3 + i2 * 16);
                     try {
                         buf.append(new String(data, "UTF-16BE").toString());
                     } catch (Exception ex) {
                         buf.append("\\u" + sub);
                     }
                     i += 5;
                 }
                 //怎加了对标点符号!判断,对其他标点没测试过
             }else if(c=='\\' && i + 1 <= str.length() && str.charAt(i + 1) == '!'  ){
               buf.append("!");
               i += 1;
             }
             else {
                 buf.append(c);
             }
         }

         return buf.toString();
     }

        /**
     * unsigned integer to binary
     * <P>
     * 
     * @param i
     *            unsigned integer
     * @return binary
     */
     private static byte i2b(int i) {
         return (byte) ((i > 127) ? i - 256 : i);
     }
}
原文地址:https://www.cnblogs.com/chinareny2k/p/1646337.html