Chrome扩展中的本地化 [英] localization in Chrome extensions

查看:151
本文介绍了Chrome扩展中的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Chrome扩展的本地化相关的查询。我正在尝试本地化我的扩展程序中弹出式HTML文件中的按钮中的文本。我使用的代码如下:

 < button name =buttonSaveid =buttonSavestyle =width :100px; float:right;> 
< label class =buttonTextStyle>

< / label>
< / button>

javascript代码如下:

  document.getElementById(buttonSave)。innerText = chrome.i18n.getMessage(SaveButton); 

我曾尝试使用innerText,innerHTML,但该按钮中的文本未应用。我在这里做错了什么?

解决方案

我只是使用< input type =button > 并设置按钮的属性:

 < input type =buttonname =buttonSaveid =buttonSavestyle =width:100px; float:right;> 

....

document.getElementById(buttonSave)。value = chrome.i18n.getMessage(SaveButton);

顺便说一句,我相当确定有一个< label>嵌套在< button> 内的不是有效的HTML。改用< div> < span>

编辑

我刚做了一个测试,并使用 .innerHTML .innerText 应该可以正常工作。您确定:


  1. chrome.i18n.getMessage(SaveButton)的返回值非空?

  2. (即, document.getElementById(buttonSave)不为空或抛出错误)?


另外,要明白覆盖 innerText innerHTML 会破坏已经在按钮内的元素。也许你真的想做 document.querySelector(#buttonSave .buttonTextStyle)。innerText 来编写嵌套在按钮中的元素?


I have a query related to localization of Chrome extensions. I am trying to localize the text in a button in a popup html file in my extension. The code I am using is as follows:

<button name="buttonSave" id="buttonSave" style="width: 100px; float: right;">
       <label class="buttonTextStyle">

       </label>
</button>

The javascript code is as follows:

document.getElementById("buttonSave").innerText = chrome.i18n.getMessage("SaveButton");

I have tried using innerText, innerHTML, value but the text in the button is not applied. Am I doing something wrong here?

解决方案

I would just use <input type="button"> and set the value property of the button:

<input type="button" name="buttonSave" id="buttonSave" style="width: 100px; float: right;">

....

document.getElementById("buttonSave").value = chrome.i18n.getMessage("SaveButton");

By the way, I'm fairly certain having a <label> nested inside of a <button> is not valid HTML. Use a <div> or <span> instead.

EDIT:

I just did a test, and using either .innerHTML or .innerText should work fine. Are you sure that:

  1. the return value of chrome.i18n.getMessage("SaveButton") is non-empty?

  2. you are running document.getElementById("buttonSave") after the DOM is built (i.e., document.getElementById("buttonSave") is not null or throwing an error)?

Also, understand that overwriting the innerText or innerHTML of the button will destroy the elements you have inside the button already. Perhaps you actually want to do document.querySelector("#buttonSave .buttonTextStyle").innerText to write the the element nested inside the button?

这篇关于Chrome扩展中的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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