Sticky Posts
Jul 5, 2009
Minimizing Footprints | Basic .MO-less Wordpress Plugin Localization
![]()
I write many plugins that need to do a simple thing (sometimes on one site only) and most times carry less than 5 translatable (localized) strings. This poses a problem because a tiny .php file is no fun with a .MO tail strapped to its ass. So, after peeping into the Wordpress localization engine, I wrote a function to push some basic translations inside the $l10n domain space.
This will provide a very simple way to localize your plugins by plugging in the init action and pushing the new Wordpress translation (localization) table.
Registration is quick, painless and worth its weight in gold.
And this is how you use it:
<?php
if(function_exists('el_wp_pushTranslations')){
el_wp_pushTranslations(
array(
// Basic ... just translate mode ... 99.99% of cases it's the one to use
'Ana has apples.' => array(
'ro_RO' => 'Ana are mere',
),
// Advanced ... array elements are arguments for Translation_Entry
// This is like the next only without the new Translation_Entry
// ... and does some validations internally
// If translation is mentioned but translations is not it will be fixed
// ... internally ... example below
'Mark has classes.' => array(
'ro_RO' => array(
'translation' => 'Marcu are ore.'
),
),
// TOO advaced ... added this one just to be fancy ... use the aboves
// ... or use this one if you know what you're doing ;) ... DON'T USE IT
// Unlike the above, you need to mention the original twice: key and singular
// ... where the above adds the singular itself before creating the Translation_Entry
'John has problems.' => array(
'ro_RO' => new Translation_Entry(array(
'singular' => 'John has problems.', // Singular must also be specified here too
'translations' => array('Ion are probleme.')
)),
),
)
);
}
// As you notice you need to feed an array with:
// keys being untranslated texts, subkeys being locales, values for locales being translations.
?>
It does a lot of checks on parameters allowing you great versatility. Very easy to use and it's fully ... extensively commented.
As a side effect using such a function will allow your plugin users to push new translations easily into the plugins. Just add a new locale to each untranslated string with the translation - Copy/Paste.
PS: As a side comment, the very way Wordpress localizes is really unoptimized. For real performance things could be trimmed down a whole lot.

