jenkins-bot has submitted this change and it was merged.

Change subject: jquery.i18n message store for ULS
......................................................................


jquery.i18n message store for ULS


Bug: 49935
Change-Id: Iddef0805abbf9b4ff0e606fc311c57e9dfa7107d
---
M resources/js/ext.uls.displaysettings.js
M resources/js/ext.uls.init.js
2 files changed, 80 insertions(+), 20 deletions(-)

Approvals:
  Amire80: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/js/ext.uls.displaysettings.js 
b/resources/js/ext.uls.displaysettings.js
index 7de1345..11a8772 100644
--- a/resources/js/ext.uls.displaysettings.js
+++ b/resources/js/ext.uls.displaysettings.js
@@ -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();
@@ -314,6 +312,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}
                 */
diff --git a/resources/js/ext.uls.init.js b/resources/js/ext.uls.init.js
index 4531923..dc542a3 100644
--- a/resources/js/ext.uls.init.js
+++ b/resources/js/ext.uls.init.js
@@ -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;
+       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;
                        }
-               } )
-                       // Preload i18n for current language.
-                       .load( jsonLoader + currentLang, currentLang );
-       }
 
-       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();
-               initialized = true;
-               callback.call( this, true );
+               $.i18n( {
+                       locale: currentLang,
+                       messageStore: messageStore
+               } );
+
+               if ( !jsonLoader ) {
+                       jsonLoader = messageStore.load( currentLang );
+               } else {
+                       jsonLoader.done( function () {
+                               initialized = true;
+                       } );
+                       jsonLoader.done( callback );
+               }
        };
 
        $( document ).ready( function () {

-- 
To view, visit https://gerrit.wikimedia.org/r/71990
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iddef0805abbf9b4ff0e606fc311c57e9dfa7107d
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/UniversalLanguageSelector
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
Gerrit-Reviewer: Amire80 <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Santhosh <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to