jquery.i18n message store for ULS

Bug: 49935
Change-Id: Iddef0805abbf9b4ff0e606fc311c57e9dfa7107d
This commit is contained in:
Santhosh Thottingal
2013-07-04 20:16:27 +05:30
committed by Santhosh
parent 8ddff0b2fc
commit 3d6e4a007f
2 changed files with 80 additions and 20 deletions

View File

@@ -204,8 +204,7 @@
// set the language for the settings panel so that webfonts
// are correctly applied.
displaySettings.$template.attr( 'lang', displaySettings.uiLanguage );
$.i18n().locale = displaySettings.uiLanguage;
displaySettings.i18n();
displaySettings.preview( displaySettings.uiLanguage );
};
}
@@ -300,8 +299,7 @@
// set the language for the settings panel so that webfonts
// are correctly applied.
displaySettings.$template.attr( 'lang', langCode );
$.i18n().locale = langCode;
displaySettings.i18n();
displaySettings.preview( langCode );
},
quickList: function () {
return mw.uls.getFrequentLanguageList();
@@ -313,6 +311,20 @@
} );
},
/**
* Preview the settings panel in the given language
* @param {String} language Language code
*/
preview: function ( language ) {
var displaySettings = this,
i18n = $.i18n();
i18n.locale = language;
i18n.messageStore.load( i18n.locale ).done( function () {
displaySettings.i18n();
} );
},
/**
* Get previous languages
* @returns {Array}

View File

@@ -31,8 +31,11 @@
this.$languageFilter.addClass( 'noime' );
};
var initialized = false,
var MWMessageStore,
jsonLoader,
initialized = false,
currentLang = mw.config.get( 'wgUserLanguage' );
mw.uls = mw.uls || {};
mw.uls.previousLanguagesCookie = 'uls-previous-languages';
/**
@@ -173,30 +176,65 @@
}
/**
* i18n initialization
* jquery.i18n message store for MediaWiki
*
*/
function i18nInit() {
var jsonLoader = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=';
MWMessageStore = function () {
this.messages = {};
};
$.i18n( {
locale: currentLang,
messageLocationResolver: function ( locale ) {
return jsonLoader + locale;
}
} )
// Preload i18n for current language.
.load( jsonLoader + currentLang, currentLang );
MWMessageStore.prototype = {
init: function () {},
get: function ( locale, messageKey ) {
return ( this.isLoaded( locale ) && this.messages[locale][messageKey] ) ||
'<' + messageKey + '>';
},
set: function( locale, messages ) {
this.messages[locale] = messages;
},
isLoaded: function ( locale ) {
if ( this.messages[locale] ) {
return true;
}
mw.uls.init = function( callback ) {
return false;
},
load: function ( locale ) {
var store = this,
deferred = $.Deferred(),
url = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=';
if ( store.isLoaded( locale ) ) {
return deferred.resolve();
}
deferred = $.getJSON( url + locale ).done( function ( data ) {
store.set( locale, data );
} ).fail( function ( jqxhr, settings, exception ) {
mw.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
} );
return deferred.promise();
}
};
mw.uls.init = function ( callback ) {
var messageStore = new MWMessageStore();
callback = callback || $.noop;
if ( initialized ) {
callback.call( this, false );
return;
}
if ( !isBrowserSupported() ) {
$( '#pt-uls' ).hide();
return;
}
@@ -211,9 +249,19 @@
$.uls.data.addLanguage( 'als', { target: 'gsw' } );
// JavaScript side i18n initialization
i18nInit();
$.i18n( {
locale: currentLang,
messageStore: messageStore
} );
if ( !jsonLoader ) {
jsonLoader = messageStore.load( currentLang );
} else {
jsonLoader.done( function () {
initialized = true;
callback.call( this, true );
} );
jsonLoader.done( callback );
}
};
$( document ).ready( function () {