Localizing a Google Chrome Web App

自作多情 提交于 2020-01-05 05:21:25

问题


I'm trying to add localization support to a Google Chrome Web App and, while it is easy to define strings for manifest and CSS files, it is somewhat more difficult for HTML pages.

In the manifest and in CSS files I can simply define localization strings like so:

__MSG_name__

but this doesn't work with HTML pages.

I can make a JavaScript function to fire onload that does the job like so:

document.title = chrome.i18n.getMessage("name");
document.querySelector("span.name").innerHTML = chrome.i18n.getMessage("name");

but this seems awfully ineffecient. Furthermore, I would like to be able to specify the page metadata; application-name and description, pulling the values from the localization files. What would be the best way of doing all this?

Thanks for your help.


回答1:


Please refer to this documentation: http://code.google.com/chrome/extensions/i18n.html

If you want to add localized content within HTML, you would need to do it via JavaScript as you mentioned before. That is the only way you can do it.

chrome.i18n.getMessage("name")

It isn't inefficient to do that, you can place your JavaScript at the end of the document (right before the end body tag) and it will fill up the text with respect to the locale.




回答2:


Dunno if i understand exactly what you are trying to do but you could dynamically retrieve the LANG attribute (using .getAttribute("lang") or .lang) of the targeted tag and serve accordingly the proper values.



来源:https://stackoverflow.com/questions/4290135/localizing-a-google-chrome-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!