From: Niklas Laxström Date: Thu, 28 Nov 2013 09:43:00 +0000 (+0000) Subject: New hook MessageCache::get X-Git-Tag: 1.31.0-rc.0~16939^2 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=a064f780535a79699e346a39dc8ab7a5b87527a1 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 --- diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23 index 660869c259..2050e93ad1 100644 --- a/RELEASE-NOTES-1.23 +++ b/RELEASE-NOTES-1.23 @@ -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 diff --git a/docs/hooks.txt b/docs/hooks.txt index 390da77546..627fcab124 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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. diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index 3dee806208..daaa915b93 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -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 ); }