If i have a JSON with phrases in different languages:
var lang = {
'topic' : {
'en' : 'Topic',
'et' : 'Teema',
'fi' : 'Aihe'
},
'MainPage' : {
'en' : 'Main page',
'et' : 'Pealeht',
'fi' : 'Pääsivu'
}//,
//...etc
};
How to define a function for choosing a particular language:
function getText(lang, langKey) {
//???????
return langJson;
}
that outputs the same phrases like:
getText(lang, 'en'); //outputs {topic:'Topic',MainPage:'Main page',..}
getText(lang, 'et'); //outputs {topic:'Teema',MainPage:'Pealeht',..}
getText(lang, 'fi'); //outputs {topic:'Aihe',MainPage:'Pääsivu',..}
All the best: I
Basically you have to go through the whole
langobject and pick the value for the correct language.Object.keyswill return anArraycontaining the keys of your initiallangobject.You can use those to create a new object.
You could use a for loop to iterate over those keys and add the corresponding value to the resulting object, but I like to use
reduceto do so: