Expand all

mw.Message

Describes a translateable text or HTML string. Similar to the Message class in MediaWiki PHP.

Example

var obj, str;
mw.messages.set( {
    'hello': 'Hello world',
    'hello-user': 'Hello, $1!',
    'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3',
    'so-unusual': 'You will find: $1'
} );

obj = mw.message( 'hello' );
mw.log( obj.text() );
// Hello world

obj = mw.message( 'hello-user', 'John Doe' );
mw.log( obj.text() );
// Hello, John Doe!

obj = mw.message( 'welcome-user', 'John Doe', 'Wikipedia', '2 hours ago' );
mw.log( obj.text() );
// Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago

// Using mw.msg shortcut, always in "text' format.
str = mw.msg( 'hello-user', 'John Doe' );
mw.log( str );
// Hello, John Doe!

// Different formats
obj = mw.message( 'so-unusual', 'Time "after" <time>' );

mw.log( obj.text() );
// You will find: Time "after" <time>

mw.log( obj.escaped() );
// You will find: Time &quot;after&quot; &lt;time&gt;

Constructor

new mw.Message(map, key, [parameters]) #

Object constructor for messages. The constructor is not publicly accessible; use mw.message instead.

Parameters:

Name Type Attributes Description
map mw.Map

Message store

key string
parameters Array optional
Source:

Methods

escaped() → {string} #

Format message and return as escaped text in HTML.

This is equivalent to the #text format, which is then HTML-escaped.

Source:

Returns:

String form of html escaped message

Type
string
Format message and return as escaped text in HTML.

exists() → {boolean} #

Check if a message exists. Equivalent to mw.Map.exists.

Source:

Returns:

Type
boolean
Check if a message exists.

isParseable() → {boolean} #

Check whether the message contains only syntax supported by jqueryMsg.

This method is only available when jqueryMsg is loaded.

Example

const msg = mw.message( 'key' );
mw.loader.using(`mediawiki.jqueryMsg`).then(() => {
  if ( msg.isParseable() ) {
    ...
  }
})
Since:
  • 1.41
Source:

Returns:

Type
boolean
Check whether the message contains only syntax supported by jqueryMsg.

params(parameters) → {mw.Message}chainable #

Add (does not replace) parameters for $N placeholder values.

Parameters:

Name Type Description
parameters Array
Source:

Returns:

Type
mw.Message
Add (does not replace) parameters for $N placeholder values.

parse() → {string} #

Parse message as wikitext and return HTML.

If jqueryMsg is loaded, this transforms text and parses a subset of supported wikitext into HTML. Without jqueryMsg, it is equivalent to mw.Message#escaped.

Source:

Returns:

String form of parsed message

Type
string
Parse message as wikitext and return HTML.

parseDom() → {jQuery} #

Parse the message to DOM nodes, rather than HTML string like mw.Message#parse.

This method is only available when jqueryMsg is loaded.

Example

const msg = mw.message( 'key' );
mw.loader.using(`mediawiki.jqueryMsg`).then(() => {
  if ( msg.isParseable() ) {
    const $node = msg.parseDom();
    $node.appendTo('body');
  }
})
Since:
  • 1.27
Source:

Returns:

Type
jQuery
Parse the message to DOM nodes, rather than HTML string like mw.Message#parse.

plain() → {string} #

Return message plainly.

This substitutes parameters, but otherwise does not transform the message content.

Source:

Returns:

String form of plain message

Type
string
Return message plainly.

text() → {string} #

Format message with text transformations applied.

If jqueryMsg is loaded, {{-transformation is done for supported magic words such as {{plural:}}, {{gender:}}, and {{int:}}. Without jqueryMsg, it is equivalent to mw.Message#plain.

Source:

Returns:

String form of text message

Type
string
Format message with text transformations applied.