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;
}
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>
<span onclick="showAsProperties()">
<input type=radio name="showType">资源文件格式
</span>
<span onclick="showAsText()">
<input type=radio name="showType">文本格式
</span>
<input type=button value="保存文件" onclick="saveFile()" id=buttonSave disabled>
<input type=button value="另存为文件" onclick="saveAs()" id=buttonSaveAs disabled>
</body>
</html>