如何使用Chrome.i18n Web扩展API检索消息的内容。

发布于 2025-02-12 08:49:11 字数 641 浏览 1 评论 0原文

使用Chrome的Chrome.i18n API时,chrome.i18n.getMessage方法一次仅检索一条消息。

const buttonText = chrome.i18n.getMessage('buttonText');

我想要的是在弹出窗口打开message.json文件的本地内容时更新弹出式UI。这样做需要我知道使用的消息名称。是否有类似于getMessage的方法/技术,用于一次检索Message.json文件的所有内容?

现在,我只能将所有消息名称保留在某个地方,并单独检索每个本地化消息,例如:


const messages = ["buttonText", "buttonTitle"];

messages.forEach((message) => {
  const messageText = chrome.i18n.getMessage(message);
  // Update UI
});

When using chrome's chrome.i18n API, the chrome.i18n.getMessage method retrieves only one message at a time.

const buttonText = chrome.i18n.getMessage('buttonText');

What I want is to update the popup UI when the popup opens with the localized contents of message.json file. Doing so requires me to know the message names used. Is there a method/technique similar to getMessage for retrieving all the contents of the message.json file at once?

Right now, I can only keep all the message names somewhere and retrieve each localized message individually like:


const messages = ["buttonText", "buttonTitle"];

messages.forEach((message) => {
  const messageText = chrome.i18n.getMessage(message);
  // Update UI
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2025-02-19 08:49:11

要获取“ messages.json”文件,您必须使用“ fetch”并使用” chrome.runtime“ API和“ geturl”方法

fetch(chrome.runtime.getURL(`_locales/en/messages.json`))

唯一的事情是,以这种方式,您必须自己指定语言并验证它,这就是我的做法:

let lang = 'en';
if (["es"].includes(chrome.i18n.getUILanguage().split('-')[0])) {
    lang = chrome.i18n.getUILanguage().split('-')[0];
}
fetch(chrome.runtime.getURL(`_locales/${lang}/popup_content.json`))

检测浏览器的语言,并验证它是否存在于我的扩展中,除了默认语言外,在这种情况下,在这种情况下是英语,如果数组中不存在它,则不会修改并默认保留。

我的“ _locales”目录:
_locales-> en->消息
_locales-> es->消息

最终,要迭代“ message.json”的结果,因为它是一个对象,我找到了这些方法:

for (let key in obj) {
    console.log(obj[key]);
}

Object.keys(obj).forEach(key => {
    console.log(obj[key]);
});

两种方式迭代并返回给定对象的键。更多信息:与对象一起工作

最终代码:这将返回JSON主对象中每个对象的“消息”键的每个值。

let lang = 'en';
if (["es"].includes(chrome.i18n.getUILanguage().split('-')[0])) {
    lang = chrome.i18n.getUILanguage().split('-')[0];
}
fetch(chrome.runtime.getURL(`_locales/${lang}/messages.json`)).then(res=> {
    res.json().then(json=> {
        Object.keys(json).forEach(key => {
            console.log(json[key].message);
        });
    });
});

文件“ _locales - > en - > messages.json”:

{
  "appName": {
    "message": "Name of my extension"
  },
  "appDesc": {
    "message": "Awesome extension."
  }
}

控制台中的结果将是:

我的扩展名称

很棒的扩展。

Sorry if the wording is not correct or I am very repetitive, I had to use the translator.

To get the "messages.json" file you would have to use "fetch" and access the URL of the file with the "chrome.runtime" API and the "getURL" method.

fetch(chrome.runtime.getURL(`_locales/en/messages.json`))

The only thing is that in this way you have to specify the language yourself and verify it, that's how I did it:

let lang = 'en';
if (["es"].includes(chrome.i18n.getUILanguage().split('-')[0])) {
    lang = chrome.i18n.getUILanguage().split('-')[0];
}
fetch(chrome.runtime.getURL(`_locales/${lang}/popup_content.json`))

Detecting the language of the browser and verifying if it exists in my extension apart from the default language, in this case English, if it does not exist in the array it is not modified and remains by default.

My "_locales" directory:
_locales -> en -> messages.json
_locales -> es -> messages.json

Finally, to iterate the result of "messages.json", as it is an object, I found these ways:

for (let key in obj) {
    console.log(obj[key]);
}

Object.keys(obj).forEach(key => {
    console.log(obj[key]);
});

Both ways iterate and return the keys of the given object. more info: Working with objects.

Final code: this will return each value of the "message" key of each object within the main object of the json.

let lang = 'en';
if (["es"].includes(chrome.i18n.getUILanguage().split('-')[0])) {
    lang = chrome.i18n.getUILanguage().split('-')[0];
}
fetch(chrome.runtime.getURL(`_locales/${lang}/messages.json`)).then(res=> {
    res.json().then(json=> {
        Object.keys(json).forEach(key => {
            console.log(json[key].message);
        });
    });
});

The file "_locales -> en -> messages.json":

{
  "appName": {
    "message": "Name of my extension"
  },
  "appDesc": {
    "message": "Awesome extension."
  }
}

The result in the console will be something like:

Name of my extension

Awesome extension.

Sorry if the wording is not correct or I am very repetitive, I had to use the translator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文