L10n for StatusNet plugins

Now that we’ve got UI translations going pretty well for StatusNet core via TranslateWiki, I’d like to see if we can get things working for plugins as well before the 0.9 final release.

I’d like some quick feedback from folks before merging into 0.9.x; you can see my work branch here.

In core code, to make a string translatable we wrap it in one of the gettext functions like this, most often the _() shortcut:

 $this->text(_("You have a new message."));

If you’re a plugin though, you need to add your own translations into the mix, and gettext makes you tell it which “domain” you’re pulling from each time…

 // at init time
 bindtextdomain("AwesomePlugin",
                dirname(__FILE__) . "/locale");
 ...
 $this->text(dgettext("AwesomePlugin",
                         "You have a new message."));

Repeating your plugin name for every string gets old REAL fast!

In my work branch I’ve added a new gettext wrapper function _m() which knows if it’s been called from a plugin, and picks out the right domain for you based on the plugin directory.

So rather than playing around with bindtextdomain() and dgettext() manually, you can just add one character and not worry about the rest:

 $this->text(_m("You have a new message."));

It also knows how many parameters you passed to it, so instead of whipping out ngettext() or dngettext() (or god forbid dnpgettext!) you can just keep using the nice compact _m() when your needs get fancier:

Plurals:

 $this->text(sprintf(_m("You have a new message.",
                        "You have %d new messages.",
                        $messageCount),
                     $messageCount);

Contexts for disambiguation:

 // read vs delete
 $this->text(_m("message-action", "Read"));
 
 // read vs unread
 $this->text(_m("message-state", "Read"));

Plurals with contexts!

 $this->text(_m("message-state",
                "Read",
                "Read",
                $messageCount));

Plugins maintained in the main StatusNet repo shouldn’t need to worry about anything else — the .po file templates and updates via TranslateWiki will be handled through the same workflow as the core. (I’m working with Siebrand at TranslateWiki to make sure we can automate this as much as possible, including adding new plugins.)

scripts/update_po_translations.php can regenerate all (or just core, or just plugin) .po templates for those who want to push them in immediately, though.

As with core, the binary .mo files won’t be included in the git repository to simplify code maintenance. I’ve added a Makefile at the base level which’ll build all the .mo files for folks to test localization in their working copies (or to build a release!)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>