hedera-web/js/vn/locale.js

72 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
vnLocaleStrings = {};
2016-09-26 09:28:47 +00:00
/**
* Class to manage the internationalization.
**/
2016-09-26 09:28:47 +00:00
module.exports =
{
language: null
,init: function ()
{
if (!this.language)
{
var language = navigator.language.substr (0, 2);
2015-11-19 13:57:23 +00:00
this.language = language ? language : 'en';
}
}
,load: function (path, callback)
{
this.init ();
var file = path +'/locale/'+ this.language +'.json'+ Vn.getVersion ();
var request = new XMLHttpRequest ();
request.open ('get', file, true);
request.onreadystatechange = this.loadDone.bind (this, request, callback);
request.send ();
}
,loadScript: function (path, callback)
{
this.init ();
Vn.includeJs ('locale/'+ this.language +'/'+ path, callback);
}
,loadDone: function (request, callback)
{
if (request.readyState != 4)
return;
2015-02-17 11:48:53 +00:00
var success = false;
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);
}
2016-09-27 06:18:20 +00:00
,add: function (translations)
{
2016-09-27 06:18:20 +00:00
for (var str in translations)
vnLocaleStrings[str] = translations[str];
}
}
2016-09-26 09:28:47 +00:00
window._ = function (stringId)
{
2016-09-26 09:28:47 +00:00
var string = vnLocaleStrings[stringId];
return (string) ? string : stringId;
}