[英]i18next Displayed key instead of value
I have translation.json
file in /locales/en/
我在
/locales/en/
有translation.json
文件
{
"app": {
"name": "Example App"
}
}
In html
, I have:在
html
,我有:
<a href="/" data-i18n="app.name">
In js
:在
js
:
$(document).ready(function() {
var language_complete = navigator.language.split("-");
var language = (language_complete[0]);
console.log('language', language);
$.i18n.init({
lng: language,
fallbackLng: false,
debug: false
}, function() {
$('a').i18n();
});
});
It displayed app.name
instead of Example App
.它显示
app.name
而不是Example App
。 What i missed in my code?我在代码中遗漏了什么? Thanks
谢谢
您必须将useLocalStorage和useDataAttrOptions设置为 true
$.i18n.init({useLocalStorage: true , useDataAttrOptions:true, ....});
as of i18next V2, the backend is no longer provided out of the box ( see the migration guide ), so you're required to define a backend
configuration in the init block:从 i18next V2 开始,不再提供开箱即用的后端( 请参阅迁移指南),因此您需要在 init 块中定义
backend
配置:
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json'
},
if you don't do that, your resources will not be loaded, and translation values will fallback to their respective keys ( see the source code ).如果你不这样做,你的资源将不会被加载,翻译值将回退到它们各自的键( 参见源代码)。
In js it's because $(document).ready
doesn't wait for external contents to be loaded.在 js 中,这是因为
$(document).ready
不等待加载外部内容。 You need to use $(window).on('load',function(){...})
.您需要使用
$(window).on('load',function(){...})
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.