I understand that document.write
is evil and should not be used to output text on a website.
However, I did not find a better solution yet for my usage, where I output translated text via javascript on a page. Consider this:
My template code looks like this:
<h1><script>_e("title");</script></h1>
In a javascript file I have similar code like this to translate the strigs:
// Initialize the dictionaries and set current language.
window.lang = {};
window.lang.en = { "title": "Sample Page" };
window.lang.de = { "title": "Beispiel Seite" };
window.curLang = "en";
// Translation function that should output translated text.
function _e(text) {
var dictionary = window.lang[window.curLang];
var translation = dictionary[text];
// BELOW LINE IS EVIL.
// But what's the alternative without having to change the template code above?
document.write( translation );
}
Question:
Can I change the javascript function to not use document.write
to work without changing the template code?
If not: What would be the best solution for translation here?