forked from nuclear-unicorn/kittensgame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
190 lines (165 loc) · 4.99 KB
/
i18n.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* FOR FULL I18N!
*/
//Localization support
dojo.declare("com.nuclearunicorn.i18n.Lang", null, {
fallbackLocale: "en",
availableLocales: null,
availableLocaleLabels: null,
language: null,
messages: null,
_deffered: null,
platformLocale: null,
//TODO: move to the configuration file
constructor: function(){
var config = new classes.KGConfig();
this.availableLocales = [this.fallbackLocale];
//console.info("Available locales:", config.statics.locales);
for (var i in config.statics.locales ){
this.availableLocales.push(config.statics.locales[i]);
}
this.availableLocaleLabels = {
"en" : "English",
"br": "Português",
"cz": "Čeština",
"de": "Deutsch",
"es": "Español",
"esl": "Español (LATAM)",
"fr": "Français",
"fro": "Ancien Français",
"it": "Italiano",
"ja": "日本語",
"ko": "한국어",
"nl": "NL",
"no": "NO",
"pl": "Polski",
"ro": "RO",
"ru": "Русский",
"tr": "TR",
"uk": "Українська",
"be": "Беларуская",
"zh": "中文",
"zht": "漢語"
};
},
init: function(timestamp){
var self = this;
if (navigator.globalization !== undefined) {
var def = $.Deferred();
navigator.globalization.getPreferredLanguage(
function (language) {
//console.log("platform locale:", language);
self.platformLocale = language.value;
def.resolve();
},
function (err) {
console.error("Unable to get platform locale", err);
def.resolve();
}
);
return def.promise().then(function(){return self._init(timestamp);});
} else {
return this._init(timestamp);
}
},
_init: function(timestamp) {
if (this._deffered) {
return this._deffered.promise();
}
// check if user already selected the locale
var lang = LCstorage["com.nuclearunicorn.kittengame.language"];
this.fallbackLocale = 'en';
if (!lang || !this.isAvailable(lang)) {
//console.log("navigator:", navigator, "platform:", this.platformLocale);
var defaultLocale = this.platformLocale || navigator.language || navigator.userLanguage;
// find closes match
var parts = defaultLocale.split(/[-_]/);
lang = this.fallbackLocale;
for (var j = 0; j < this.availableLocales.length; j++) {
if (this.availableLocales[j] == parts[0].toLowerCase()) {
lang = this.availableLocales[j];
break;
}
}
console.log("Using preferred OS locale:", lang);
LCstorage["com.nuclearunicorn.kittengame.language"] = lang;
}
// at this point we always have correct lang selected
this.language = lang;
var self = this;
this._deferred = $.Deferred();
// now we can try to load it
// fallback
console.log("User selected locale:", "'" + lang + "'", "fallback locale", "'" + this.fallbackLocale + "'");
console.log("loading fallback messages");
$.getJSON( "res/i18n/" + this.fallbackLocale + ".json?_=" + timestamp).done(function(fallbackLocaleMessages){
self.messages = fallbackLocaleMessages;
if (self.language != self.fallbackLocale ) {
// legacy
console.log("trying to load legacy locale system");
$.getJSON( "res/i18n/" + lang + ".json?_=" + timestamp).
then(function(legacyLocaleMessages){
console.log("loaded legacy locale for lang", lang, legacyLocaleMessages);
$.extend(self.messages, legacyLocaleMessages);
}).fail(function(){
console.warn("no legacy locale ", "'" + lang + "'", " found, skipping...");
}).always(function(){
// crowdin
console.log("trying to load crowdin locale system");
$.getJSON( "res/i18n/crowdin/" + lang + ".json?_=" + timestamp).then(function(crowdinLocale){
console.log("loaded crowdin locale for lang", lang, crowdinLocale);
$.extend(self.messages, crowdinLocale);
self._deferred.resolve();
}).fail(function(){
console.warn("no crowdin locale ", "'" + lang + "'", " found, skipping...");
self._deferred.resolve();
});
});
} else {
self._deferred.resolve();
}
}).fail(function(error){
console.error("Unable to load locale chain for '" + lang + "', error:", error);
});
return this._deferred.promise();
},
getAvailableLocales: function() {
return this.availableLocales;
},
getAvailableLocaleLabels: function() {
return this.availableLocaleLabels;
},
getLanguage: function() {
return this.language;
},
updateLanguage: function(lang) {
this.language = lang;
LCstorage["com.nuclearunicorn.kittengame.language"] = lang;
},
isAvailable: function(lang) {
for (var i = 0; i < this.availableLocales.length; i++) {
if (this.availableLocales[i] == lang) {
return true;
}
}
return false;
},
msg: function(key, args) {
var msg = this.messages[key];
if (!msg) {
console.error("Key '" + key + "' wasn't found");
return "$" + key;
}
if (args) {
for (var i = 0; i < args.length; i++) {
msg = msg.replace("{" + i + "}", args[i]);
}
}
return msg;
}
});
i18nLang = new com.nuclearunicorn.i18n.Lang();
// i18nLang.init();
$I = function(key, args) {
return i18nLang.msg(key, args);
};