i18next 库无法翻译 div 内的 <a> 标签
Not able to translate <a> tag inside div by i18next library
</head>
<body>
<div id="add">
<div data-i18n="key">You have to <a href="">Click here</a> to find better result</div>
// 如果我使用那种方式翻译完整 div 并从我的文本中删除 link。
<div data-i18n="key">You have to</div> <a href="" data-i18n="key2">Click here</a> <div data-i18n="key3">to find better result</div>
// 如果我使用这种方式代码工作正常,但为此我必须对单个句子使用 3 个键。是否可以通过单键修复此问题?
<script>
i18next
i18next.use(window.i18nextBrowserLanguageDetector)
i18next.use(window.i18nextXHRBackend)
.init({
debug: true,
tName: 't',
handleName: 'localize',
selectorAttr: 'data-i18n',
targetAttr: 'i18n-target',
optionsAttr: 'i18n-options',
useOptionsAttr: true,
parseDefaultValueFromContent: true,
initImmediate: true,
fallbackLng: false,
interpolation: {
"escapeValue": true,
"prefix": "{{",
"suffix": "}}",
"formatSeparator": ",",
"unescapePrefix": "-",
"nestingPrefix": "$t(",
"nestingSuffix": ")"
},
detection: {
order: ['querystring', 'cookie', 'navigator', 'htmlTag'],
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
caches: ['cookie'],
},
"backend": {
"loadPath": "/locales/{{lng}}/{{ns}}.json"
}
}, function(err, t) {
jqueryI18next.init(i18next, $);
$('#add').localize();
});
</script>
</body>
此代码翻译完成div
a) 将标签添加到翻译后的文本中。 -> 一键
"key": "You have to <a href="">Click here</a> to find better result"
和
<div data-i18n="html:key">You have to <a href="">Click here</a> to find better result</div>
设置innerHTML
b) 将 a 标签插入到翻译中。 -> 两个键
"key": "You have to {{link}} to find better result"
与:
$('#add').localize({ link: '<a href="">Click here</a>', interpolation: { escapeValue: false }});
翻译点击此处直接使用i18next.t
</head>
<body>
<div id="add">
<div data-i18n="key">You have to <a href="">Click here</a> to find better result</div>
// 如果我使用那种方式翻译完整 div 并从我的文本中删除 link。
<div data-i18n="key">You have to</div> <a href="" data-i18n="key2">Click here</a> <div data-i18n="key3">to find better result</div>
// 如果我使用这种方式代码工作正常,但为此我必须对单个句子使用 3 个键。是否可以通过单键修复此问题?
<script>
i18next
i18next.use(window.i18nextBrowserLanguageDetector)
i18next.use(window.i18nextXHRBackend)
.init({
debug: true,
tName: 't',
handleName: 'localize',
selectorAttr: 'data-i18n',
targetAttr: 'i18n-target',
optionsAttr: 'i18n-options',
useOptionsAttr: true,
parseDefaultValueFromContent: true,
initImmediate: true,
fallbackLng: false,
interpolation: {
"escapeValue": true,
"prefix": "{{",
"suffix": "}}",
"formatSeparator": ",",
"unescapePrefix": "-",
"nestingPrefix": "$t(",
"nestingSuffix": ")"
},
detection: {
order: ['querystring', 'cookie', 'navigator', 'htmlTag'],
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
caches: ['cookie'],
},
"backend": {
"loadPath": "/locales/{{lng}}/{{ns}}.json"
}
}, function(err, t) {
jqueryI18next.init(i18next, $);
$('#add').localize();
});
</script>
</body>
此代码翻译完成div
a) 将标签添加到翻译后的文本中。 -> 一键
"key": "You have to <a href="">Click here</a> to find better result"
和
<div data-i18n="html:key">You have to <a href="">Click here</a> to find better result</div>
设置innerHTML
b) 将 a 标签插入到翻译中。 -> 两个键
"key": "You have to {{link}} to find better result"
与:
$('#add').localize({ link: '<a href="">Click here</a>', interpolation: { escapeValue: false }});
翻译点击此处直接使用i18next.t