Extension:UserMessageFunction
UserMessageFunction Release status: experimental |
|
---|---|
Implementation | Parser functions |
Description | Provides multilingual user messages |
Author(s) | dick6809talk |
MediaWiki | 1.16+ |
License | GPL2 |
Download | Subversion |
Translate the UserMessageFunction extension if it is available at translatewiki.net |
|
Check usage and version matrix; code metrics |
![]() |
This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net. |
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[edit | edit source]
{{#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 {{int}}, not for namespace MediaWiki: but for the subpages of Message:Msg. 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. dict/science/biology. |
||||||||||||
{{#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 synonym id can provide two or more expressions to a single base message key.
{{#message:child|dict|pl}} → children |
||||||||||||
{{#message:key |
en |
1. Message:Msg/key/de |
Optional parameter lang forces the specified language irrespective of user language preference or uselang parameter in url. |
||||||||||||
{{#message:key |
en |
1. Message:Msg/key/en |
Optional parameter xx (any language code, e.g. de=, fr=, ja=) overrides synonym id value in specific user language. This feature is useful to translate a keyword which have multiple meaning in other language, or to deal with the grammatical fluctuation, such as declension, conjugation, inflaction, etc.
{{#message:topic|dict|de=3}} → Gegenstand |
||||||||||||
de |
1. Message:Msg/key/2/de |
||||||||||||||
fr |
1. Message:Msg/key/3/fr |
||||||||||||||
{{#message:key|anchor}} |
Optional parameter anchor produces additional HTML anchor code with original message key. This feature is useful especially in section title. For example, if in German user language, the code == {{#message:Chapter 1|anchor}} == would be displaied as
Click [[#Chapter 1|here]]. |
||||||||||||||
{{#message:key}} |
Optional parameter format can format message text. For example;
{{#message:mediawiki|foramt=uc}} → MEDIAWIKI |
||||||||||||||
{{#message:key}} | simple | 1. Message:Msg/key |
Language simple is used for debugging of wikitext. When you see the page in user language simple by adding &uselang=simple parameter to page url, the wiki page will be displayed in English but all embedded {{#message:}} code will be highlighted with links whiches are point to the referencing message pages as below: [[Message:Msg/key|key]] Honestly this feature is slightly tricky but useful. Please note that any links whiches contain {{#message:}} as below will be broken in user language simple, because MediaWiki does not allow the nesting of links. This is not a bug. Click [[MediaWiki|{{#message:here}}]]. → Click here. (in English) |
Installation[edit | edit source]
- 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[edit | edit source]
The table below explains UserMessageFunction settings and their default values.
Grobal 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[edit | edit source]
The UserMessageFunction extension requires
- MediaWiki 1.16 or above
- ParserFunctions 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[edit | edit source]
<?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[edit | edit source]
- Semantic MediaWiki
- Semantic Forms
- LangSwitch - A template that this function is based on.