After a discussion with Paul Gibbs at #wcukretreat about Internationalization (i18n) and Localization (l10n) of my WordPress plugins – he was reviewing my code for the oik-css plugin – I decided to bite the bullet and see what I needed to do for the oik suite of plugins. Here are the results of the first part of my investigation.
oik i18n – the lazy way
There’s more to National Language Enabling (aka National Language Support) than simply translating text strings into the user’s preferred language. But given that WordPress already has some support for dates, currency and number formats it’s a good idea to start with getting the text literals translated.
More advanced logic, dealing with country specific formats for telephone numbers, addresses and names for example, could be added at a later stage.
In the long run I want to ensure that my oik plugins can be used on just about any WordPress installation. AND, just as importantly, that the site would be as performant as the native implementation.
Note: oik is currently developed in British English.
I’m also looking at the prospect of delivering native language files AS REQUIRED, with a lazy implementation; deferring the loading of a specific text domain for a specific locale until it’s needed.
But first I wanted to get comfortable with the i18n / l10n APIs that WordPress provides and the tools that are used to create .pot files.
makepot – xgettext()
The WordPress tool that is used to generate .pot files is called makepot.
You get a copy by checking it out from SVN.
svn co http://i18n.svn.wordpress.org/tools/trunk/
You can use the
makepot.php PHP script to generate a .pot file for your plugin, theme OR WordPress itself.
The documentation in the codex suggests that makepot uses
xgettext to extract the strings to be translated. It may have used the utility program in the past; now it uses PHP functions, called xgettext().
The code parses the source .php files using the standard PHP parser ( token_get_all() ) then looks for function calls to each of the i18n functions, including the deprecated ones.
The routines deal with singular and plural versions of text strings, those which require the translators to understand the context and any comments for translators. They also do a fairly good job of reducing duplicates, while recording the original source of the duplicate text strings.
Although not using
xgettext any more, they still rely on other utility programs:
If you happen to use Windows then you’ll have to look around to find suitable versions of these utilities, OR compile them yourself :-), OR just hope that they weren’t really needed.
In order to actually create a .mo file for the target locale ( language and country ) of your choice you need to
first create a .po file then convert that into a .mo file using
One way of doing this is with poedit.
poedit – translation tool
One of the tools suggested for performing manual translations of .po files to .mo files is poedit.
This is quite a nice tool.
AND a bonus for Windows users is that it delivers some of the gettext utility functions required, especially
It doesn’t YET deliver
msguniq but I’m going to attempt to live without it for the time being.
Auto translating by “bbboing’ing”
One of the best ways to test something is to actually build it then use it.
Or to put it another way…
One of the best wyas to tset simhteong is to aluctlay biuld it then use it
A while ago I developed a WordPress plugin called bbboing.
I decided to give makepot a trial run by building a local version of WordPress in the “bbboing” language.
I developed a PHP script using a slightly improved version of the
bbboing() API to do it for me, converting each .pot file created by makepot into a .po file for my locale bb_BB.
The improved version of bbboing() includes changes to handle nested HTML tags, words less than 4 characters, and other things that might break the system.
I then ran my tool ( bb_BB.php ) to create the localized version of WordPress and installed the localized version in a test environment.
WP on steroids?
So here’s a screen capture of the Dashboard.
Notice that my custom post types ( Testimonials from oik-testimonial and Products from Rachel McCollin’s Pushing The Limits Chapter 3 plugin ) and oik options have not yet been translated.
Now I’m going to start changing oik.
I’ll extract the .pot file using
php makepot.php wp-plugin source-directory-for-oik
and create a bbboing language version, then carry on from there.
Note: There are some bits of code in oik that will call __() by default. I’m going to try to find the best way of dealing with these; change my code or improve makepot? We’ll see.