New hook MessageCache::get
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Thu, 28 Nov 2013 09:43:00 +0000 (09:43 +0000)
committerAnomie <bjorsch@wikimedia.org>
Mon, 10 Feb 2014 15:51:52 +0000 (15:51 +0000)
commita064f780535a79699e346a39dc8ab7a5b87527a1
tree6fbcc39b07cdff1b5b3b73d2db8bfc2d74c169ab
parent9648aafb808f632b0c48226b878ea2d1f4011873
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
RELEASE-NOTES-1.23
docs/hooks.txt
includes/cache/MessageCache.php