| 如何对包含在 .js 文件中的字符串进行本地化? |
|
|
| Written by Goofy | |||||
| Saturday, 26 January 2008 | |||||
如何对包含在 .js 文件中的字符串进行本地化? 我们有时候会遇到这种情况:扩展的大部分字符串已包含在本地化了的语言文件夹中, 但仍有少量的词句还未本地化。由于它们是存在于 .js 文件中,因此这些词句无法像平常那样通过添加 ENTITY 行进行翻译。 |
|||||
| 假定您在某个 .js 文件中发现以下字符串: if ( password == userPassword ) { oPrefs.setBoolPref("access.authenticated", true); } else { alert ("Invalid password"); ...... function clear() { sure = confirm("Are you sure?"); |
首先,您需要在语言文件夹里找到或创建一个 myextension.properties 文件。 接着可以输入您设定的变量名以及要显示在最终用户面前的词句。 例如: WrongPassMessage=Invalid password AreYouSureMessage=Are you sure? (这里 Invalid password 和 Are you sure? 就是显示在最终用户面前的字符串) |
| 现在返回到刚才需要翻译的字符串所在的 .js 文件,在文件的开头加入如下两句: var gBundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService); var mystrings = gBundle.createBundle("chrome://myextension/locale/myextension.properties"); 紧接着声明变量,用于提取 .properties 文件中的变量名到 .js 文件中: var wrongpassalert = mystrings.GetStringFromName("WrongPassMessage"); var confirmmessage=mystrings.GetStringFromName("AreYouSureMessage"); |
| 现在您就可以将 .js 中要翻译的词句替换为之前声明的变量名,例如: if ( password == userPassword ) { oPrefs.setBoolPref("access.authenticated", true); } else { alert (wrongpassalert); function clear() { sure = confirm(confirmmessage); |






