perl-i18n

Re: Alternative catalogue format for maktext?

2002-05-02 11:25:39
On Sun, Apr 28, 2002 at 03:57:32PM -0600, Sean M. Burke wrote:
Uh, sorry, but what about the .po and load() idea? :)
Sure, try them all!  They all sound fine.

Okay -- here is a proposal of integrating it with Maketext, under
the name parse(). Please tell me if you think it's sane. :)

I'm planning to use this framework to localize RT, if Jesse agrees.

/Autrijus/

#!/usr/bin/env perl
# $File: //member/autrijus/parse_demo.pl $ $Author: autrijus $
# $Revision: #1 $ $Change: 4091 $ $DateTime: 2002/05/02 18:04:48 $

use strict;
use warnings;

=head1 NAME

parse_demo.pl - Demonstration of parse() in Locale::Maketext

=head1 SYNOPSIS

    % perl parse_demo.pl

=head1 DESCRIPTION

This script demonstrates the proposal of adding a I<parse> method
to B<Locale::Maketext>, which allows lexicon-handling modules to
read from other localization formats, such as I<gettext>, I<catmsg>,
or others.

=head1 METHODS

=head2 $self->parse(I<$format>, I<$source>)

This method takes two arguments. The first one denotes the format of
the catalog to be imported, and the second specifies where to acquire
it.  It should return a list suitable to put into the caller's
I<%Lexicon> hash.

The actualy parsing is delegated to the I<parse_B<$format>> method;
i.e. if the I<$format> is C<gettext>, then I<parse_text> is called.
It will try to look up that method in the I<@ISA> tree, as well ast
heir I<ucfirst($format)> classes, which will be automatically I<use>d.
The I<@ISA> traversal is performed by B<Class::ISA>, a core module
since perl 5.7.1.

For example, B<Demo::L10N::de_mo>-E<gt>I<parse>(C<'gettext'>, I<...>)
will search for I<parse_gettext> method in these modules:

    Demo::L10N::de_mo
    Demo::L10N::de_mo::Gettext
    Demo::L10N
    Demo::L10N::Gettext
    Locale::Maketext
    Locale::Maketext::Gettext

=head2 $self->parse_gettext(I<$source>)

This method takes a filename or file handle of a I<.po> file, parses
its content, and returns the lexicon as a flattened hash.

It may also support B<Locale::Gettext> objects and I<.mo> files in the
future.

=head1 SEE ALSO

L<Locale::Maketext>, L<Class::ISA>, L<Locale::Maketext::TPJ13>

=head1 COPYRIGHT

Copyright 2002 by Autrijus Tang E<lt>autrijus(_at_)autrijus(_dot_)orgE<gt>.

This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut

############################################################################
package Demo::L10N::de_mo;
use base 'Demo::L10N';
our %Lexicon = __PACKAGE__->parse(gettext => \*::DATA);

############################################################################
package Demo::L10N;
use base 'Locale::Maketext';

############################################################################
package Locale::Maketext;

sub parse {
    my ($self, $format, $src) = @_;
    my $method = (caller(0))[3].'_'.$format;
    $method =~ s/.*:://; # parse_$format

    require Class::ISA;

    my $mod;
    foreach (Class::ISA::self_and_super_path(scalar caller)) {
        ($mod = $_, last) if $_->can($method);
        $_ .= "::\u$format"; eval "use $_";
        ($mod = $_, last) if $_->can($method);
    }

    die "Can't handle catalog type $format!" unless $mod;
    return $mod->$method($src);
}

############################################################################
package Locale::Maketext::Gettext;

sub parse_gettext {
    my ($self, $src) = @_;
    my (%var, $key, @ret);

    unless (ref($src)) {
        require FileHandle;
        my $fh = FileHandle->new; # filename - open and return its handle
        $src = $fh->open($src);
    }

    # Parse *.po; Locale::Gettext objects and *.mo are not yet supported.
    while (<$src>) {
        /^(msgid|msgstr) +"(.*)" *$/    ? do {  # leading strings
            $var{$1} = $2;
            $key = $1;
        } :

        /^"(.*)" *$/                    ? do {  # continued strings
            $var{$key} .= $1;
        } :

        /^#, +(.*) *$/                  ? do {  # control variables
            $var{$1} = 1;
        } :

        /^ *$/                          ? do {  # interpolate string escapes
            push @ret, map { s/\\([0x]..|c?.)/qq{"\\$1"}/eeg; $_ }
                @var{'msgid', 'msgstr'};
            %var = ();
        } : ();
    }

    push @ret, map { s/\\([0x]..|c?.)/qq{"\\$1"}/eeg; $_ }
        @var{'msgid', 'msgstr'} if defined $var{msgid};

    return @ret;
}

############################################################################
package main;

my $lh; # localization handle
sub loc {
    $lh ||= Demo::L10N->get_handle('de_mo');
    return $lh->maketext(@_);
}

print loc(
    "Please type 'perldoc [_1]' to see [_2].\n", $0, loc("the documentation")
);

############################################################################
__DATA__
# FIRST AUTHOR: B1FF
#
msgid ""
msgstr ""
"Project-Id-Version: Demo 6.6.6\n"
"POT-Creation-Date: 2000-10-25 11:36+0800\n"
"PO-Revision-Date: 2000-10-08 02:00+0800\n"
"Last-Translator: B1FF\n"
"Language-Team: Hax0r\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=hax0r\n"
"Content-Transfer-Encoding: 8bit\n"

#: ../demo.pl:131
#, maketext-format
msgid "Please type 'perldoc [_1]' to see [_2].\n"
msgstr "P14z34 7yp4 'perldoc [_1]' 70 z44 [_2].\n"

#: ../demo.pl:131
msgid "the documentation"
msgstr "d4 d0Kvm4n73710n"

Attachment: pgpVgs7LBNf4H.pgp
Description: PGP signature

<Prev in Thread] Current Thread [Next in Thread>