</head>
<body>
<div id="add">
<div data-i18n="key">Hello</div>
</div>
<script>
i18next.t('key', {yes: 'this', no: 'great'});
</script>
<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>
Json文件
{ “key”:“{{yes}}是{{no}}”
}
控制台结果
i18next :: interpolator:错过传入变量yes以进行插值{{yes}}是{{no}}
i18next :: interpolator:错过传递变量no for interpolation {{no}}
此代码有什么问题吗?
感谢名单
答案 0 :(得分:0)
在初始化完成之前调用i18next.t('key', {yes: 'this', no: 'great'});
。
你应该:
``` 。在里面({ ...
}, function(err, t) {
jqueryI18next.init(i18next, $);
// add vars here!!!
$('#add').localize({yes: 'this', no: 'great'});
});
```
或使用data-i18n-option属性:https://github.com/i18next/jquery-i18next#using-options-in-translation-function