Search code examples
perllocalizationpackagegettext

How to localize perl package (tar.gz)


Let's say I have myscript.tar.gz package generated by h2xs -AX myscript containing bin/myScript.pl and lib/MyPackage/MyModule.pm. Makefile.PL and MANIFEST manualy edited so I'm able to install the package and run myScript.pl.

myscript.pl:

#!/usr/bin/perl
use strict;
use warnings;
use MyPackage::MyModule;

my $generator = MyPackage::MyModule->new();
my $value = $generator->getValue();

#And the message to be translated/localized
print "Obtained value was $value";

How do I localize my package?

I read this: How can I add internationalization to my Perl script? and alike, but it's sort of outdated. I also tried example from libintrl-perl, but I'm not wise from it and couldn't make it work.


Solution

  • Thanks to @Håkon:

    Solution: Dist:Zilla - instead of h2xs approach.

    In case of using Debian these packages are neccessary: libdist-zilla-perl libdist-zilla-localetextdomain-perl libdist-zilla-plugin-localemsgfmt-perl

    First start with dzil init

    $ dzil setup
    

    Next create a new package:

    $ dzil new myscript
    

    then basically follow Dist::Zilla::LocaleTextDomain and use this in a script/module to be translated:

    use Locale::TextDomain "myscript";
    #and format strings like this:
    print __ "Obtaining value...";
    print __x("Obtained value was {value}", value => $value);
    

    add this to dist.ini:

    [LocaleTextDomain]
    textdomain = myscript
    

    scan for messages/strings to be translated:

    $ dzil msg-scan
    

    initialize language translation files:

    $ dzil msg-init en us ...
    

    translate *.po files in po/ directory

    possibly test:

    $ dzil msg-compile po/en.po
    $ LANGUAGE=en perl -Ilib -CAS -I. bin/myScript.pl
    

    and remove language test dir after

    $ rm LocaleData/ -r
    

    now just create package:

    $dzil release 
    

    and enjoy the beautiful .tar.gz package. During the release process Dist::Zilla offers to upload the module to PAUSE, but defaults to not to upload (still figuring out how to prevent the offer).

    It's actually more convenient - no MANIFEST to include files just throw them to lib/ and bin/- It's magic! :)

    I hope someone else will find this usefull too.