New hook MessageCache::get

Example usage:

$wgHooks['MessageCache::get'][] = function( &$key ) {
	static $keys = null;
	if ( $keys === null ) {
		global $wgExtensionMessagesFiles;
		require( $wgExtensionMessagesFiles['OverrideMessages'] );
		$keys = array_flip( array_keys( $messages['en'] ) );
	}

	if ( isset( $keys["myprefix-$key"] ) ) {
		$key = "myprefix-$key";
	}

	return true;
}

Pros:
* Easy way to override standard core and extension messages without
  any changes to them
* Messages can be stored in a standard i18n file
* Messages can be translated easily with Translate
* Messages can be shared accross multiple wikis easily
* Takes advantage of the normal message cache behavior unlike the
  MessagePreLoad hook
* Missing translations fallback to the override, not to the
  uncustomized standard translation
* Do not need to handle conflicting message keys at translatewiki.net
  if adopted by WMF

Cons:
* This method is called often, so there will be small performance
  impact if no hooks are registered. Impact can be big if the
  implementation of hook subscriber is inefficient.

This can help with bugs like 36149. It doesn't remove the manual work
needed to detect those messages and adding them to the i18n file.

I have been using this patch in a wiki farm for months.

Change-Id: Ib39937a440e71ae7292cf992ab37a569189741e4
This commit is contained in:
Niklas Laxström 2013-11-28 09:43:00 +00:00 committed by Anomie
parent 9648aafb80
commit a064f78053
3 changed files with 16 additions and 2 deletions

View file

@ -86,6 +86,8 @@ production.
* New user accounts' personal and talk pages are now watched by them by default.
* Added SkinTemplateGetLanguageLink hook to allow changing the html of language
links.
* Added MessageCache::get hook as a new way to customize messages across
multiple sites.
=== Bug fixes in 1.23 ===
* (bug 41759) The "updated since last visit" markers (on history pages, recent

View file

@ -1667,6 +1667,12 @@ $mediaWiki: The $mediawiki object
$title: title of the message (string)
$message: value (string), change it to the message you want to define
'MessageCache::get': When fetching a message. Can be used to override the key
for customisations. Given and returned message key must be in special format:
1) first letter must be in lower case according to the content language.
2) spaces must be replaced with underscores
&$key: message key (string)
'MessageCacheReplace': When a message page is changed. Useful for updating
caches.
$title: name of the page changed.

View file

@ -728,11 +728,17 @@ class MessageCache {
// Normalise title-case input (with some inlining)
$lckey = strtr( $key, ' ', '_' );
if ( ord( $key ) < 128 ) {
if ( ord( $lckey ) < 128 ) {
$lckey[0] = strtolower( $lckey[0] );
$uckey = ucfirst( $lckey );
} else {
$lckey = $wgContLang->lcfirst( $lckey );
}
wfRunHooks( 'MessageCache::get', array( &$lckey ) );
if ( ord( $lckey ) < 128 ) {
$uckey = ucfirst( $lckey );
} else {
$uckey = $wgContLang->ucfirst( $lckey );
}