我正在构建一个FF扩展,我正在为自己处理一些xhtml,以支持子表单加载,因此我必须识别定义了l10n属性的元素,并将它们添加到字符串值中。由于l10n不能从主代码共享到内容脚本(因为它不是一个简单的JSON对象),所以我通过获取加载的键值并定义“本地化数组包”来管理这种情况,如下所示:
lStrings = ["step_title", ........ ];
for (var i = 0; i < lStrings.length; i++) {
bundle[lStrings[i]] = this.locale(lStrings[i]);
} 问题是,我必须在这里写.properties文件中的每一个条目.那么,您知道如何访问这个键值吗?我已经尝试过使用.toString .toLocalString并检查对象,但是找不到对象返回所有键集合的方式。
你有什么更好的改进方法吗?
发布于 2015-07-17 02:50:20
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的知识。
https://stackoverflow.com/questions/31461676
复制相似问题