Manual:Messages API/ja

は、Message クラスやそれに関するメソッドを介してコードで使用できます.



メッセージのパラメーター
一部のメッセージにはパラメーターがあります. これらは、(静的) メッセージ テキストで 、 、 、… で表され、実行時に置換されます. 典型的なパラメーター値には、数字 (「3」の場合、「3つのバージョンを削除しますか?」)、利用者名 (「Bob」の場合、「ページ最終編集者 Bob」)、ページ名、リンクなどがあります. また、別のメッセージの場合もあります. パラメーターは任意の複雑さを持てます.

特定のメッセージそれぞれに定義されたパラメーターのリストは、MediaWiki の「languages/」フォルダー内の特別なファイル「qqq.json」に配置されます. 詳細は説明文書を参照してください.

PLURAL、GENDER、GRAMMAR のマジックワードを使用することをお勧めします. 例えば、 は、  よりも優れています. これは検索がより容易になるからです.



他のメッセージを参照する
メッセージ内で下位メッセージを参照すると便利な場合があります. 例えば、「X ボタンを使用」や「Y ページを訪問」というフレーズでは、翻訳が一貫していることを保証するために使用されます. これを行うには、以下の構文を使用できます:


 * - インターフェイス言語で下位メッセージを使用する. これは、フォーム ボタンやサイト ナビゲーションなどを参照する場合によく使用されます. 例: showpreview.
 * - コンテンツ言語で下位メッセージを使用する. これは、下位メッセージがローカル ページの翻訳可能な名前を定義する場合に使用されます. 例: mainpage、grouppage-sysop、policy-url など.
 * - コンテンツ言語で特別ページの名前を使用する.

より複雑なものが必要な場合 (例: 使用される下位メッセージが構成に依存する場合)、コードで解析して、下位メッセージ全体をパラメーターとして渡します.

例:



メッセージ内の分岐…
パラメーターの値によって、メッセージの正確な言い回しや文法的なバリエーションが影響する場合があります. 私たちが「$1 (sub)page(s) of his/her userpage」のような醜い構造物に頼らないのは、これらが利用者にとって不親切で、より良い方法があるからです. 代わりに、実行時に既知の値に従って解析されるスイッチを作成します. その後、静的なメッセージ テキストは、可能な選択肢のそれぞれをリストで提供し、スイッチの名前と、違いを生む値への参照が前に付けられます.

これは、MediaWiki で が呼び出される方法に似ています. いくつかの種類のスイッチを利用できます. '''これらは、メッセージの完全な解析、または  変換の場合にのみ機能します. '''

…on numbers via PLURAL
MediaWiki supports plurals, which makes for a nicer-looking product. 例:

If there is an explicit plural form to be given for a specific number, it is possible with the following syntax:

Be aware of PLURAL use on all numbers
When a number has to be inserted into a message text, be aware that some languages will have to use PLURAL on it even if always larger than 1. The reason is that PLURAL in languages other than English can make very different and complex distinctions, comparable to English 1st, 2nd, 3rd, 4th, … 11th, 12th, 13th, … 21st, 22nd, 23rd, … etc.

Do not try to supply three different messages for cases like "no items counted", "one item counted", "more items counted". Rather, let one message take them all, and leave it to translators and PLURAL to properly treat any possible differences of presentation for them in their respective languages.

Always include the number as a parameter if possible. Always add  syntax to the source messages if possible, even if it makes no sense in English. The syntax guides translators.

Fractional numbers are supported, but the plural rules may not be complete.

Pass the number of list items as parameters to messages talking about lists
Don't assume that there's only singular and plural. Many languages have more than two forms, which depend on the actual number used and they have to use grammar varying with the number of list items when expressing what is listed in a list visible to readers. Thus, whenever your code computes a list, include  as parameter to headlines, lead-ins, footers and other messages about the list, even if the count is not used in English. There is a neutral way to talk about invisible lists, so you can have links to lists on extra pages without having to count items in advance.

…on user names via GENDER
If you refer to a user in a message, pass the user name as parameter to the message and add a mention in the message documentation that gender is supported. If it is likely that GENDER will be used in translations for languages with gender inflections, add it explicitly in the English language source message.

If you directly address the currently logged-in user, leave the user name as parameter empty:

If you include the user name into the message (e.g. ""), consider passing it through  first, to ensure that characters like   or   are not interpreted.

Users have grammatical genders
When a message talks about a user, or relates to a user, or addresses a user directly, the user name should be passed to the message as a parameter. Thus languages having to, or wanting to, use proper gender dependent grammar, can do so. This should be done even when the user name is not intended to appear in the message, such as in "inform the user on his/her talk page", which is better made "inform the user on talk page" in English as well.

This does not mean that you are encouraged to "sexualise" messages' language: please use gender-neutral language whenever this can be done with clarity and precision.

…on use context inside sentences via GRAMMAR
Grammatical transformations for is also available. For example for Finnish, where it was an absolute necessity to make language files site-independent, i.e. to remove the Wikipedia references. In Finnish, "about Wikipedia" becomes "Tietoja Wikipediasta" and "you can upload it to Wikipedia" becomes "Voit tallentaa tiedoston Wikipediaan". Suffixes are added depending on how the word is used, plus minor modifications to the base. There is a long list of exceptions, but since only a few words needed to be translated, such as the site name, we didn't need to include it.

MediaWiki has grammatical transformation functions for over 20 languages. Some of these are just dictionaries for Wikimedia site names, but others have simple algorithms which will fail for all but the most common cases.

Even before MediaWiki had arbitrary grammatical transformation, it had a nominative/genitive distinction for month names. This distinction is necessary for some languages if you wish to substitute month names into sentences.

Filtering special characters in parameters and messages
The other (much simpler) issue with parameter substitution is HTML escaping. Despite being much simpler, MediaWiki does a pretty poor job of it.



PHP でのメッセージの使用
簡単な例を示します：

は、Message クラスのラッパーとして機能し、Message オブジェクトを作成するグローバル関数です. This example then invokes Message method  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 following sections explain the code.

パラメーター
Given a message like the following:

You pass parameters to messages that need them in several ways:

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:


 * Normal message substitution parameter.
 * Normal message substitution parameter.


 * Substitutes the parameter 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.
 * Substitutes the parameter 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.


 * Like, but does escaping. It is useful when you pass user input that may contain wikitext that should not be parsed.
 * Like, but does escaping. It is useful when you pass user input that may contain wikitext that should not be parsed.

Each function from the second group formats the value in a specific way before the substitution. must be used if the message uses. 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,  ,  ,   and  , 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.

Output modes and escaping
The Message class, and thus the object returned by wfMessage, has five output modes:


 * - returns the message text as-is; only parameters are substituted
 * - transforms the message text (see ) which transforms all   including templates and parser functions like PLURAL and GENDER, but neither escapes nor sanitizes
 * - same as 'text', but also escapes it for use in HTML
 * - parses the message text from wikitext to HTML and sanitizes (MessageCache::parse which calls the Parser)
 * - 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  or, you should always specify an output type. is appropriate when you're outputting it through.

Which output mode to use
Generally speaking, the most common modes you will use are  and. You use ->parse in most places where html markup is supported, and you use  in places where the content is going to become html escaped or html markup is not supported.

Some common cases:


 * If you are putting the message in the text part (third argument) of use  . You may also consider using  instead and using the   mode.
 * If you are putting in text (third argument) of, you should generally use.
 * If you are putting into the attributes (second argument) of or, use
 * If you are manually constructing html attributes, you should use . However you should never manually construct html attributes
 * For where   is an OutputPage object use   or  . However consider if you would rather use  instead.
 * For $out->addHTML use

Method chaining
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:

Additional methods of printing messages
The general message function in MediaWiki is. 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  that automatically sets the current context (language, current page etc.). It is therefore recommended to use for those classes, like special pages. Here is a non-exhaustive list of such classes:
 * CategoryViewer
 * HTMLForm
 * LogEventsList
 * DifferenceEngine
 * OutputPage
 * IndexPager
 * ImageHistoryList
 * ApiBase
 * ChangesList
 * Skin

Examples of correct usage: Examples of incorrect usage:

Getting the messages to the client
To use the messages, we need to make sure that the messages are available at client side first. This can be done using either a ResourceLoader module (most common) or an API query from JavaScript (rare).

Using a ResourceLoader module


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.

If you plan to use the  to generate HTML from wikitext in interface messages, then it is important to load the mediawiki.jqueryMsg module.

例 (extension.json):

Using an API query from JavaScript


You can use the following code:

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

For older MediaWiki versions before 1.27, use the following:

Use of the messages
The messages defined in the above example will be available at client side and can be accessed by.

例: $.( mw.message. ); Note how we use jQuery  method to escape our output properly when using mw.message   format.

If your message contains wikitext formatting, you can instead use the following: $.( mw.message. ); Here we use jQuery  method to insert the DOM nodes returned by mw.message   format.

In older code you might also encounter the following: ( was not available until MediaWiki 1.27) $.( mw.message. ); $.( mw.message. );

There are other correct combinations, but whenever possible, stick to the patterns above to avoid XSS vulnerabilities and make your code easier to understand for others.

We can also pass the dynamic parameters to the message (i.e. the values for $1, $2, etc.) as shown below.

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 curved angle brackets U+29FC/U+29FD (part of mathematical symbols), like '⧼message-key-foo⧽'. In older versions of MediaWiki, the message key was returned in ASCII angle brackets, like '&lt;message-key-foo&gt;', and this could generate invalid or fake HTML elements. In the case where the message key does not exists, the  method of the returned message object will also return false instead of true.

To use a message that must not go through the parser (e.g. when passing JSON data as messages, or when the message will be used as preloaded text of a page), use:

Format options
If you don't specify the output format, mw.message just returns a Message object. To output the message itself, you should specify an output format. The formats are mostly the same as in PHP side:


 * Returns the message text as-is; only parameters are substituted.
 * 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 (  (but only without parameters), ,   etc.) work, but tranclusion (e.g.  ) and server-side Magic words such as  or      do not work,
 * HTML escaped version of.
 * Parses the message text from wikitext to HTML. This supports everything from  mode, as well as most links, and allow listed HTML.
 * Like, but returns a jQuery collection instead of a HTML string.

Parameters
Parameters can be specified as additional arguments to. They can be passed as strings or as DOM nodes / jQuery collections.

Unlike in PHP, wikitext in the parameters is not parsed. Effectively, all string parameters behave like.

DOM/jQuery parameters can be used to achieve the equivalent of.

There is no support for other parameter formats. Instead of, you must format numbers before passing them as parameters, using.

Feature support in JavaScript
JavaScript messages only support a small subset of wikitext syntax. Supported features include:


 * Internal links (except pipe trick)
 * Explicit external links (no auto-numbered and free links)
 * The magic words SITENAME, PAGENAME, PAGENAMEE, (in MW 1.38+) SERVERNAME
 * The parser functions PLURAL, GENDER, GRAMMAR, int, ns, formatnum, lc, uc, lcfirst, ucfirst
 * HTML tags which are allowed in wikitext (HTML must be well-formed)
 * HTML entities,  ,  ,  ,
 * The tag

Notable wikitext syntax that is not supported:


 * テンプレート
 * Non-local interwiki links
 * All other parser functions and magic words
 * Modules (for example Module:String)
 * All other XML-like tags (extension tags)
 * Bold and italic,   (use ,  instead)
 * Lists using,   (use  or ,  instead)
 * Definition lists / indents using,   (use , ,  instead)
 * Multiple paragraphs (use instead)
 * Self-closing HTML tags
 * コメント )

The doc-jqueryMsg template can be used to document such messages, to let translators know which wikitext restrictions apply.

mw.msg
The  function is commonly used as a shortcut for.

Exporting messages through ResourceLoader callbacks
If you need to process a message on the server and send the result to the client (e.g. because you need to parse the message using parsing features that aren't supported in JS), you can do that with a package files callback in your ResourceLoader module. When you do this, take care to use, because using  will cause errors.

Using messages in Lua
Modules written in Lua using 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:

Notes about gender, grammar, plural

 * See also #Switches in messages…; the syntax itself is documented at Help:Magic words#Localization and related.

In general, GENDER, GRAMMAR and PLURAL magic words work identically in both PHP and JavaScript sides.


 * 1) You must use ,  ,   or   output formats for them to work.
 * 2) * In PHP, you can use wfMessage or.
 * 3) * In JavaScript, make sure your resource loader module depends on  (see #Using messages in JavaScript).
 * 4) You need to pass the relevant parameter as normal parameter to the message.
 * 5) * The parameter is the number for PLURAL; the plain text or wikitext-escaped username for GENDER in PHP; the gender from preferences or a user object for GENDER in JavaScript (see below).
 * 6) * For enabling plural and correct number localization in PHP, you need to use  for the number, see also #Chaining.
 * 7) * For enabling plural and correct number localization in JavaScript, you need to use  for the number

GENDER in JavaScript
If you have a message, say,, in JavaScript, you can use it as given below:

Instead of passing the gender directly, we can pass any "User-like" object with a gender option. For example, the current user object.

If the gender passed is invalid or unknown, the gender neutral form will be used as defined for each language. Pass  if you intentionally want the neutral form.

Finally, if you want to use the gender of the current user, you can pass an empty string:

PLURAL in JavaScript
If you have a message, say , in JavaScript, you can use it as given below:

Help with replacing deprecated wfMsg* functions
The code using these functions often has incorrect escaping and other code quality issues, so it's also recommended to


 * replace all Xml:: functions with their Html:: equivalents, which make it easier to do the right thing;
 * where possible, avoid globals and use  (see above);
 * replace  with   where appropriate.



関連項目

 * Message class documentation
 * Message class documentation
 * Message class documentation