
var vnLocaleStrings = {};

/**
 * Class to manage the internationalization.
 **/
module.exports =
{
	strings: {}
	
	,language: null
	
	,init: function ()
	{
		if (!this.language)
		{
			var language = navigator.language.substr (0, 2);
			this.language = language ? language : 'en';
		}
	}
	
	,load: function (path, callback)
	{
		this.init ();
	
		var file = 'locale/'+ this.language +'/'+ path +'.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;
			
		var success = false;

		if (request.status == 200)
		{
			try {
				this.add (JSON.parse (request.responseText));
				success = true;
			}
			catch (e) {
				console.log ('Locale: %s', e);
			}
		}		

		callback (success);
	}
	
	,add: function (translations)
	{
		for (var str in translations)
			vnLocaleStrings[str] = translations[str];
	}
}

window._ = function (stringId)
{
	var string = vnLocaleStrings[stringId];
	return (string) ? string : stringId;
}

