Your plugins ready for WordPress PolyGlots

Perhaps, you are writing a new WordPress plugin and perhaps, you do want to translate it… If this is your case, there are two approaches: to translate the code via a code translator programme, for example PoEdit, or to use the translations WordPress system called PolyGlots.

Close up of the Rosetta Stone replica

In the first case –translating via an external programme– you just follow the general i18n WordPress developement recomendations for local languages but, if you have choosen WordPress PolyGlots plataform then, in addition to the general i18n rules, you have to do some little changes in the code of your plugin. In fact, there are not a lot of changes to do, just a few ones.

Making your plugin ready for WordPress PolyGlots

Firstly, you have to check if the Text Domain: tag in the header of the plugin, has got a value exactly equal to the path where your plugin is installed, ie, if the name of the directory of your plugin is my-plugin-name-fantastic, the tag for the textdomain have to be the same.

// for a plugin installed in ../plugins/my-plugin-name-fantastic/

// in the plugin header you have to write
/*
// ...other vars of plugin header
Text Domain: my-plugin-name-fantastic
// ...other vars of plugin header
*/

Second, you have to check if the Domain Path: tag –also in the header of the plugin–, has got a value exactly equal to /languages

/*
// ...other vars of plugin header
Text Domain: my-plugin-name-fantastic
Domain Path: /languages
// ...other vars of plugin header
*/

And third, you have to change the common function for loading the plugin translations

// usually, you would write something like
// WRONG
function load_translations() {
    load_plugin_textdomain( 'my-plugin-name-fantastic', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action('plugins_loaded', 'load_translations');


// but, if you are using PolyGlots, you have to write simply
// CORRECT
function load_translations() {
    load_plugin_textdomain( 'my-plugin-name-fantastic' );   // only the textdomain name
}
add_action('plugins_loaded', 'load_translations');

Finally, avoid the substitution of the textdomain string for a variable. You must use always the complete textdomain string.

// WRONG
$myTexdomain = 'my-plugin-name-fantastic';
echo __( 'hello', $myTexdomain );

// CORRECT
echo __( 'hello', 'my-plugin-name-fantastic' );

I hope these simple instructions help you for a better i18n of your plugins.

Have a nice WordPressing!

2 Comments on Your plugins ready for WordPress PolyGlots

  • Hey Joan,

    I recommend you also try out the translation platform https://poeditor.com for translating WordPress strings.
    It supports .po files but also other popular localization formats (like JSON, XML, .properties, .strings etc), and has one of the simplest and most flexible collaborative translation interfaces I’ve worked with.

    Cheers!

    • Thanks, Carina.

      Of course it’s possible to use poedit, in fact, I’ve used it a lot but, if you want to use the WordPress.org translation system, PolyGlots, then you can’t use poedit and, in addition, you have to introduce some modification in your code. This article explains how to make this change, from poedit (or similars) to PolyGlots.

      Cheers,

      Joan Miquel

Leave a Reply to Carina Cancel Replay

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>