使用native2ascii進行編碼轉換

在項目開發中,由於涉及到國際化,所以採用該工具進行轉換,將中文字符轉為unicode的ascii形式.
項目開發平台是繁體2k系統,

簡單例子,msg.txt原始內容為簡體中文(即,msg.txt中是big5編碼)
1.所以得到繁體字的編碼可以直接運行:
native2ascii msg.txt msg_zh_TW.properties
得到編碼為 \u7c21\u9ad4\u4e2d\u6587(在頁面顯示正常)

2.我用其他工具將msg.txt轉為簡體gbk編碼後,這時打開該文件,是一些不認識的中文字行(潠极笢恅),
換掉文件後綴為htm,打開後顯示為正常的簡體字.
當直接使用
native2ascii msg.txt msg_zh_TW.properties
其結果編碼為 \u6f60\u6781\u7b22\u6045(在頁面顯示為,潠极笢恅)
採用以下命令即正常
native2ascii -encoding gbk msg.txt msg_zh_TW.properties
得到結果 \u7b80\u4f53\u4e2d\u6587(在頁面顯示正常)



當文件比較多時,可以寫成ant任務,自動批次處理
 <target name="gbk2ascii" depends="clean_gbk" description="Native to ascii for gbk properties(*zh_CN.properties)">
  <native2ascii encoding="GBK" src="${project.web.prop.dir}"
   dest="${project.web.prop.dir}" includes="**/*zh_CN.properties_native"
   ext=".properties"/>
 </target>

 <target name="big2ascii" depends="clean_big" description="Native to ascii for big properties(*zh_TW.properties)">
  <native2ascii encoding="BIG5" src="${project.web.prop.dir}"
   dest="${project.web.prop.dir}" includes="**/*zh_TW.properties_native"
   ext=".properties" />
 </target>

原文地址:https://www.cnblogs.com/oisiv/p/437256.html