============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================
这一节我们介绍一下chorme App本地化问题--i18n。
在实现国际化,我们需要添加对应的国际化文件,最终整个文件结构图如下
新增了_locales文件夹存放多语言文件,messages.json存放对应key-value多语言文字信息。
我们过去的文字调用方法如下:
引入国际化后,调用方法如下:
那么怎么调用呢?”在manifest.json和css中,使用 “__MSG_messagename__”来调用相应的文字,如“__MSG_chrome_extension_name__”。
在js中,使用如下方法来调用
var message = chrome.i18n.getMessage("click_here", ["string1", "string2"]);//这里知多可以使用9个占位符
对于某些系统信息,chrome提供了相应的常亮,如 @@bidi_dir and @@ui_locale,详情如下:
Message name | Description |
---|---|
@@extension_id | The extension ID; you might use this string to construct URLs for resources inside the extension. Even unlocalized extensions can use this message. Note: You can't use this message in a manifest file. |
@@ui_locale | The current locale; you might use this string to construct locale-specific URLs. |
@@bidi_dir | The text direction for the current locale, either "ltr" for left-to-right languages such as English or "rtl" for right-to-left languages such as Japanese. |
@@bidi_reversed_dir | If the @@bidi_dir is "ltr", then this is "rtl"; otherwise, it's "ltr". |
@@bidi_start_edge | If the @@bidi_dir is "ltr", then this is "left"; otherwise, it's "right". |
@@bidi_end_edge | If the @@bidi_dir is "ltr", then this is "right"; otherwise, it's "left". |
{
"search_string": {
"message": "hello%20world", //具体的显示文字
"description": "The string we search for. Put %20 between words that go together." //注释文字,不会被用户看到
},
...
}
对应的语言支持代号表如下
ar Arabic
bg Bulgarian
ca Catalan
cs Czech
da Danish
de German
el Greek
en English
en_GB English (Great Britain)
en_US English (USA)
es Spanish
es_419 Spanish (Latin America and Caribbean)
et Estonian
fi Finnish
fil Filipino
fr French
he Hebrew
hi Hindi
hr Croatian
hu Hungarian
id Indonesian
it Italian
ja Japanese
ko Korean
lt Lithuanian
lv Latvian
nl Dutch
no Norwegian
pl Polish
pt_BR Portuguese (Brazil)
pt_PT Portuguese (Portugal)
ro Romanian
ru Russian
sk Slovak
sl Slovenian
sr Serbian
sv Swedish
th Thai
tr Turkish
uk Ukrainian
vi Vietnamese
zh_CN Chinese (China)
zh_TW Chinese (Taiwan)
参见: https://code.google.com/chrome/extensions/i18n.html#overview-predefined