L10n in Gtk2-perl with Locale::Maketext::Lexicon



Hello,

I just wrote the following example of how to do localization of a
Gtk2-perl program and thought it might be useful for others. Perhaps
we should put it somewhere in the wiki:

    #!/usr/bin/perl
    
    ######################################################################
    #  This is a complete example that shows how to support multiple
    #  languages in a Gtk2-Perl program through the
    #  Locale::Maketext::Lexicon class. The current example uses
    #  translation files in the same directory. For example of how to
    #  put the translation in other locations, see the
    #  Locale::Maketext::Lexicon documentation.
    #
    #  All translations are in .po files and are encoded in UTF-8.
    #
    #  Dov Grobgeld
    #  2005-03-08
    #
    #  This example is in the Public Domain
    ######################################################################
    
    use Gtk2 '-init';
    use base 'Locale::Maketext'; # inherits get_handle();
    
    use Locale::Maketext::Lexicon {
        en => ['Auto'],
        '*' => ['Gettext' => '*.po'],
    };
    
    my $lh = __PACKAGE__->get_handle; # magically gets the current locale
    
    # Lookup and convert to UTF-8
    sub _ {
        my $m = $lh->get_handle()->maketext(@_);
        utf8::decode($m);  # Turn on UTF8-flag for string
        $m;
    };
    
    ######################################################################
    #  The rest is normal gtk, but all strings are looked up with the
    #  _() function above.
    ######################################################################
    
    my $window = Gtk2::Window->new("toplevel");
    
    my $button = Gtk2::Button->new(_("Hello, world!"));
    $button->signal_connect(clicked => sub {
        Gtk2::MessageDialog->new($window,
                             'modal',
                             'error',
                             'ok',
                             _("Bye cruel world!"))
        ->run;
    
        Gtk2->main_quit;
    });
    $window->add($button);
    $window->set_border_width(10);
    $window->show_all;
    
    Gtk2->main;

I have attached three localization files as examples. Please excuse
my bad French. ;-)

Regards,
Dov

Attachment: sv_SE.po
Description: Binary data

Attachment: fr_FR.po
Description: Binary data

Attachment: he_IL.po
Description: Binary data



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]