Currently I am working on web app that will support several languages. Therefore I prepared table in my database with translations. However, I am not sure how to populate we
Use on every piece of text you want to change according to the language a span HTML tag because you can embed inline on every piece of HTML (contrary to div or p which have a display:block by default). Then for each span use a class with a name starting with a certain pattern, for example:
Then using jQuery's function .each change every span tag that matches the pattern lang in its class name, according to the selected language.
I put also here a simple example for you to check.
var LanguageList = {
"EN" : "English",
"ES" : "Español",
"PT" : "Português"
};
//languages Objects
var WORDS_EN = {
"text1" : "text One",
"text2" : "text Two"
};
var WORDS_ES = {
"text1" : "texto Un",
"text2" : "texto Dos"
};
var WORDS_PT = {
"text1" : "texto Um",
"text2" : "texto Dois"
};
window.onload = initialize;
function initialize() {
var $dropdown = $("#country_select");
$.each(LanguageList, function(key, value) {
$dropdown.
append($("").
val(key).
text(value));
});
loadsLanguage("EN");
}
function loadsLanguage(lang){
/*fills all the span tags with class=lang pattern*/
$('span[class^="lang"]').each(function(){
var LangVar = (this.className).replace('lang-','');
var Text = window["WORDS_"+lang][LangVar];
$(this).text(Text);
});
}
div{
margin: 15px;
}
/