获取存储在本地化包中的所有 l10n 值

Getting all l10n values stored in localized bundle

我正在构建一个 FF 扩展,我正在为自己处理一些 xhtml 以支持子表单加载,因此我必须识别定义了 l10n 属性的元素并将它们添加到字符串值。因为 l10n 不能从主代码共享到内容脚本(因为它不是一个简单的 JSON 对象),我通过获取加载的键值并定义一个 "localized array bundle" 来处理这种情况,就像这样:

lStrings = ["step_title", ........ ];
for (var i = 0; i < lStrings.length; i++) {
    bundle[lStrings[i]] = this.locale(lStrings[i]);
} 

问题是,我必须在这里写下 .properties 文件中的每个条目...那么,您知道如何访问这个键值吗?我已经尝试使用 .toString .toLocalString 并检查对象,但找不到对象能够返回所有密钥集合的方式。

您有更好的改进想法吗?

    var yourStringBundle = Services.strings.createBundle('chrome://blah@jetpack/content/bootstrap.properties?' + Math.random()); /* Randomize URI to work around bug 719376 */

var props = yourStringBundle.getSimpleEnumeration();
// MDN says getSimpleEnumeration returns nsIPropertyElement // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIStringBundle#getSimpleEnumeration%28%29

while (props.hasMoreElements()) {
  var prop = props.getNext();
  // doing console.log(prop) says its an XPCWrappedObject but we see QueryInterface (QI), so let's try QI'ing to nsiPropertyElement

  var propEl = prop.QueryInterface(Ci.nsIPropertyElement);
  // doing console.log(propEl) shows the object has some fields that interest us

  var key = propEl.key;
  var str = propEl.value;

  console.info(key, str); // there you go
}

看评论学习。很好的问题。我从回复中了解了更多关于 QI 的信息。