
vnLocaleStrings = {};

/**
 * Class to manage the internationalization.
 */
module.exports =
{
	language: null
	
	,init: function ()
	{
		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);
	}
	
	,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 (path +'/locale/'+ this.language, 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;
}

