javascript:用于简单本地化的对象数组

发布于 2024-11-16 02:52:05 字数 672 浏览 0 评论 0原文

我需要实现一种简单的方法来处理有关工作日名称的本地化,并且我想出了以下结构:

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'}
);

我知道我可以实现类似关联数组的东西(鉴于我知道 javascript 不提供关联数组而是对象具有类似的结构),但我需要使用数字索引而不是标签来迭代数组。 所以,我想在 for 循环中使用特定值(如 j-1 或类似的索引)来处理这个问题。 我的结构正确吗?提供了一个变量“lang”作为“it-it”或“en-us”之间的值之一,我尝试打印 weekdaysLegend[j-1][lang] (或 weekdaysLegend[j-1].lang,我认为我尝试了一切!)但结果是[object Object]。显然我错过了一些东西.. 有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-11-23 02:52:06

您必须从数组中访问索引,然后通过指定对象中的键来访问值。

这对我来说效果很好: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]);

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]);
稍尽春風 2024-11-23 02:52:05

结构看起来不错。您应该能够通过以下方式访问值:

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

当然,这也适用于变量中的值,例如:

weekdaysLegend[i][lang];

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

这将提醒一周中的日子。

听起来你做的一切都是正确的,而且这个结构也适合我。

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.

终难愈 2024-11-23 02:52:05

只是一个小注释(我看到答案已经被标记),因为我目前正在设计一个大型应用程序,我想将本地变量放入 JavaScript 数组中。

假设:1000 个单词 x4 种语言生成“xx-xx”+单词本身...

即 1000 行。语言 + 单独用于语言的相同 7 个字符 = 浪费带宽...

  • 客户端/浏览器必须先解析所有这些字符,然后才能在数组中进行任何查找。

这是我的方法:

为什么不一次生成一种语言的 javascript,如果用户选择另一种语言,只需响应(发送)正确的 javascript 到浏览器以包含?

要么存储单独的 javascript每种语言都有大数组,或者使用该语言作为服务器端脚本的参数,又名:

如果语言文件变化很大或者您需要将每个用户/模块最小化,那么使用这种方法可以很好地存档,因为您只需添加一个额外的参数“which部分/模块”来生成时间戳或时间戳,以便 javascript 文件的缓存将一直工作,直到发生更改。

如果动态方法对于网络服务器来说性能太重,那么每次发生更改/添加新的语言环境时都会发布/生成文件 - 您所需要的只是页面顶部的“语言链接器”检查,以检查为浏览器提供服务的语言文件。

结论

如果区域设置列表变大,此方法将消除大量重复“语言”ID 的开销。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文