Jump to content

User:Kbrown (WMF)/switchLang.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*****************************************************************************************************
* Adapted from example at https://www.mediawiki.org/wiki/Help:Extension:GlobalCssJs#Example:_Set_a_global_interface_language
* To use this script to convert to a language other than English, copy the below code to your global.js 
* and change the value of the "wantLang" variable to your desired language code
***************************************************************************************************/
mw.loader.using("mediawiki.user", function() {
	var wantLang = "en";
	var nowLang = mw.user.options.get('language');
	if (nowLang != wantLang) {
		( new mw.Api() ).postWithToken( 'options', {
			action: "options",
			optionname: "language",
			optionvalue: wantLang,
		} ).done( function() {
			mw.loader.using([], function(){
				mw.notify( "Language has been changed from " + nowLang + " to " + wantLang + ". Please refresh the page." );
			} );
		} );
	} else {
		console.log('Language already set to English!');
	}
} );