为什么我不能动态翻译。在 i18next 中使用变量?
例如在我的 JS 文件中我得到了这个:
这里我使用一个变量并将其分配给 i18n 函数,但它不起作用:
//this does not work:
var dynamicTranslation = "myText.toBetranslated";
console.log("translation dynamic ", !{JSON.stringify(t(dynamicTranslation))});
在这里,我将上面的字符串直接硬编码到 i18n 函数中,它确实有效:
//inserted string is the same string as in dynamic translation but this does work:
console.log("translation static ", !{JSON.stringify(t("myText.toBetranslated"))});
结果我得到:
翻译动态
翻译 静态正确 翻译
为了解决这个问题,我尝试通过使用 setTimeout 来解决它:
setTimeout(function() {console.log("time out translation: " + !{JSON.stringify(t(dynamicTranslation))})}, 2000);
但它仍然会显示空结果:
超时翻译:
这是玉语法。第一个代码不起作用,因为它是在后端站点用玉渲染的。此时,jade 不会考虑 JS 部分,而只是渲染它并将结果(以及未触及的 JS 代码)推送到前端。因此,t() 函数内带有变量的部分不会被翻译,因为它没有考虑 JS 代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)