Extension:UserMessageFunction/zh
此扩展在wiki页面上存储其源代码。 请注意此代码可能未审核,或被恶意修改。 其可能包含安全漏洞,不再兼容的过时界面等等。 注意: translatewiki.net 不提供此扩展的本地化更新。 |
此扩展目前不再活跃维护! 尽管它可能仍然工作,但任何错误报告或功能请求将很可能被忽略。 |
![]() 發佈狀態: 不再維護 |
|
---|---|
实现 | 解析器函数 |
描述 | Provides multilingual user messages |
作者 | dick6809讨论 |
MediaWiki | 1.16+ |
许可协议 | GNU通用公眾授權條款2.0 |
下載 | Subversion |
The UserMessageFunction extension provides a method to present a string of text selected from a set of translation pages in the user language which is set in the user's preferences.
It works in a manner similar to the magicword {{int:}}
, but it is available not only for administrators but for users.
Usage
{{#message: keyword | dictionary | synonym id | anchor | format = lc | lcfirst | uc | ucfirst | lang = language code | xx = language specific synomym id }}
Usage | User lang. |
Message search path | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
{{#message:key}}
|
en |
1. Message:Msg/key/en |
It works in a manner similar to the magicword Please note that the keywords are case sensitive. | ||||||||||||
de |
1. Message:Msg/key/de | ||||||||||||||
{{#message:key|dict}}
|
xx |
1. Message:Dict/key/xx |
You may use two or more dictionaries. They can be arranged in a hierarchical page structure, e.g. | ||||||||||||
{{#message:key|alt:dict}}
|
xx |
1. Alt:Dict/key/xx |
You may assign dictionaries in any namespace. | ||||||||||||
{{#message:key||id}}
|
xx |
1. Message:Msg/key/id/xx |
Optional parameter
| ||||||||||||
|
en |
1. Message:Msg/key/de |
Optional parameter | ||||||||||||
|
en |
1. Message:Msg/key/en |
Optional parameter
| ||||||||||||
de |
1. Message:Msg/key/2/de | ||||||||||||||
fr |
1. Message:Msg/key/3/fr | ||||||||||||||
|
Optional parameter == {{#message:Chapter 1|anchor}} == would be displaied as
Click [[#Chapter 1|here]]. | ||||||||||||||
|
Optional parameter format can format message text. For example;
| ||||||||||||||
{{#message:key}}
|
simple | 1. Message:Msg/key
|
Language [[Message:Msg/key|key]] Honestly this feature is slightly tricky but useful. Please note that any links whiches contain Click [[MediaWiki|{{#message:here}}]]. → Click here. (in English) |
Installation
- Copy php code below to
$IP/extensions/UserMessageFunction/UserMessageFunction.php
. - Configure LocalSettings.php to add new namespace for user messages according to Manual:Using custom namespaces as below.
define( 'NS_MESSAGE', 120 ); # set the appropriate namespace number for your wiki. $wgExtraNamespaces[NS_MESSAGE] = 'Message'; $wgExtraNamespaces[NS_MESSAGE+1] = 'Message_talk'; $wgNamespacesWithSubpages[NS_MESSAGE] = true;
- Add the following line to the bottom of LocalSetting.php.
If Semantic-MediaWiki extension has been installed:require_once( "$IP/extensions/UserMessageFunction/UserMessageFunction.php" ); $wgUMFEnableAnnotation = true;
If Semantic-MediaWiki extension has not been installed:
require_once( "$IP/extensions/UserMessageFunction/UserMessageFunction.php" );
- Installation can now be verified through Special:Version of your wiki.
Configuration
The table below explains UserMessageFunction settings and their default values.
Global variable | Default value | Description |
---|---|---|
$wgUMFEnableAnnotation | false | If it set to true and Semantic MediaWiki extension has been installed, UserMessageFunction produces semantic annotations for cross-reference between wiki pages and dictionaries. For example, the code below in each message key definition pages provides the list of all pages whiches consults this page.
{{ask: [[message link::{{FULLPAGENAME}}]] | format = ul }} |
$wgUMFLinkProperty | message link | You may configure this variable to change the semantic property name of semantic annotations above. |
$wgUMFDefaultNamespace | Message | You may configure this variable to change default namespace of user messages. |
$wgUMFDefaultDictionary | Msg | You may configure this variable to change default dictionary path. |
Requirements
The UserMessageFunction extension requires
- MediaWiki 1.16+
- Extension:解析器函數
It should work on earlier versions of MediaWiki but they doesn't validated yet. Your feedback is appreciated.
Semantic MediaWiki and Semantic Forms extensions are not essential, but recommended strongly to manage messages. You can see a case study of dictionaries on our development site.
Code
<?php
#
# $Id: UserMessageFunction.php 8 2011-09-05 18:06:10Z dick6809 $
#
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => "UserMessageFucntion",
'description' => "A parser function to handle purulilingual user messages",
'version' => 0.01,
'author' => "Kazuhiro Kawaguchi",
'url' => "http://www.mediawiki.org/w/index.php?title=Manual:Parser_functions",
);
$wgUMFDefaultNamespace = 'Message';
$wgUMFDefaultDictionary = 'Msg';
$wgUMFLinkProperty = 'Message link';
$wgUMFEnableAnnotations = false;
$wgHooks['ParserFirstCallInit'][] = 'efUserMessageParserFunction_Setup';
$wgHooks['LanguageGetMagic'][] = 'efUserMessageParserFunction_Magic';
function efUserMessageParserFunction_Setup( &$parser ) {
$parser->setFunctionHook( 'message', 'efUserMessageParserFunction_Render' );
return true;
}
function efUserMessageParserFunction_Magic( &$magicWords, $langCode ) {
$magicWords['message'] = array( 1, 'message' );
return true;
}
function efUserMessageParserFunction_Render( &$parser ) {
global $wgRequest, $wgLang;
global $wgUMFDefaultNamespace;
global $wgUMFDefaultDictionary;
global $wgUMFLinkProperty;
global $wgUMFEnableAnnotations;
$args = func_get_args();
$opts = array();
array_shift( $args );
$word = array_shift( $args );
# parse option parameters
foreach( $args as $key => $val ) {
$parts = array_map( 'trim', explode( '=', $val, 2 ) );
if ( count( $parts ) == 2 ) {
$opts[$parts[0]] = $parts[1];
unset( $args[$key] );
}
elseif ( $val == 'anchor' ) {
$opts['anchor'] = true;
unset( $args[$key] );
}
}
# parse dictionary
$dict = array_shift( $args );
if ( empty( $dict ) ) {
$dict = $wgUMFDefaultDictionary;
}
if ( strpos( $dict, ':' ) === false ) {
$dict = "$wgUMFDefaultNamespace:$dict";
}
# parse language
if ( empty( $opts['lang'] ) ) {
$opts['lang'] = $wgRequest->wasPosted( 'uselang' )
? $wgRequest->getText( 'uselang' )
: $lang = $wgLang->getCode();
}
$lang = $opts['lang'];
# parse synonym id
$syno = array_shift( $args );
if ( !empty( $opts[$lang] ) ) {
$syno = $opts[$lang];
}
# add subpage delimiters
if ( strpos( $dict, ':' ) + 1 != strlen( $dict) ) {
$dict .= '/';
}
if ( !empty( $syno ) ) {
$syno = "/$syno";
}
if ( !empty( $lang ) ) {
$lang = "/$lang";
}
# search message pages
foreach( array(
"$dict$word$syno$lang",
"$dict$word$lang",
"$dict$word$syno",
"$dict$word"
) as $val ) {
if ( Title::newFromText( $val )->exists() ) {
$output = "{{{$val}}}";
break;
}
}
# if message page doesn't exist
if ( empty( $output ) ) {
$output = $word;
}
# text format option
if ( !empty( $opts['format'] ) ) {
$output = "{{{$opts['format']}:$output}}";
}
# debug option
if ( $lang == '/simple' ) {
$output = "[[$dict$word|[$output]]]";
}
# anchor option
if ( !empty( $opts['anchor'] ) ) {
$output .= "<span id=\"{{anchorencode:{$word}}}\"></span>";
}
# add semantic annotation
if ( $wgUMFEnableAnnotations ) {
$output .= "{{#set:{$wgUMFLinkProperty}={$dict}{$word}}}";
}
return array( $output, 'noparse' => false );
}
See also
- Semantic MediaWiki
- Semantic Forms
- LangSwitch - A template that this function is based on.