2016-09-26 09:28:47 +00:00
|
|
|
|
2016-10-15 18:58:30 +00:00
|
|
|
vnLocaleStrings = {};
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Class to manage the internationalization.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports =
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-10-15 18:58:30 +00:00
|
|
|
language: null
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-09-11 09:37:16 +00:00
|
|
|
,init: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-01-18 12:11:23 +00:00
|
|
|
if (this.language)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var language = 'en';
|
|
|
|
var languages = navigator.languages;
|
|
|
|
|
|
|
|
if (languages && languages.length > 0)
|
|
|
|
language = languages[0];
|
|
|
|
else if (navigator.language)
|
|
|
|
language = navigator.language;
|
|
|
|
|
|
|
|
this.language = language.substr (0, 2);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,load: function (path, callback)
|
|
|
|
{
|
|
|
|
this.init ();
|
2016-10-20 16:37:28 +00:00
|
|
|
|
|
|
|
var file = path +'/locale/'+ this.language +'.json'+ Vn.getVersion ();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest ();
|
2016-12-20 09:32:17 +00:00
|
|
|
request.open ('GET', file, true);
|
2015-01-23 13:09:30 +00:00
|
|
|
request.onreadystatechange = this.loadDone.bind (this, request, callback);
|
|
|
|
request.send ();
|
|
|
|
}
|
2015-09-11 09:37:16 +00:00
|
|
|
|
|
|
|
,loadScript: function (path, callback)
|
|
|
|
{
|
|
|
|
this.init ();
|
2016-11-08 07:00:02 +00:00
|
|
|
Vn.includeJs (path +'/locale/'+ this.language, callback);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,loadDone: function (request, callback)
|
|
|
|
{
|
|
|
|
if (request.readyState != 4)
|
|
|
|
return;
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
var success = false;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (request.status == 200)
|
2015-02-17 11:48:53 +00:00
|
|
|
{
|
|
|
|
try {
|
2016-08-22 10:41:05 +00:00
|
|
|
this.add (JSON.parse (request.responseText));
|
2015-02-17 11:48:53 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log ('Locale: %s', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback (success);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 06:18:20 +00:00
|
|
|
,add: function (translations)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-09-27 06:18:20 +00:00
|
|
|
for (var str in translations)
|
|
|
|
vnLocaleStrings[str] = translations[str];
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
window._ = function (stringId)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
var string = vnLocaleStrings[stringId];
|
2015-01-23 13:09:30 +00:00
|
|
|
return (string) ? string : stringId;
|
|
|
|
}
|
|
|
|
|