Thomas Steur

How to make your plugin multilingual – Introducing the Matomo Platform

This is the next post of our blog series where we introduce the capabilities of the Matomo (Piwik) platform (our previous post was Generating test data – Introducing the Matomo Platform). This time you’ll learn how to equip your plugin with translations. Users of your plugin will be very thankful that they can use and translate the plugin in their language!

Managing translations

Matomo (Piwik) is available in over 50 languages and comes with many translations. The core itself provides some basic translations for words like “Visitor” and “Help”. They are stored in the directory /lang. In addition, each plugin can provide its own translations for wordings that are used in this plugin. They are located in /plugins/*/lang. In those directories you’ll find one JSON file for each language. Each language file consists in turn of tokens that belong to a group.

A group usually represents the name of a plugin, in this case “MyPlugin”. Within this group, all the tokens are listed on the left side and the related translations on the right side.

Building a translation key

As you will later see to actually translate a word or a sentence you’ll need to know the corresponding translation key. This key is built by combining a group and a token separated by an underscore. You can for instance use the key MyPlugin_BlogPost to get a translation of “Blog post”. Defining a new key is as easy as adding a new entry to the “MyPlugin” group.

Providing default translations

If a translation cannot be found then the English translation will be used as a default. Therefore, you should always provide a default translation in English for all keys in the file en.json (ie, /plugins/MyPlugin/lang/en.json).

Adding translations for other languages

This is as easy as creating new files in the lang subdirectory of your plugin. The filename consists of a 2 letter ISO 639-1 language code completed by the extension .json. This means German translations go into a file named de.json, French ones into a file named fr.json. To see a list of languages you can use have a look at the /lang directory.

Reusing translations

As mentioned Matomo (Piwik) comes with quite a lot of translations. You can and should reuse them but you are supposed to be aware that a translation key might be removed or renamed in the future. It is also possible that a translation key was added in a recent version and therefore is not available in older versions of Matomo. We do not currently announce any of such changes. Still, 99% of the translation keys do not change and it is therefore usually a good idea to reuse existing translations. Especially when you or your company would otherwise not be able to provide them. To find any existing translation keys go to Settings => Translation search in your Matomo installation. The menu item will only appear if the development mode is enabled.

Translations in PHP

Use the Piwik::translate() function to translate any text in PHP. Simply pass any existing translation key and you will get the translated text in the language of the current user in return. The English translation will be returned in case none for the current language exists.

Advanced features

Of course there are more useful things you can do with translations. For instance you can use placeholders like %s in your translations and you can use translations in JavaScript as well. In case you want to know more about those topics check out our Internationalization guide. Currently, this guide only covers translations but we will cover more topics like formatting numbers and handling currencies in the future.

Congratulations, you have learnt how to make your plugin multilingual!

If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.

Share this post

Subscribe to our newsletter to receive regular information about Matomo. You can unsubscribe at any time from it. This service uses MadMimi. Learn more about it within our privacy Policy page.

Become a partner

Privacy

Sign up for our newsletter

We are constantly adding new features and content to the leading All-In-One Analytics Platform that gives you control over your data. If you want to stay up to date with everything that is happening, feel free to subscribe below. You can unsubscribe at any time from it. The newsletter service uses MadMimi. Learn more about it within our privacy Policy page.