wfMessage() is a global function which acts as a wrapper for the Message class, creating a Message object. This example then invokes Message method text() which fetches the text of the 'submit' message in the current language, performs certain language transformations (such as gender and plural), and returns the unescaped message text.

Here is a more complex example using a message that takes a count and supports linguistic plural handling:

The first approach is most common, use the second approach when mixing different types of parameters, and you can use the third to construct message objects dynamically from other data. There are different types of parameters:

wfMessage('msg')->params($username)->plain();wfMessage('msg')->rawParams($link)->plain();wfMessage('msg')->numParams($count)->plain();wfMessage('msg')->durationParams($duration)->plain();// since MW 1.22wfMessage('msg')->expiryParams($expiry)->plain();// since MW 1.22wfMessage('msg')->timeperiodParams($period)->plain();// since MW 1.22wfMessage('msg')->sizeParams($size)->plain();// since MW 1.22wfMessage('msg')->bitrateParams($bitrate)->plain();// since MW 1.22

The first one is a normal message substitution parameter. The second one substitutes the rawParams after the message has been otherwise processed; this means that these parameters are not available to parser functions nor are they escaped if escaping output format is used (see below). Make sure you escape them properly yourself.

Each function from the second group formats the value in a specific way before the substitution. numParams must be used if the message uses {{PLURAL:}}. In some cases you might not want to use it even though you have a number, for example a revision id. The other functions correspond to Language functions formatDuration, formatExpiry, formatTimePeriod, formatSize and formatBitrate, and are just shorthands for calling them directly.

To override the language in which you want the message, there is one method and one shortcut for the common case of using wiki content language. In the latter case you can use either a language code or a language object. The usual language fallback chains apply, so the actual message you get may be in a different language than requested, if a translation does not exist.

parse(): parses the message text from wikitext to HTML and sanitizes (MessageCache::parse() which calls the Parser)

parseAsBlock(): the output is wrapped in a block level HTML element, if not already, similarly to OutputPage::addWikiMsg

Remember that Html:: functions escape everything fed into them, so use the text() format with those to avoid double escaping. Hence the most common output format is text(). Also, make sure to use parse() or parseAsBlock() if the message has wikitext in it, otherwise the wikitext will just be escaped and output as plain text.

When using wfMessage() or $this->msg(), you should always specify an output type. text() is appropriate when you're outputting it through addWikiText().

Most Message methods return the current object, so you can conveniently call one after another to operate on an object before finally returning its text. This is called method chaining. Here is an example:

The general message function in MediaWiki is wfMessage. However, since in a message the value of magic words can depend on the context, there are various wrappers to this function, that automatically set the correct context.

OutputPage has a few methods that append directly to the generated output. The useful ones are:

Both of the above parse the wikitext in the context of the current page before appending it to output buffer.

Classes extending ContextSource have a method msg that automatically sets the current context (language, current page etc.). It is therefore recommended to use $this->msg() for those classes, like special pages. Here is a non-exhaustive list of such classes:[2]

CategoryViewer

HTMLForm

LogEventsList

DifferenceEngine

OutputPage

IndexPager

ImageHistoryList

ApiBase

ChangesList

Skin

Warning:

The QuickTemplate class and its subclasses (BaseTemplate, VectorTemplate, MonoBookTemplate, UserloginTemplate, UsercreateTemplate) have a method named msg which is different from the one from ContextSource. In these classes $this->msg() will simply output the escaped text of the message.

We are going to use ResourceLoader to make sure that the messages are available at the client side. For this, in your ResourceLoader modules, define the messages to be exported to the client side. It is important to note that loading the mediawiki.jqueryMsg module significantly changes the behavior of mw.message and mw.msg. Generally, you should always load jqueryMsg.

// When: The 'mediawiki.api.messages' module is loaded, and, the page is ready$.when(mw.loader.using(['mediawiki.api.messages','mediawiki.jqueryMsg']),$.ready)// Then: Load the messages that you need (if they are not yet loaded).then(function(){returnnewmw.Api().loadMessagesIfMissing(['january','february','march']);})// Then: Do stuff with them.then(doStuff);

To get the messages in some language other than the UserLanguage language, use getMessages instead of loadMessagesIfMissing, and supply the target language as the "amlang" field of the optional second parameter, like so:

// When: The 'mediawiki.api.messages' module is loaded. no need to wait for the page to be ready.$.when(mw.loader.using(['mediawiki.api.messages']))// Then: get some messages in French (language code 'fr').then(function(){returnnewmw.Api().getMessages(['january','february','march'],{amlang:'fr'});})// Then: Do stuff with them.then(doStuff);// doStuff is a function that will receive as its first parameter an object that looks like so:// { february: "février", january: "janvier", march: "mars" }

If manually building an html string, always escape the message by creating a message object and calling .escaped() (don't use mw.message().text() or the mw.msg shortcut):'<foo>' + mw.message( 'example' ).escaped() + '</foo>';

There are some more or less used aliases like gM (legacy shortcut for mw.jqueryMsg.getMessageFunction()) but its use is deprecated in favour of mw.message( ... ).parse().

In the above examples, note that the message should be defined in an i18n file. If the message key is not found in any i18n file, the result will be the message key in angle brackets – like <message-key-foo>.

To use a message that is natively HTML and has no other logic or variables, such as MediaWiki:copyright, use:

mw.message( 'foobar' ).text() Transforms the message text (all supported {{}} blocks are replaced with transformed results). See #Feature support in JavaScript for details of what is supported. For example, certain keywords ({{int:}}, {{GENDER}}, {{SITENAME}} etc.) work, but tranclusion (e.g. {{MediaWiki:}}) and server-side Magic words such as {{NUMBEROFEDITS}} or {{ns:Project}} do not work,

mw.message( 'foobar' ).escaped() HTML escaped version of text.

mw.message( 'foobar' ).parse() Parses the message text from wikitext to HTML. This supports everything from text mode, as well as most links, and whitelisted HTML.

Warning: If the mediawiki.jqueryMsg module is not loaded, all of the above methods behave essentially like plain() with possible escaping.

Note: There is no equivalent of parseAsBlock. Where necessary, wrap the output in a block element yourself.

Modules written in Lua using Scribunto run similarly to templates and have access to MediaWiki messages. The MediaWiki Lua library includes the mw.message class for processing messages. Refer to the full Lua message library documentation for the full API. Here is a simple example:

localp={}functionp.nmembers(frame)localnmembersMsg=mw.message.new('nmembers')nmembersMsg:numParams(3)-- This ensures number localization-- Show the message in the language of the wiki. frame:preprocess expands the {{plural}} clause.returnframe:preprocess(nmembersMsg:plain())endreturnp

If you have a message, say, 'message-key-gender-foo' => '{{GENDER:$1|he|she}} created an article' , in JavaScript, you can use it as given below:

mw.message('message-key-gender-foo','male').text();// returns 'he created an article'mw.message('message-key-gender-foo','female').text();// returns 'she created an article'

Instead of passing the gender directly, we can pass a User object - i.e. mw.User object with a gender attribute to mw.msg. For e.g., the current user object.

varuser=mw.user;// current usermw.message('message-key-gender-foo',user).text();// The message returned will be based on the gender of the current user.

If the gender passed to mw.msg is invalid or unknown, the gender neutral form will be used as defined for each language.

Finally, if you just want to use the gender of current user, you can leave the parameter out:

// the following line illustrates the message content, you can run this snippet on developer consolemw.messages.set('message-key-gender-foo','{{GENDER:|male|female|unknown}}');mw.message('message-key-gender-foo').text();// return value depends on your gender preference

The second parameter specifies the output mode, usually expressed as an array like array( 'escape' ) but sometimes just like 'escape': it needs to be replaced according to #Output modes and escaping, like ->escaped().

Instead of:

wfMsgExt('key',array('parse'),'apple');

write:

wfMessage('key','apple')->parseAsBlock();

Use full parsing, and wrap the output in block-level HTML tags.

Instead of:

wfMsgExt('key',array('parseinline'),'apple');

write:

wfMessage('key','apple')->parse();

Use full parsing. Parseinline is used because it is more useful when pre-building HTML. In normal use it is better to use OutputPage::(add|wrap)WikiMsg.

Instead of:

wfMsgExt('key',array('parsemag'),'apple','pear');

write:

wfMessage('key','apple','pear')->text();

Places where HTML cannot be used. {{-transformation is done.

Instead of

wfMsgHtml('key','apple');

write:

wfMessage('key')->rawParams('apple')->escaped();

wfMsgHtml does not escape parameters: to get the same result you need to use rawParams; check that the parameter really is safe for html output. If the message is then output as HTML, you must use escaped() for security: it will escape the parameters too and that's not always wanted, although it doesn't matter e.g. when the parameter is a number.