开发者

javascript: array of object for simple localization

I need to implement a simple way to handle localization about weekdays' names, and I came up with the following structure:

var weekdaysLegend=new Array(
{'it-it':'Lunedì', 'en-us':'Monday'}, 
{'it-it':'Martedì', 'en-us':'Tuesday'},
{'it-it':'Mercoledì', 'en-us':'Wednesday'},
{'it-it':'Giovedì', 'en-us':'Thursday'}, 
{'it-it':'Venerdì', 'en-us':'Friday'},
{'it-it':'开发者_开发技巧Sabato', 'en-us':'Saturday'}, 
{'it-it':'Domenica', 'en-us':'Sunday'}
);

I know I could implement something like an associative array (given the fact that I know that javascript does not provide associative arrays but objects with similar structure), but i need to iterate through the array using numeric indexes instead of labels. So, I would like to handle this in a for cycle with particular values (like j-1 or indexes like that). Is my structure correct? Provided a variable "lang" as one of the value between "it-it" or "en-us", I tried to print weekdaysLegend[j-1][lang] (or weekdaysLegend[j-1].lang, I think I tried everything!) but the results is [object Object]. Obviously I'm missing something.. Any idea?


The structure looks fine. You should be able to access values by:

weekdaysLegend[0]["en-us"]; // returns Monday

Of course this will also work for values in variables such as:

weekdaysLegend[i][lang];

for (var i = 0; i < weekdaysLegend.length; i++) {
    alert(weekdaysLegend[i]["en-us"]);
}

This will alert the days of the week.

Sounds like you're doing everything correctly and the structure works for me as well.


Just a small note (I see the answer is already marked) as I am currently designing on a large application where I want to put locals into a javascript array.

Assumption: 1000 words x4 languages generates 'xx-xx' + the word itself...

Thats 1000 rows pr. language + the same 7 chars used for language alone = wasted bandwitdh...

  • the client/browser will have to PARSE THEM ALL before it can do any lookup in the arrays at all.

here is my approach:

Why not generate the javascript for one language at a time, if the user selects another language, just respond(send) the right javascript to the browser to include?

Either store a separate javascript with large array for each language OR use the language as parametre to the server-side script aka:

If the language file changes a lot or you need to minimize it per user/module, then its quite archivable with this approach as you can just add an extra parametre for "which part/module" to generate or a timestamp so the cache of the javascript file will work until changes occures.

if the dynamic approach is too performance heavy for the webserver, then publish/generate the files everytime there is a change/added a new locale - all you'll need is the "language linker" check in the top of the page, to check which language file to server the browser.

Conclusion

This approach will remove the overhead of a LOT of repeating "language" ID's if the locales list grows large.


You have to access an index from the array, and then a value by specifying a key from the object.

This works just fine for me: http://jsfiddle.net/98Sda/.

var day = 2;
var lang = 'en-us';

var weekdaysLegend = [
{'it-it':'Lunedì', 'en-us':'Monday'}, 
{'it-it':'Martedì', 'en-us':'Tuesday'},
{'it-it':'Mercoledì', 'en-us':'Wednesday'},
{'it-it':'Giovedì', 'en-us':'Thursday'}, 
{'it-it':'Venerdì', 'en-us':'Friday'},
{'it-it':'Sabato', 'en-us':'Saturday'}, 
{'it-it':'Domenica', 'en-us':'Sunday'}
];

alert(weekdaysLegend[day][lang]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜