Extension:UserMessageFunction

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
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.

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]

{{#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
2. Message:Msg/key
3. "key"

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
2. Message:Msg/key
3. "key"

{{#message:key|dict}} xx

1. Message:Dict/key/xx
2. Message:Dict/key
3. "key"

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
2. Alt:Dict/key
3. "key"

You may assign dictionaries in any namespace.

{{#message:key||id}} xx

1. Message:Msg/key/id/xx
2. Message:Msg/key/xx
3. Message:Msg/key/id
4. Message:Msg/key
5. "key"

Optional parameter synonym id can provide two or more expressions to a single base message key.
For example;

Message page title Content
Message:Dict/child child
Message:Dict/child/pl children
Message:Dict/go go
Message:Dict/go/p went
Message:Dict/go/pp gone

{{#message:child|dict|pl}} → children
{{#message:go|dict|p}} → went

{{#message:key
| lang=de
}}

en

1. Message:Msg/key/de
2. Message:Msg/key
3. "key"

Optional parameter lang forces the specified language irrespective of user language preference or uselang parameter in url.

{{#message:key
| de=2
| fr=3
}}

en

1. Message:Msg/key/en
2. Message:Msg/key
3. "key"

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.
For example;

Message page title Content
Message:Dict/topic topic
Message:Dict/topic/de Thema
Message:Dict/topic/2/de Thematik
Message:Dict/topic/3/de Gegenstand
Message:Dict/topic/4/de Stoff

{{#message:topic|dict|de=3}} → Gegenstand

de

1. Message:Msg/key/2/de
2. Message:Msg/key/de
3. Message:Msg/key/2
4. Message:Msg/key
5. "key"

fr

1. Message:Msg/key/3/fr
2. Message:Msg/key/fr
3. Message:Msg/key/3
4. Message:Msg/key
5. "key"

{{#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

Kapitel 2



but following link is still effective.

Click [[#Chapter 1|here]].

{{#message:key}}
| format=uc
}}

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)
→ Click [[MediaWiki|[here]]] (in Simple)

Installation[edit]

$IP stands for the Installation Path (or "directory") of your MediaWiki installation, the same directory that holds LocalSettings.php, index.php etc..
  1. Copy php code below to $IP/extensions/UserMessageFunction/UserMessageFunction.php.
  2. 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;
    
  3. 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" );
    
  4. Installation can now be verified through Special:Version of your wiki.

Configuration[edit]

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]

The UserMessageFunction extension requires

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]

<?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|&#91;$output&#93;]]";
	}

	# 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]