Hello,
I'm tring to create a script that changes the MediaWiki interface language via javascript based on the page language at my private wiki.
For logged-in users I'm able to do that by changing the user's options via an API request as shown below, but this do not work for anonymous users. So my question is: Is it possible to change the interface language for anonymous users/visitors via JavaScript? And how do I do that?
/**
* Automatic switch of wgUserLanguage, based on wgPageContentLanguage
* The output of Template:AutoLangSwitch (id='auto-lang-switch') is the triger
**/
( function ( $, mw ) {
mw.loader.using( 'mw.Api' ).then( function () {
var nsNumber = mw.config.get( 'wgNamespaceNumber' );
var lgAction = mw.config.get( 'wgAction' );
var pageLang = mw.config.get( 'wgPageContentLanguage' );
var userLang = mw.config.get( 'wgUserLanguage' );
var langTest = document.getElementById( 'auto-lang-switch' );
if ( pageLang !== userLang && lgAction === 'view' && nsNumber === 0 && langTest ) {
var params = {
action: 'options',
change: 'language=' + pageLang,
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', params ).done( function ( data ) {
console.log( data );
} );
location.reload();
}
} );
}( jQuery, mediaWiki ) );