Разлика между версии на „МедияУики:Common.js/CoreObjects.js“

От БГ-Фантастика
Направо към: навигация, търсене
(Cookie, MessageLanguage)
 
(засега без вмъкване на езикови файлове)
 
Ред 98: Ред 98:
  
 
// import message files
 
// import message files
gLang.setLanguage( wgUserLanguage );
+
//gLang.setLanguage( wgUserLanguage );
  
 
var mainLangs = [ DEFAULT_USER_LANGUAGE, FALLBACK_USER_LANGUAGE ];
 
var mainLangs = [ DEFAULT_USER_LANGUAGE, FALLBACK_USER_LANGUAGE ];
if ( ! inArray( wgUserLanguage, mainLangs ) ) {
+
 
gLang.importMessages( FALLBACK_USER_LANGUAGE );
+
//if ( ! inArray( wgUserLanguage, mainLangs ) ) {
}
+
// gLang.importMessages( FALLBACK_USER_LANGUAGE );
 +
//}
  
 
// add messages on load
 
// add messages on load

Текуща версия към 22:56, 15 септември 2009

// from http://www.quirksmode.org/js/cookies.html
// escape(), unescape() methods added
var Cookie = {
	create: function(name, value, days) {
		var expires;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			expires = "; expires=" + date.toGMTString();
		} else {
			expires = "";
		}
		document.cookie = Cookie.escape(name) + "=" + Cookie.escape(value)
			+ expires + "; path=/";
	},

	read: function(name) {
		var nameEQ = Cookie.escape(name) + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') {
				c = c.substring(1, c.length);
			}
			if (c.indexOf(nameEQ) === 0) {
				return Cookie.unescape(c.substring(nameEQ.length, c.length));
			}
		}
		return null;
	},

	erase: function(name) {
		Cookie.create(name, "", -1);
	},

	escape: function(v) {
		return encodeURIComponent(v);
	},
	unescape: function(v) {
		return decodeURIComponent(v);
	}
};


var _lang_messages = {};
var _debug_lang = false;

function MessageLanguage() {
	this.lang = DEFAULT_USER_LANGUAGE;
	this.messages = new Object();
	this.prefix = "";

	this.addMessages = function(messages, code, prefix) {
		if ( typeof this.messages[code] == "undefined" ) {
			this.messages[code] = new Object();
		}
		var p = typeof prefix == "string" ? prefix : "";
		for (var key in messages) {
			if ( typeof key == "string" ) {
				this.messages[code][p + key] = messages[key];
			}
		}
	};

	this.setLanguage = function(langCode) {
		this.lang = langCode;
		this.importMessages( this.lang );
	};

	this.setPrefix = function(prefix) {
		this.prefix = prefix;
	};

	this.msg = function(key) {
		key = this.prefix + key;
		var msg = this.messages[this.lang] && this.messages[this.lang][key];
		if ( typeof msg == "undefined" ) {
			if ( _debug_lang ) alert(key + " го няма на "+this.lang);
			msg = this.messages[FALLBACK_USER_LANGUAGE]
				&& this.messages[FALLBACK_USER_LANGUAGE][key];
		}
		if ( typeof msg == "undefined" ) {
			return "{"+ key +"}";
		}
		for (var i = 1; i < arguments.length; i++) {
			msg = msg.replace( new RegExp("\\$"+i, "g"), arguments[i]);
		}
		return msg;
	};

	this.importMessages = function(lang) {
		importScript("MediaWiki:Messages/" + lang + ".js");
	};
}


var gLang = new MessageLanguage();

// import message files
//gLang.setLanguage( wgUserLanguage );

var mainLangs = [ DEFAULT_USER_LANGUAGE, FALLBACK_USER_LANGUAGE ];

//if ( ! inArray( wgUserLanguage, mainLangs ) ) {
//	gLang.importMessages( FALLBACK_USER_LANGUAGE );
//}

// add messages on load
addOnloadHook( function() {
	if ( typeof _lang_messages[ wgUserLanguage ] == "object" ) {
		gLang.addMessages( _lang_messages[ wgUserLanguage ], wgUserLanguage );
	}
	if ( typeof _lang_messages[ FALLBACK_USER_LANGUAGE ] == "object" ) {
		if ( ! inArray( wgUserLanguage, mainLangs ) ) {
			gLang.addMessages( _lang_messages[ FALLBACK_USER_LANGUAGE ],
				FALLBACK_USER_LANGUAGE );
		}
	}
});