MediaWiki Extension Localisation – use of i18n and language json files

This article is all about MediaWiki extension localisation, that means to display message in different languages according to the different wiki languages. The process is really easy and supported as a core feature of wiki so we aren’t required to do lot of customization. just little tweaks and we are good to go.


In the Previous tutorial we have demonstrate how you can set up a custom extension for your MediaWiki. In this part we are going to demonstrate how you can apply one of the foundation principal of MediaWiki on your extension. This tutorial is about localization, that means to present your extension in various languages for wikis in various languages without making any changes to code while deploying. That can be done using the localization files. In the previous tutorial we create a file by the name of en.json in directory i18n. But we never used any of it in previous tutorial. Now it is time to make use of them. If you are unfamiliar with the file structure of extension and how to create the base please take a peek at the previous article from the index below.

MediaWiki Extension – How to Develop custom extension in MediaWiki
MediaWiki Extension – localization of MediaWiki Extension
MediaWiki Extension – Using Resource Loader to inject JavaScript and CSS
MediaWiki Extension – Enabling use of ajax in Extension

We have already created the directory i18n and file en.json in it. Now just to demonstrate the actual working we are creating another file in the same directory we will now create de.json. de is language code for German. now here is the content of file in the form of JSON structure, that is key:value pair.

{
	"the-key":"Hallo, ich bin Deutsche Botschaft, Aus de.json Datei"
}

{
	"the-key":"Hi I am English Message Coming from localization directory and en.json file"
}

Okay So we have the key:value pair of messages in place, now we need to tell our extension a way to find it. Which goes into the setup file again, there we register the messages directory(i18n) which holds these messages.

//Specify extension messages directory
$wgMessagesDirs['DemoExtension'] = __DIR__.'/i18n';

and a little tweak in the file where we use these messages, So we change the hooked function to use wfMessage(“key”) function which is the way we ask the MediaWiki core to find the appropriate message from message repository in i18n to look for appropriate message according to language of wiki.

class DemoExtension {
    public static function injectHTMLInEditor(&$editPage,&$output) {
		$editPage->editFormTextAfterWarn .='<div id="demoDiv" class="highlight-green">****Hi This is HTML Injected via Demo Extension****</div>';
		$editPage->editFormTextAfterWarn .='This Message Comes From The localization Files:'.wfMessage('the-key');
		return true;
    }
}

Finally save and re test the extension and now you shall see something like this in the output on the edit page form. Now with the static line it should be showing the message from the language file en.json.

Extension Localization english message

Now change the language variable in LocalSettings.php from en to de, save and test again.

# Site language code, should be one of the list in ./languages/Names.php
$wgLanguageCode = "de";

extension localisation german message

Now the message should be coming up from other file de.json. This is the base of localization in mediawiki extension development. In the next artical you can read about the use of Resource Loader in extension to add css and Javascript to the extension.