/* gettext-like internationalization (i18n) functions
 * 
 * Copyright (C) 2005  Sylvain Beucler
 *
 * This work is distributed under the W3C(R) Software License [1] in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.
 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
 */


// TODO: consider the 'lang' QUERYSTRING param as a Accept-Language
// HTTP header, ie with a multiple languages choice. Load the
// appropriate message catalog in the first gettext() call.

/* First decode the URI and try to get language information */
var lang;
try {
    var querystring = decodeURIComponent(document.URL + '?');
    var pairs=querystring.split('?')[1].split('&');
} catch (e) {}
for (var i=0; i < pairs.length; i++) {
    var pairsValues = pairs[i].split('=');
    if (pairsValues[0] == "lang")
	lang = pairsValues[1];
}
lang = lang || navigator.language || navigator.browserLanguage || "";
lang = lang.substring(0, 2);

var intlTextDomain;
var intlTexts = new Array();

function getLang() {
    return lang;
}

// Sets the current text domain (ie application)
function textdomain (domainname) {
	intlTextDomain = domainname;
}

// Returns a translation of the text
function _(text) {
    // Is the application domain defined?
    var domain = intlTexts[intlTextDomain];
    if (domain != undefined) {
	// Is there translation data for the current language?
	var translations = intlTexts[intlTextDomain][lang];
	if (translations != undefined) {
	    // Is there a translation for this text in the current
	    // language?
	    var result = translations[text];
	    if (result != undefined)
		return result;
	}
    }
    // If there was an error, returns the original text, untranslated
    return text;
}

// '_' is an alias for 'gettext', but the former is more frequently
// used than the latter
function gettext(text) {
    return _(text);
}

