summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js')
-rw-r--r--MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js71
1 files changed, 53 insertions, 18 deletions
diff --git a/MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js b/MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js
index 5a112e75..4dae749d 100644
--- a/MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js
+++ b/MLEB/UniversalLanguageSelector/resources/js/ext.uls.init.js
@@ -22,7 +22,7 @@
var hasOwn = Object.prototype.hasOwnProperty;
mw.uls = mw.uls || {};
- mw.uls.previousLanguagesCookie = 'uls-previous-languages';
+ mw.uls.previousLanguagesStorageKey = 'uls-previous-languages';
mw.uls.previousLanguageAutonymCookie = 'uls-previous-language-autonym';
mw.uls.languageSettingsModules = [ 'ext.uls.inputsettings', 'ext.uls.displaysettings' ];
@@ -60,19 +60,42 @@
};
/**
- * Change the language of wiki using setlang URL parameter
+ * Change the language of wiki using API or set cookie and reload the page
* @param {string} language Language code.
*/
mw.uls.changeLanguage = function ( language ) {
- var uri = new mw.Uri( window.location.href ),
- deferred = new $.Deferred();
+ var deferred = new $.Deferred();
+
+ function changeLanguageAnon() {
+ if ( mw.config.get( 'wgULSAnonCanChangeLanguage' ) ) {
+ mw.cookie.set( 'language', language );
+ location.reload();
+ }
+ }
deferred.done( function () {
- uri.extend( {
- setlang: language
- } );
+ var api;
+
+ if ( mw.user.isAnon() ) {
+ changeLanguageAnon();
+ return;
+ }
- window.location.href = uri.toString();
+ api = new mw.Api();
+ // @todo Change this to api.saveOption when ULS minimum MW version is 1.25
+ api.postWithToken( 'options', {
+ action: 'options',
+ optionname: 'language',
+ optionvalue: language
+ } )
+ .done( function () {
+ location.reload();
+ } )
+ .fail( function () {
+ // Set options failed. Maybe the user has logged off.
+ // Continue like anonymous user and set cookie.
+ changeLanguageAnon();
+ } );
} );
mw.hook( 'mw.uls.interface.language.change' ).fire( language, deferred );
@@ -85,22 +108,34 @@
};
mw.uls.setPreviousLanguages = function ( previousLanguages ) {
- $.cookie( mw.uls.previousLanguagesCookie,
- JSON.stringify( previousLanguages ), {
- path: '/'
- }
- );
+ try {
+ localStorage.setItem(
+ mw.uls.previousLanguagesStorageKey,
+ JSON.stringify( previousLanguages.slice( -5 ) )
+ );
+ } catch ( e ) {}
};
mw.uls.getPreviousLanguages = function () {
- var previousLanguages = $.cookie( mw.uls.previousLanguagesCookie );
+ var previousLanguages = $.cookie( mw.uls.previousLanguagesStorageKey );
- if ( !previousLanguages ) {
- return [];
+ $.removeCookie( mw.uls.previousLanguagesStorageKey, { path: '/' } );
+
+ if ( $.isArray( previousLanguages ) ) {
+ // Migrate data from cookie to localStorage.
+ mw.uls.setPreviousLanguages( previousLanguages );
+ } else {
+ previousLanguages = [];
}
- // return last 5 language changes
- return JSON.parse( previousLanguages ).slice( -5 );
+ try {
+ previousLanguages.push.apply(
+ previousLanguages,
+ JSON.parse( localStorage.getItem( mw.uls.previousLanguagesStorageKey ) )
+ );
+ } catch ( e ) {}
+
+ return previousLanguages.slice( -5 );
};
/**