How to create a Multilingual Plugin
- December 2, 2015
- Leave a comment
Whenever you produce a WordPress plugin for general public distribution, it is of best practice to make it multilingual. Plugin with multilingual feature will help other users to use your plugin. Even many of the users will be ready to help you to produce the language files for your plugin.
Plugin translation is not a tedious task. It is easy and simple if you understand the following steps that are pretty much simple. Make a “languages” folder on plugin root directory. Following are the steps for plugin translation:
- Load Plugin Text domain
Step 1: Load Plugin Text Domain
First step is to add plugin text domain function load_plugin_textdomain, which is a WordPress function. Give the plugin a text domain name which is commonly used for your plugin name separated by a “–”. In our case, it is “simple-job-board”. Specify the path of your language folder in this function:
Load this function on “plugins_loaded” hook.
Step 2: Internationalization
Internationalization is the process to make your plugin translation ready. WordPress uses the gettext libraries and tools for internationalization. In order to make plugin strings translatable, you have to replace the original strings with special functions [__(), __e()] .
Replace it with plugin text domain as registered in step 1:
For variable strings, replace it with its text domain as registered in step 1:
Replace all plugins string that need to be translated and use the domain name which you registered in step 1:
Step 3: Localization
Localization is the subsequent process of translating an internationalized plugin. Following are the localization files created in language folder:
Portable Object Template (POT) file:
This file contains all the original strings of your plugin in English. It is just a template file for users, so that they can convert this general file for other languages. Use Poedit software for creation of POT file of your plugin, follow the codex guide.
After creating header, write the translatable strings of your plugin in the following format:
Portable Object (PO) file:
Use Poedit software to create PO file. PO file is the translation of POT file with msgstr sections in their own language. Open Poedit software and Go to:
- File > New Catalog from POT file
- Include your plugin POT file from language folder
- Save the file with your text domain name and language code (For example, in our case simple job board arabic translation in po file is simple-job-board-ar.po)
- Write the translation in msgstr section for all translatable strings
Machine Object (MO) file:
MO file is the machine object file binary of plugin translation. For creating Mo file, Open Poedit software and Go to:
- File > New Catalog
- Include your plugin language specific PO file from language folder (simple-job-board-ar.po)
- Click Save
If your file directory already have a MO file then after saving, it will update the existing MO file. After each change in POT of specific language, repeat the above steps to update MO file.
Step 4: Translation
Once you’ve completed your plugin localization process, test the translation. Change the WordPress Admin language from Settings > General.
After this, check your plugin strings that you have been added in PO file are translated.