Using gettext with Perl

We added gettext support for the Perl code in libguestfs today. We used libintl-perl which is pleasantly simple to use, and works well with our existing C-based i18n infrastructure.

To translate a file, we just add this at the top:

use Locale::TextDomain 'libguestfs';

and we mark strings as potentially translatable just by prefixing them with a double underscore:

die __"virt-inspector: no YAML support\n"
        unless exists $INC{"YAML/"};

This doesn’t work with substitutions for reasons that are explained in the FAQ, so instead we have to use brace substitutions and the __x function:

die __x("guest image {imagename} does not exist or is not readable",
         imagename => $_)
       unless -r $_;

One large missing part is how to translate all the internal POD which we use to document interfaces and generate manpages. As far as I can tell there seems to be no good solution to translating POD today.


Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s