1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
Diaspora.I18n = {
language: "en",
locale: {
pluralizationKey: function(n) { return this.fallback.pluralizationKey(n); },
data: {},
fallback: {
pluralizationKey: function(n) { return n == 1 ? "one" : "other"; },
data: {}
}
},
load: function(locale, language, fallbackLocale) {
this.updateLocale(this.locale, locale);
this.updateLocale(this.locale.fallback, fallbackLocale);
this.language = language;
},
updateLocale: function(locale, data) {
locale.data = $.extend(locale.data, data);
rule = this._resolve(locale, ['pluralization_rule']);
if (rule !== "") {
eval("locale.pluralizationKey = "+rule);
}
},
t: function(item, views) {
return this._render(this.locale, item.split("."), views);
},
resolve: function(item) {
return this._resolve(this.locale, item.split("."));
},
_resolve: function(locale, items) {
var translatedMessage, nextNamespace, originalItems = items.slice();
while(nextNamespace = items.shift()) {
translatedMessage = (translatedMessage)
? translatedMessage[nextNamespace]
: locale.data[nextNamespace];
if(typeof translatedMessage === "undefined") {
if (typeof locale.fallback === "undefined") {
return "";
} else {
return this._resolve(locale.fallback, originalItems);
}
}
}
return translatedMessage;
},
_render: function(locale, items, views) {
var originalItems = items.slice();
if(views && typeof views.count !== "undefined") {
items.push(locale.pluralizationKey(views.count));
}
try {
return _.template(this._resolve(locale, items))(views || {});
} catch (e) {
if (typeof locale.fallback === "undefined") {
return "";
} else {
return this._render(locale.fallback, originalItems, views);
}
}
},
reset: function() {
this.locale.data = {};
if( arguments.length > 0 && !(_.isEmpty(arguments[0])) )
this.locale.data = arguments[0];
}
};
|