问题描述
我正在研究 i18next 本地化库的可能性.
i am investigating what is possible with i18next localization library.
现在我有以下代码(full fiddle is here):
right now i have the following code (full fiddle is here):
html
js
$(document).ready(function () { i18n.init({ "lng": 'en', "resstore": resources, "fallbacklng" : 'en' }, function (t) { $(document).i18n(); }); $('.lang').click(function () { var lang = $(this).attr('data-lang'); i18n.init({ lng: lang }, function (t) { $(document).i18n(); }); }); });
它翻译所有 text 元素,但问题是我无法翻译 custom attributes.例如,div 内的文本已翻译,但我不明白如何翻译自定义属性,如 placeholder 和 value.
it translates all text elements, but the problem is that i can not translate custom attributes. for example text inside the div is translated, but i can not understand how can i translate custom attributes like placeholder and value.
另一个问题是我的翻译方式.每当单击按钮 chi, eng 时,我都会初始化翻译(但我不确定这是正确的方式).编辑 我想我找到了解决这个问题的方法(我需要使用 setlng):i18n.setlng(lang, function(t) { ... })
another problem is with my way of translation. whenever a button chi, eng is clicked, i am initializing the translation (but i am not sure this is a correct way). edit i think i found how to solve this problem (i need to use setlng): i18n.setlng(lang, function(t) { ... })
推荐答案
直接问i18next creator这个问题后,收到了以下回复:我需要的只是将我的自定义属性放在翻译元素的前面.这是一个例子:
after asking i18next creator this question directly, i received the following reply: all i need is to put my custom attribute in front of the translation element. here is an example:
如果需要多个属性,请用 ; 分隔它们.
if multiple attributes are needed, separate them by a ;.
我从中学到了两件事:
- 我必须阅读更好的文档.
- 118next 的创作者真的很有帮助(这是对他的感谢).