简体   繁体   English

Chrome extension how to change language?

[英]Chrome extension how to change language?

for example in my manifest.json there is a param:

  "default_locale": "en"

the question is how can I dynamically change the language in the javascript code?

can't find any examples on the docs about specific methods...

also my data is binded like this:

<div data-l10n-id="key_download"></div>

it is dynamically parsed in the html, but how can I change the language it chooses?

The point of the built-in chrome.i18n API is to make extensions match the UI language of the browser so there's no chrome API way to change it programmatically, you'll have to implement it yourself or use one of theexisting js libraries .内置 chrome.i18n API 的重点是使扩展与浏览器的 UI 语言相匹配,因此没有 chrome API 方式以编程方式更改它,您必须自己实现它或使用现有的 js 库之一。

Read the official chrome.i18n documentation for more details and examples of how to facilitate testing of your extension with different UI languages of the browser.阅读官方chrome.i18n 文档,了解更多详细信息和示例,了解如何使用浏览器的不同 UI 语言测试扩展程序。

you tagged you question as google chrome extension, but the API you use is from mozilla docs您将问题标记为 google chrome 扩展,但您使用的 API 来自 mozilla 文档

the system behind chrome.i18n API is that it relies on messages.json files for different languages chrome.i18n API背后的系统是它依赖于不同语言的messages.json文件
If developer implemented language support for particular language (added messages.json into locales folder) and that language is recognized by Chrome (browser language), Chrome will use that file.如果开发人员实现了对特定语言的语言支持(将 messages.json 添加到 locales 文件夹中)并且该语言被 Chrome 识别(浏览器语言),Chrome 将使用该文件。
That file is in form of key-value.该文件采用键值的形式。
It means that, for every key there is a value that will be used depending on browser language.这意味着,对于每个键,都有一个将根据浏览器语言使用的值。
String that you want to use, you simple describe with key (like in your example, but for Chrome is data-i18n )您要使用的 String ,您可以简单地用键描述(就像在您的示例中一样,但对于 Chrome 是data-i18n

 <div data-i18n="key_download"></div>

and in your messages.json files, add value to that key并在您的messages.json文件中,为该键添加值
default is English, this is what you defined in your manifest默认为英语,这是您在清单中定义的

"default_locale": "en"

so if there is no key in used messages, if will fallback to messages.json defined in en subfolder of your locales folder (and if that file also lacks that key...nothing, no string will be added)因此,如果已使用的消息中没有密钥,则将回退到 messages.json 在您的语言环境文件夹的en子文件夹中定义(如果该文件也缺少该密钥...什么都没有,则不会添加 string)

So, back to that key_downlaod所以,回到那个key_downlaod
in en subfolder, in your messages.json you will have:en子文件夹中,在您的 messages.json 中,您将拥有:

 { "key_downlaod ": { "message": "download" },............ }

and in your hr subfolder (example for Croatian), in messages.json it will be:在您的hr子文件夹(克罗地亚语的示例)中,在 messages.json 中它将是:

 { "key_downlaod ": { "message": "preuzmi" },............ }

it's easy when you can add those keys to HTML, but if you need it dynamically from javascript, collect all elements with data-i18n attribute and add text strings to them based on their key当您可以将这些键添加到 HTML 时很容易,但是如果您从 javascript 动态需要它,请收集所有具有data-i18n 属性的元素并根据它们的键向它们添加文本字符串

var i18n_elem = document.querySelectorAll('[data-i18n]'); for (var j=i18n_elem.length-1; j>=0; j--) i18n_elem[j].textContent = chrome.i18n.getMessage(i18n_elem[j].getAttribute('data-i18n'));

or, you can add text string directly to element (maybe in the moment of creation).或者,您可以将文本 string 直接添加到元素(可能在创建时)。 Example for button title:按钮标题示例:

 someElement.title = chrome.i18n.getMessage('key_open');

key_open is key from your messages file. key_open是消息文件中的密钥。 No need to add prefix key_ , I just add it for better understanding, you can call those keys whatever you like, but uniquely.无需添加前缀key_ ,我只是添加它以便更好地理解,您可以随意调用这些键,但要唯一。

another use case is when you need to change some elements (eg. width) for different languages另一个用例是当您需要为不同语言更改某些元素(例如宽度)时
get UI language and then do your job获得 UI 语言,然后做你的工作

var getLang = chrome.i18n.getUILanguage(); if (getLang === 'ru') { //whatever } else if (getLang === 'de') { //whatever }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM