Add mw.uls.setLanguage

Translate's pagetranslation.uls module needs to override the action
that happens after language code is set. Currently it uses setlang
url parameter, which (no longer) works with Special:MyLanguage.

After this patch, it can use mw.uls.setLanguage and does not need
to reimplement it's functionality.

Turned mw.uls.changeLanguage into simple utility that calls
location.reload when the promise is resolved.

Change-Id: I9a648b146188dd252c239085bbe276165dc5f393
This commit is contained in:
Niklas Laxström
2021-03-12 13:44:03 +01:00
committed by jenkins-bot
parent fc179cef11
commit 099f1f37c8

View File

@@ -39,25 +39,36 @@
* @param {string} language Language code. * @param {string} language Language code.
*/ */
mw.uls.changeLanguage = function ( language ) { mw.uls.changeLanguage = function ( language ) {
mw.uls.setLanguage( language ).then( function () {
location.reload();
} );
};
/**
* Change the language of wiki using API or set cookie.
*
* @param {string} language Language code.
* @return {jQuery.Promise}
*/
mw.uls.setLanguage = function ( language ) {
var api = new mw.Api(); var api = new mw.Api();
function changeLanguageAnon() { function changeLanguageAnon() {
if ( mw.config.get( 'wgULSAnonCanChangeLanguage' ) ) { if ( mw.config.get( 'wgULSAnonCanChangeLanguage' ) ) {
mw.cookie.set( 'language', language ); mw.cookie.set( 'language', language );
location.reload();
} }
return $.Deferred().resolve();
} }
// Track if event logging is enabled // Track if event logging is enabled
mw.hook( 'mw.uls.interface.language.change' ).fire( language ); mw.hook( 'mw.uls.interface.language.change' ).fire( language );
if ( mw.user.isAnon() ) { if ( mw.user.isAnon() ) {
changeLanguageAnon(); return changeLanguageAnon();
return;
} }
// TODO We can avoid doing this query if we know global preferences are not enabled // TODO We can avoid doing this query if we know global preferences are not enabled
api.get( { return api.get( {
action: 'query', action: 'query',
meta: 'globalpreferences', meta: 'globalpreferences',
gprprop: 'preferences' gprprop: 'preferences'
@@ -85,12 +96,10 @@
optionname: 'language', optionname: 'language',
optionvalue: language optionvalue: language
} ); } );
} ).done( function () { } ).catch( function () {
location.reload();
} ).fail( function () {
// Setting the option failed. Maybe the user has logged off. // Setting the option failed. Maybe the user has logged off.
// Continue like anonymous user and set cookie. // Continue like anonymous user and set cookie.
changeLanguageAnon(); return changeLanguageAnon();
} ); } );
}; };