As a quick fix, you need to use the []
syntax when returning the value, and remove the lang
argument from the fname()
function, so it becomes:
Fiddle
function language(lang){
this.error = {
fname : function(){
var fname = {
vn : "Tên",
en : "First Name",
};
return fname[lang];
},
};
}
language = new language('en');
console.log( language.error.fname() );
This works, but it isn't a very good structure. Look at the syntax for getting a translation, it's very strange looking, why is fname()
a function? Also look at how the translations are defined, they are mixed in with lots of JavaScript functions and logic, this makes it hard to maintain, and impossible for anyone to add translations that aren't familiar with JavaScript.
Let's look at a different structure:
Fiddle
function language(lang){
var translations = {
vn : {
error_fname : "Tên",
},
en : {
error_fname : 'First Name',
error_lname : 'Last Name'
}
};
var currentLang = 'en'; // default
function get(key){
return translations[currentLang][key];
}
// public pointers
this.get = get;
// constructor
if(typeof lang != 'undefined' && translations[lang]){
currentLang = lang;
}
}
var language = new language('en');
console.log( language.get('error_fname') );
First look at how the translations are defined. It's very clear and there is one en
object for all english translations. All of the english translations live in the en
object - makes sense right? There is no JavaScript logic mixed in with the translations, which means you can do things like use a JSON structure that can be read by translators that aren't familiar with JavaScript. You could even give translators a basic .ini
file, and have it transformed into this JavaScript/JSON structure because of its simplicity.
There are other benefits, such as the translations are encapsulated, and aren't directly accessible without going through the get()
method.
Here's a different way of doing it, that uses the .
syntax for getting a translation:
Fiddle
function language(lang){
var translations = {
vn : {
error_fname : "Tên",
},
en : {
error_fname : 'First Name',
error_lname : 'Last Name'
}
};
var currentLang = 'en'; // default
if(typeof lang != 'undefined' && translations[lang]){
currentLang = lang;
}
return translations[currentLang];
}
var language = language('en');
console.log( language.error_fname );
There is no get()
function, which you might or might not prefer. Personally I prefer the other way because of the encapsulation and the OOP style.