dynamic translation/translation with variable with i18next

天大地大妈咪最大 提交于 2019-12-11 01:22:19

问题


How come I can't translate dynamically resp. using a variable with i18next?

For example in my JS-file I got this:
Here I use a variable and assign it to the i18n function and it doesn't work:

//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:

//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(function() {console.log("time out translation: " + !{JSON.stringify(t(dynamicTranslation))})}, 2000);

But it would still show an empty result:

time out translation:


回答1:


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. Therefore the part with the variable inside the t()-function is not translated, because it doesn't take the JS code into consideration.



来源:https://stackoverflow.com/questions/40082873/dynamic-translation-translation-with-variable-with-i18next

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!