Firefox 字符串资源:如何获取本地化的友好语言名称?

Firefox string resources: How to get localized friendly languages names?

我正在开发一个涉及各种语言和方言词典的 Firefox 扩展。我想向用户显示一个对话框,以 select 可用语言的拼写检查词典。 用户从 en-US、ar-EG、en-GB 等值 select 会很乏味,所以,我想显示本地化语言名称,就像 Firefox 在这个屏幕截图中所做的那样

这是我的阿拉伯语 Firefox 上的字典 selection 菜单,显示了 en-US 和 ar 两种语言的名称。

如何做这样的事情?

这很容易做到。我这里有一些非常基本的本地化模板:

https://github.com/Noitidart/l10n/tree/xhtml-xul

https://github.com/Noitidart/l10n/tree/properties

属性文件用于私有 js 文件本地化。 .dtd 用于 xul/xhtml/and 内联 javascript.

以下是我查找和使用这些字符串所做的工作:

1- 我下载了 whole Firefox source code 并提取了它。

2- 我在源文件中搜索了名称 "New Zealand"。这个唯一的国家名称应该只存在于我要查找的文件中。而不是使用英语、美国、阿拉伯语等通用术语

3- 搜索让我找到了两个有趣的文件:regionNames.properties 和 languageNames.properties。第一个有所有国家的名字,另一个有所有语言的名字。 Firefox 应该使用两组字符串来显示字典名称。

4- 我发现这两个文件可以从 URL chrome://global/locale/languageNames.properties 和 chrome://global/locale/regionNames.properties 中获取,所以,我使用 string bundle service and the string bundle 对象来加载和使用资源。

这是一个代码示例:

  • 在我的 main.js 文件的顶部:

    const {Cc, Ci, Cu}  = require("chrome"),
          stringBundles = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
    
  • 那我在另一个方法中有这段代码:

    let languagesNamesBundle = stringBundles.createBundle("chrome://global/locale/languageNames.properties"),
        countriesNames = stringBundles.createBundle("chrome://global/locale/regionNames.properties");
    
    console.log(languagesNamesBundle.GetStringFromName("ar")); // العربية Arabic
    console.log(languagesNamesBundle.GetStringFromName("af")); // الأفريكانية Afrikaans
    console.log(countriesNames.GetStringFromName("fj")); // فيجي Fiji
    console.log(countriesNames.GetStringFromName("cr")); // كوستا ريكا Costa Rica
    

这就是我想要的。现在我可以根据语言和国家名称组成字典名称。 我会在发布最终结果后更新答案以添加项目存储库的路径。