[英]Translate custom attributes with i18next (placeholder, value)
I am investigating what is possible with i18next localization library.我正在研究i18next本地化库的可能性。
Right now I have the following code ( full Fiddle is here ):现在我有以下代码(完整的小提琴在这里):
HTML HTML
<div data-i18n="title"></div>
<input placeholder="Hello" value="name">
<div class="holder"></div>
<button class="lang" data-lang="en">Eng</button>
<button class="lang" data-lang="ch">Chi</button>
JS 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();
});
});
});
It translates all text
elements, but the problem is that I can not translate custom attributes
.它翻译所有
text
元素,但问题是我无法翻译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
.例如, div 内的文本已翻译,但我不明白如何翻译
placeholder
和value
等自定义属性。
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).每当单击按钮
Chi
, Eng
,我都会初始化翻译(但我不确定这是正确的方法)。 Edit I think I found how to solve this problem (I need to use setLng): i18n.setLng(lang, function(t) { ... })
编辑我想我找到了如何解决这个问题(我需要使用 setLng):
i18n.setLng(lang, function(t) { ... })
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.在直接向i18next creator询问这个问题后,我收到了以下答复:我所需要的只是将我的自定义属性放在翻译元素前面。 Here is an example:
下面是一个例子:
<div data-i18n="[title]titleTransl"></div>
<input data-i18n="[placeholder]placeTransl" value="name">
If multiple attributes are needed, separate them by a ;
如果需要多个属性,用
;
分隔它们;
. .
I learned 2 things by this:我从中学到了两件事:
For me the following worked对我来说,以下工作
<input data-i18n="[placeholder]placeTransl" value="name">
So just enter the attribute's name between [] and then the translation.因此,只需在 [] 之间输入属性名称,然后再输入翻译。
Consider calling考虑打电话
$('body').i18n()
inside .done() function.在 .done() 函数中。 You should tell where to look for localizer.
你应该告诉在哪里寻找定位器。 This will work without having to call [placeholders] in the data-i18n attribute.
这将无需在 data-i18n 属性中调用 [placeholders] 即可工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.