[英]i18next: overwrites nested items
I'm using i18next
to translate my form. 我正在使用
i18next
来翻译我的表格。 It works fine but I have problems with nested items, for example: 它工作正常,但是我在嵌套项目方面遇到了问题,例如:
<div style="margin-bottom: 25px" class="input-group">
<div class="checkbox">
<label class="form" data-i18n="form.checkbox">
<input type="checkbox" name="checkbox" value="true" required>
</label>
</div>
</div>
After applying the translation, the actual HTML code is like this: 应用翻译后,实际的HTML代码如下所示:
<div style="margin-bottom: 25px" class="input-group">
<div class="checkbox">
<label class="form" data-i18n="form.checkbox">
translated value (no more <input> tag!)
</label>
</div>
</div>
It overwrites the innerHTML
with the translation string. 它用翻译字符串覆盖
innerHTML
。 Instead I need to "save" existing items and append the translation after them. 相反,我需要“保存”现有项目并在其后附加翻译。
What is the correct use of i18next
on a checkbox form entry? 复选框表
i18next
上i18next
的正确用法是什么?
I had the same problem and I solved it like this: 我有同样的问题,我这样解决了:
$('[data-i18n]').each(function each() {
const el = $(this);
const contents = el.contents();
el.text(i18n.t($(this).attr('data-i18n')))
.append(contents);
});
The above is oversimplifying the problem because, as you pointed out, it won't work for custom attributes. 上面的方法简化了这个问题,因为如您所指出的,它不适用于自定义属性。
Therefore I searched a bit more and I found out it's already supported by jquery-i18next#append-content like this: 因此,我进行了更多搜索,发现像这样的jquery-i18next#append-content已经支持它:
<label class="form" data-i18n="[append]form.checkbox">
<input type="checkbox" name="checkbox" value="true" required>
</label>
You can either specify a custom attribute or one of the special attributes like prepend, append , etc. to specify where you want the translated text to appear. 您可以指定一个自定义属性,也可以指定特殊属性之一,例如prepend,append等,以指定要在其中显示翻译后的文本的位置。 More info on jquery i18next doc
有关jquery i18next doc的更多信息
I solved in this way: 我以这种方式解决了:
<div class="checkbox">
<label class="form">
<input id="input_flag2" type="checkbox" name="disclaimer2" value="true" required>
<span data-i18n="form.checkbox"></span>
</label>
</div>
The span
tag does the trick, embedding the translated content. span
标记可以解决问题,嵌入已翻译的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.