繁体   English   中英

i18next:覆盖嵌套项目

[英]i18next: overwrites nested items

我正在使用i18next来翻译我的表格。 它工作正常,但是我在嵌套项目方面遇到了问题,例如:

<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>

应用翻译后,实际的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>

它用翻译字符串覆盖innerHTML 相反,我需要“保存”现有项目并在其后附加翻译。

复选框表i18nexti18next的正确用法是什么?

我有同样的问题,我这样解决了:

$('[data-i18n]').each(function each() {
  const el = $(this);
  const contents = el.contents();

  el.text(i18n.t($(this).attr('data-i18n')))
    .append(contents);
});

编辑:

上面的方法简化了这个问题,因为如您所指出的,它不适用于自定义属性。

因此,我进行了更多搜索,发现像这样的jquery-i18next#append-content已经支持它:

<label class="form" data-i18n="[append]form.checkbox">
    <input type="checkbox" name="checkbox" value="true" required>                                        
</label>

您可以指定一个自定义属性,也可以指定特殊属性之一,例如prepend,append等,以指定要在其中显示翻译后的文本的位置。 有关jquery i18next doc的更多信息

jsfiddle上的示例

我以这种方式解决了:

<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>

span标记可以解决问题,嵌入已翻译的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM