我可以从我的Chrome扩展程序中获得可用区域设置翻译的列表吗? [英] Can I get a list of available locale translations from my Chrome extension?

查看:149
本文介绍了我可以从我的Chrome扩展程序中获得可用区域设置翻译的列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从我的Google Chrome浏览器扩展程序中检索所有可用翻译的列表?

Is there a way to retrieve from within my Google Chrome extension the list of all available translations?

例如,我的应用程序可能包含以下文件夹:

For instance, my app may contain the following folders:

_locales\en\messages.json
_locales\fr\messages.json
_locales\es\messages.json

有没有办法知道它是 en fr es 是否来自扩展名本身?

Is there a way to know that it's en, fr, and es from within the extension itself?

第二个问题,有没有办法将特定的 messages.json 文件解析为JSON数据?我的意思是比 chrome.i18n.getMessage()提供的功能稍多一些。

And a second question, is there any way to parse a specific messages.json file as the JSON data? I mean a little bit more capabilities than what's provided by chrome.i18n.getMessage().

推荐答案


Yes to both questions, thanks to the ability to read the extension's own folder:

https://developer.chrome.com/extensions/runtime#method-getPackageDirectoryEntryrel =nofollow noreferrer> chrome.runtime.getPackageDirectoryEntry(function callback)

chrome.runtime.getPackageDirectoryEntry(function callback)

返回包目录的 DirectoryEntry

例如,您可以以这种方式列出语言环境(不具有弹性,添加您自己的错误检查):

For example, you can list locales in this way (not resilient, add your own error checks):

function getLocales(callback) {
  chrome.runtime.getPackageDirectoryEntry(function(root) {
    root.getDirectory("_locales", {create: false}, function(localesdir) {
      var reader = localesdir.createReader();
      // Assumes that there are fewer than 100 locales; otherwise see DirectoryReader docs
      reader.readEntries(function(results) {
        callback(results.map(function(de){return de.name;}).sort());
      });
    });
  });
}

getLocales(function(data){console.log(data);});

同样,您可以使用它获取messages.json文件的FileEntry并解析它。
编辑:或者您可以使用XHR,如 Marco的回答中所述,一旦您知道文件夹名称。

Likewise, you can use this to obtain a FileEntry for the messages.json file and parse it.
or you can use XHR as described in Marco's answer once you know the folder name.

这篇关于我可以从我的Chrome扩展程序中获得可用区域设置翻译的列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆