User:Mooeypoo/User script demos/WrapHeadingsWithMwMsgText.js

/** * User script demo: Wrap headings with i18n text. * This is an expansion of the simpler script: https://www.mediawiki.org/wiki/User:Mooeypoo/User_script_demos/WrapHeadingsWithSimpleText.js * * NOTE! mw.msg is automatically loaded but only in "basic" mode to all JavaScript in MediaWiki. * This is sufficient for simple messages like the one this script uses, but it isn't if you require * using things like "GENDER" and "PLURAL". To use either of those, you'll need to make sure the * jQueryMsg module is loaded. See https://www.mediawiki.org/wiki/User:Mooeypoo/User_script_demos * for demos using those. */ // Define messages messages = { 'en': { 'gadget-demo-wraptextwothertext-wrapper': 'OMG, $1, yo!' } };

mw.messages.set(messages['en']);

// If the user's language is anything other than English, this will // look if we have the user's language code set in the 'messages' // object, and if we do, it will set these messages instead of the // English ones, so the user can see those in their own language. var lang = mw.config.get( 'wgUserLanguage' ); if (lang && lang != 'en' && lang in messages) { mw.messages.set( messages[ lang ] ); }

$( document ).ready( function {	// Get all headings and first heading	$( '.firstHeading, .mw-headline' ).each( function  { // Get the original content var originalContent = $( this ).html; // Use the message key with the variable ($1) as the originalContent $( this ).html(			mw.msg( 'gadget-demo-wraptextwothertext-wrapper', originalContent ) 		); } ); } );