10.2. WordPress Plugin Localization

After internationalization of the plugin, next step is to prepare language files. This process is known as Localization which consists of following steps.

  • generate Portable Object Template (POT) file.

  • generate Portable Object (PO) file for each language.

  • get PO file translated by a translator.

  • generate Machine Object (MO) file from each PO file.

Install Software

To generate POT file, install WordPress Tools with following commands.

$ cd /opt/lampp/htdocs/wordpress
$ svn co http://develop.svn.wordpress.org/trunk/tools/
$ ln -s . src

First, change to the WordPress installation directory. Next, use SVN to download the tools. It will download tools from WordPress source repository and save it your local WordPress directory. We also create a symbolic link src pointing to wordpress directory, which is for the hard coded paths in tools php files.

 
 

To generate PO and MO files, we require GNU gettext. Normally, GNU gettext is shipped with standard Linux distributions and you may check whether it is available in your system with msginit -V command. If not installed, then install it with following command.

# yum install gettext

To run gettext on Windows, download the Setup program from GNUWin and install.

WordPress Plugin Localization - gettext

WordPress Plugin Localization

Once required softwares are installed, we can start the localization process.

The first task is to generate POT file, which is done with following commands.

$ cd /opt/lampp/htdocs/wordpress/wp-content/plugins/share-on-social/
$ php /opt/lampp/htdocs/wordpress/tools/i18n/makepot.php wp-plugin .

Run makepot.php from the plugin directory. It pulls the translatable strings from all PHP files of the plugin and creates the POT file share-on-social.pot. Next, move the POT file to langs directory.

$ mkdir langs
$ mv share-on-social.pot langs

Next task is to generate PO file for each language. As an example, let’s generate PO file for Esperanto language whose locale code is eo. For other languages and locale code, refer List of localizations.

$ cd langs
$ msginit -i share-on-social.pot -o sos-domain-eo.po -l eo

GetText’s msginit generates PO file from the POT file. The output file is named as sos-domain-eo.po where sos-domain is the plugins’ text domain and eo is the locale code of Esperanto.

Next, translate the strings in langs/sos-domain-eo.po.

share-on-social/langs/sos-domain-eo.po

# Copyright (C) 2015 Share on Social
# This file is distributed under the same license as the Share on Social package.
msgid ""
msgstr ""
"Project-Id-Version: Share on Social 1.0.0n"
"Report-Msgid-Bugs-To: http://wordpress.org/support/plugin/share-on-socialn"
"POT-Creation-Date: 2015-01-21 06:42:09+00:00n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"PO-Revision-Date: 2015-01-21 12:13+0530n"
"Last-Translator: m <m@m>n"
"Language-Team: Esperanton"
"Language: eon"

#: admin/class-sos.php:110
msgctxt "post type singular name"
msgid "Share Locker"
msgstr ""

#: admin/class-sos.php:112
msgctxt "admin menu"
msgid "Share on Social"
msgstr ""

....

For each translatable string, the msgid holds the original string and msgstr will be empty string. Edit the file and enter translated string in msgstr as shown in next listing.

share-on-social/langs/sos-domain-eo.po

#: admin/class-sos.php:110
msgctxt "post type singular name"
msgid "Share Locker"
msgstr "Konigi Locker"

#: admin/class-sos.php:112
msgctxt "admin menu"
msgid "Share on Social"
msgstr "Dividi en Socia"

Finally, generate MO file from PO using GetText’s msgfmt command.

$ msgfmt -o sos-domain-eo.mo  sos-domain-eo.po

It generates sos-domain-eo.mo from sos-domain-eo.po. We have to do this for each language. When bundling the plugin for release, include langs directory and MO files. The POT and PO files are not required in the plugin zip file.

With that, the plugin localization is complete.

 
 

Validation

We would like to verify whether internationalization and localization of the plugin is done properly.

Open WordPress Admin Screen and go to SettingsGeneral and select Esperanto in Site Language field.

WordPress Plugin Localization - Validate Language

We can also change site language through WPLANG setting in wp-settings.php

WordPress Admin screen flips to Esperanto and Share on Social Plugin loads langs/sos-domain-eo.mo and displays the menu and other labels in Esperanto.

WordPress Plugin Localization - Esperanto

In the next chapter, we cover the plugin activation, deactivation and uninstall topics.