Help:Extension:Translate/Hooks

This document describes how event hooks work in the Translate extension; how to add hooks for an event; and how to run hooks for an event. See also the main Translate extension documentation page.

Rationale
Hooks allow us to decouple optionally-run code from code that is run for everyone. It allows MediaWiki hackers, third-party developers and local administrators to define code that will be run at certain points in the mainline code, and to modify the data run by that mainline code. Hooks can keep mainline code simple, and make it easier to write extensions. Hooks are a principled alternative to local patches.

PHP hooks
For a more comprehensive guide to hooks, navigate to your root MediaWiki directory, and find.

JavaScript hooks
JavaScript hooks for the Translate extension are a little different. To use them, declare a dependency on ext.translate.hooks:

$wgResourceModules['...'] = array( 	'scripts' => '...js', 	'dependencies' => array( 'ext.translate.hooks' ), );

And then in that module use: mw.translateHooks.add( name, hookFunction );

where name is a string, but hookFunction is not (unlike in the case of PHP hooks) but rather a function reference. Return values from JavaScript hooks are ignored.

Events and parameters
This is a list of known events and parameters; please add to it if you're going to add events to the Translate extension.

PHP events
RequestContext $context: For adding output, accesing current interface language etc. TranslateTask   $task = null: Current task if any MessageGroup   $group array          $options: Active options
 * SpecialTranslate :: executeTask: You can output the default task output

array         &$modules: List of resource loader module names
 * TranslateBeforeAddModules: Provides an opportunity to load extra modules

string        &$message: The message that will be displayed TMessage       $m: Object representing the source object, useful for $m->original and $m->translation MessageGroup   $group: The source message group string         $targetLanguage: The language for which translations are being shown array         &$extraAttribs: An array of attributes that will be added to the table row
 * TranslateFormatMessageBeforeTable: Provides an opportunity to manipulate the display of translation strings (messages) in the main table

MessageGroup   $group: The message group being worked on MessageHandle   $handle: An object representing the translation page (e.g. 'MediaWiki:Example/qqq') array         &$boxes: An associative array to be appended to (format: 'name' => 'some HTML string')
 * TranslateGetBoxes: Provides an opportunity to add or remove "boxes" (translation helpers) from the translation interface

string        &$translation: The translation as it stands string        &$extraInputs: The extra input space, ready for HTML to be appended
 * TranslateGetExtraInputs: Provides an opportunity to add extra form inputs to to the default translation editor

array         &$defaults: Associative array of default values array         &$nondefaults: Associative array of nondefault (override) values
 * TranslateGetSpecialTranslateOptions: Provides an opportunity for overriding task values

string        &$translation: The translation string as it stands MessageHandle  $handle: The current MessageHandle object
 * TranslatePrefillTranslation: Provides an opportunity for a new translation to start not from as a carte blanche (the default) but from some prefilled string

JavaScript events
jQuery         form: The new form
 * afterRegisterFeatures: Provides an opportunity to register event handlers, etc., whenever a new form is opened.

jQuery         form: The form that has just been submitted
 * afterSubmit: Provides an opportunity to modify a Translate translation form immediately after it is submitted

jQuery         form: The form being submitted
 * beforeSubmit: Provides an opportunity to modify a Translate translation form immediately before it is submitted