emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

          java的資源文件一個設計目的就是方便的提供多語言支持,可是它本身對unicode的支持是十分搞笑的。很多人見到這樣的資源文件都會覺得很熟悉吧:

          tongren.oa.system.user.userNameExists=\u7528\u6237\u540D\u5DF2\u7ECF\u5B58\u5728\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\u4E00\u4E2A\u7528\u6237\u540D
          tongren.oa.system.role.roleNameExists=\u89D2\u8272\u540D\u5DF2\u7ECF\u5B58\u5728\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\u4E00\u4E2A\u89D2\u8272\u540D
          tongren.oa.system.role.UserAssignedToRole=\u6307\u5B9A\u89D2\u8272\u4E0D\u80FD\u5220\u9664\uFF0C\u56E0\u4E3A\u7528\u6237{0}\u62E5\u6709\u8FD9\u4E2A\u89D2\u8272

          我的天,這要怎么讀?還要用工具把它轉回unicode才能看到漢字,修改完了還要轉回去?


          昨天下午抽了一點時間寫了一個hta工具來編輯資源文件,現在終于可以直接修改,自動轉換保存了。直接把下面的代碼保存為一個后綴為hta的文件即可:

          <html>
          <head>
          <title></title>
          </head>
          <body>
          <input type=file onchange="getFile()" id=fileSelector>
          <SCRIPT LANGUAGE="JavaScript">
          <!--
          var fso;
          var OpenFileForReading = 1
          var OpenFileForWriting = 2
          var OpenFileForAppending = 8
          var srcFilePath="";
          fso = new ActiveXObject("Scripting.FileSystemObject");

          function getFile(){
           if (!fso.FileExists(fileSelector.value)){
            alert("指定文件不存在或已經被移除");
            event.returnValue=false;
            return;
           }
           srcFilePath = fileSelector.value;
           var textStream = fso.OpentextFile(srcFilePath, OpenFileForReading,true);
           viewer.value = textStream.ReadAll();
           textStream.Close();
           document.getElementsByName("showType")[0].checked=true;
           buttonSaveAs.disabled=false;
           buttonSave.disabled=false;
           showAsText();
          }
          function showAsText(){
           document.getElementsByName("showType")[1].checked=true;
           viewer.value = unescape(viewer.value.replace(/\\u/g,"%u"))
          }
          function showAsProperties(){
           document.getElementsByName("showType")[0].checked=true;
           viewer.value = unescape(escape(viewer.value).replace(/%u/g,"\\u"))
          }
          function saveFile(){
           showAsProperties();
           var fileStillExists = fso.FileExists(srcFilePath);
           if (fileStillExists)
            fso.CopyFile (srcFilePath,fileSelector.value+".bak");
           var textStream = fso.OpentextFile(srcFilePath, OpenFileForWriting,true);
           textStream.Write(viewer.value);
           textStream.Close();
           alert("資源文件已經成功保存"+(fileStillExists?(",原來的文件備份為\n"+srcFilePath+".bak"):"!"));
          }
          function saveAs(){
           showAsProperties();
           var srcFileName = srcFilePath.substr(srcFilePath.lastIndexOf("\\")+1);
           var newFileName = prompt("請輸入新文件名",srcFileName);
           var newFilePath = srcFilePath.replace(srcFileName,newFileName);
           if (srcFileName == newFileName){
            if (confirm("您確認要覆蓋原來的文件?")) saveFile();
           }else if (fso.FileExists(newFilePath)){
            if (confirm("您確認要覆蓋文件"+newFilePath+"?")) {
             var textStream = fso.OpentextFile(newFilePath, OpenFileForWriting,true);
             textStream.Write(viewer.value);
             textStream.Close();
             srcFilePath = newFilePath;
             alert("資源文件已經成功保存")
            }
           }else {
            var textStream = fso.OpentextFile(newFilePath, OpenFileForWriting,true);
            textStream.Write(viewer.value);
            textStream.Close();
            fileSelector = fileSelector.value;
            srcFilePath = newFilePath;
            alert("資源文件已經成功保存")
           }
          }
          setInterval("document.title='當前文件:'+srcFilePath",500);
          //-->
          </SCRIPT>
          <textarea id=viewer style="width:90%;height:90%"></textarea>
          <BR>

          <input type=radio name="showType" id="asProperties" onclick="showAsProperties()">
          <label for="asProperties">資源文件格式</label>

          <input type=radio name="showType" id="asText" onclick="showAsText()">
          <label for="asText">文本格式</label>
          <input type=button value="保存文件" onclick="saveFile()" id=buttonSave disabled>
          <input type=button value="另存為文件" onclick="saveAs()" id=buttonSaveAs disabled>
          </body>
          </html>


           

          kukoo 發表于2004-12-24 4:50 PM  
          Looks nice, but 為什么不用JDK自己的native2ascii來轉換?


          emu 發表于2004-12-27 12:17 PM  
          因為native2ascii只能單向轉換,又沒有可視化編輯功能,你要維護一個原始版本,每次修改原始版本后重新native2ascii ...


          native2ascii是雙向的。 發表于2004-12-29 6:10 PM  
          Usage: native2ascii [-reverse] [-encoding encoding] [inputfile [outputfile]]


          是雙向的 發表于2004-12-29 6:10 PM  
          Native2Ascii是雙向的!

          Usage: native2ascii [-reverse] [-encoding encoding] [inputfile [outputfile]]


          emu 發表于2004-12-30 12:08 PM  
          哈哈,原來可以轉來轉去的啊

           

          posted on 2005-05-18 16:07 emu 閱讀(696) 評論(0)  編輯  收藏 所屬分類: java技術
          主站蜘蛛池模板: 丹凤县| 开原市| 汕头市| 长武县| 屏山县| 营口市| 德钦县| 松滋市| 湘西| 房产| 自贡市| 团风县| 桃源县| 加查县| 布尔津县| 云安县| 开阳县| 庆阳市| 通海县| 修水县| 洞头县| 金华市| 海原县| 鞍山市| 葵青区| 饶平县| 沐川县| 龙里县| 辽源市| 武汉市| 达拉特旗| 通河县| 根河市| 大埔区| 冷水江市| 重庆市| 霍林郭勒市| 淳安县| 禹城市| 贵南县| 阿瓦提县|