简体   繁体   English

使用i18next动态翻译/带变量翻译

[英]dynamic translation/translation with variable with i18next

How come I can't translate dynamically resp. 为什么我不能动态翻译。 using a variable with i18next? 在i18next中使用变量?

For example in my JS-file I got this: 例如,在我的JS文件中,我得到了以下信息:
Here I use a variable and assign it to the i18n function and it doesn't work: 在这里,我使用一个变量并将其分配给i18n函数,但它不起作用:

//this does not work:
var dynamicTranslation = "myText.toBetranslated";
console.log("translation dynamic ", !{JSON.stringify(t(dynamicTranslation))});    

Here I hardcoded the string from above directly into the i18n function and it does work: 在这里,我从上面将字符串直接硬编码到i18n函数中,它确实起作用:

//inserted string is the same string as in dynamic translation but this does work:           
console.log("translation static ", !{JSON.stringify(t("myText.toBetranslated"))});

As a result I get: 结果,我得到:

translation dynamic 动态翻译
translation static correct Translation 翻译静态正确翻译

In order to solve it, I tried to solve it by playing around with setTimeout: 为了解决它,我尝试通过使用setTimeout来解决它:

setTimeout(function() {console.log("time out translation: " + !{JSON.stringify(t(dynamicTranslation))})}, 2000);

But it would still show an empty result: 但是它仍然显示空结果:

time out translation: 超时翻译:

It's jade syntax. 这是玉的语法。 The first code didn't work because it is rendered with jade at the back end site. 第一个代码不起作用,因为它是在后端站点用玉渲染的。 At that point jade doesn't take the JS part into consideration but only renders it and pushes the result (along with the untouched JS code) to the front end. 在这一点上,jade并没有考虑JS部分,而只是将其渲染,并将结果(连同未修改的JS代码一起)推送到前端。 Therefore the part with the variable inside the t()-function is not translated, because it doesn't take the JS code into consideration. 因此,t()函数内部具有变量的部分不会被翻译,因为它没有考虑JS代码。

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

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