Manual:Magic words/zh



魔术词是一种将各种wiki文本字符串映射到与函数关联的单个ID的技术. 变量和解析器功能使用这种技术. 映射到该ID的所有文本都将替换为函数的返回值. 文本字符串和ID之间的映射存储在变量 中的文件中，该文件可以使用加载.

默认魔术字在中实现.

魔术字是怎样工作的
每当MediaWiki在两个中括号间寻找文本时（ ），它必须确定是变量、解释器功能又或者是模板. 因此，它会出现一系列的问题：


 * 1) 它是否有关联的魔术字ID？作为解决 形式标记的第一步，MediaWiki尝试将XXX翻译成魔术字ID. 翻译表由$magicWords定义.
 * 2) * 如果没有魔术单词ID与“XXX”相关联，则“XXX”被假定为模板.
 * 3) 它是变量吗？如果魔术字ID 被 找到，MediaWiki接下来会检查它是否有任何参数.
 * 4) *如果没有找到参数，MediaWiki会检查魔术单词ID是否已被声明为可变ID. ‎ To check this, it retrieves the list of magic words serving by calling . This method gets its list of variable IDs from a hard coded list of variable IDs (see ) and from a list of custom variable IDs provided by all functions attached to the hook.
 * 5) **如果确认魔术字ID是变量，MediaWiki相关钩子将调用与相关联的函数，直到可以找到与魔术字相关联的函数值.
 * 6) Is it a parser function? If there are any parameters or if the magic word ID is missing from the list of variable magic word IDs, then MediaWiki assumes that the magic word is a parser function or template.  If the magic word ID is found in the list of parser functions declared via a call to , it is treated as a parser function and rendered using the function named  .  Otherwise, it is presumed to be a template.
 * 1) Is it a parser function? If there are any parameters or if the magic word ID is missing from the list of variable magic word IDs, then MediaWiki assumes that the magic word is a parser function or template.  If the magic word ID is found in the list of parser functions declared via a call to , it is treated as a parser function and rendered using the function named  .  Otherwise, it is presumed to be a template.
 * 1) Is it a parser function? If there are any parameters or if the magic word ID is missing from the list of variable magic word IDs, then MediaWiki assumes that the magic word is a parser function or template.  If the magic word ID is found in the list of parser functions declared via a call to , it is treated as a parser function and rendered using the function named  .  Otherwise, it is presumed to be a template.

{{note|1= By convention:


 * The magic words called variables are capitalised, case-sensitive and do not have space characters.
 * Parserfunctions are prefixed with a hash sign ( {{# ), are case insensitive and do not include space characters.

This is however a convention and one not consistently applied (for historic reasons).


 * Variables do not have space characters, but some translations of variables in other languages DO have spaces
 * Variables generally are capitalised and case-sensitive, but some parser functions also use this convention.
 * Some parser functions start with a hash sign, but some do not.

Where possible you should follow the conventions when defining or translating magic words. Magic words are higher in priority than templates, so any magic word defined, will block the usage of that defined name as a template. Following the conventions avoids adding more and more potential collisions. }}

定义魔术字
为了让魔术字起到它的作用，我们必须做到以下事情：
 * Wiki文本与魔术字ID互相映射
 * 魔术字ID与相关的php设置映射


 * a mapping between wiki text and a magic word ID
 * a mapping between a magic word ID and some php function that interprets the magic word.

将维基文本映射到魔术字ID
变量$magicWords用来将每个魔术词语ID与一个依赖语言的数组关联在一起，该数组描述了所有映射到魔术词语ID的所有文本字符串. 重要：这仅设置了后端i18n映射，你仍然需要继续编写其他代码来让MediaWiki在其他地方使用该魔术字. Also, make sure that you initialize  as an empty array before adding language-specific values or you will get errors when trying to load the magic word and will need to rebuild your localization cache before it will work.

The first element of this array is an integer flag indicating whether or not the magic word is case sensitive. The remaining elements are a list of text that should be associated with the magic word ID. If the case sensitive flag is 0, any case variant of the names in the array will match. If the case sensitive flag is 1, only exact case matches will be associated with the magic word ID. Thus the format is

This association is created by $magicWords in a file registered using.

In the example below, a Spanish MediaWiki installation will associate the magic word ID 'MAG_CUSTOM' with "personalizado", "custom", "PERSONALIZADO", "CUSTOM" and all other case variants. In an English MediaWiki only "custom" in various case combinations will be mapped to 'MAG_CUSTOM':

文件 ：

在extension.json文件的一部分：

Note that "ExampleMagic" is a different to the key you would use for a plain internationalization file (normally just the title of the extension, i.e. "Example"). "Magic" has been appended deliberately so one does not overwrite the other.

In inline PHP
您可以在 PHP 中内置关联的魔术词，而不是通过 i18n 文件. This is useful when defining hooks in  but should not be done in extensions.

将魔术字ID与PHP函数关联
The mechanism for associating magic word IDs with rendering functions depends on whether the magic word will be used as a parser function or a variable. For more information, please see:



本地化

 * 参见 Help:魔术字#位置来寻求帮助. 

You can read more on definition and usage of magic words for localisation at Manual:Messages API, Manual:Language#Namespaces; Avoid  in messages.

行为开关（双下划线魔术词）
Behavior switches are a special type of magic word. They can be recognized by their use of double underscores (rather than double braces). Example:

These magic words typically do not output any content, but instead change the behavior of a page and/or set a page property. These magic words are listed in and also at Help:Magic words#Behavior switches. The effect of most standard behavior switches is defined in. 如果没有定义具体的效果，魔术字将简单地在 page_props 表中设置一个页面属性. This can also be checked later by testing if  is null or the empty string

Custom behavior switch
Here is an example extension implementing a custom __CUSTOM__ behavour switch

custom/extension.json - This is minimal, a real extension would fill out more fields. custom/custom.i18n.php

custom/MyHooks.php

参见

 * - 类似 以及  的列表.