Data::Localize::Gettext - Acquire Lexicons From .po Files


Data-Localize documentation Contained in the Data-Localize distribution.

Index


Code Index:

NAME

Top

Data::Localize::Gettext - Acquire Lexicons From .po Files

DESCRIPTION

Top

METHODS

Top

format_string($lang, $value, @args)

Formats the string

add_path($path, ...)

Adds a new path where .po files may be searched for.

get_lexicon($lang, $id)

Gets the specified lexicon

set_lexicon($lang, $id, $value)

Sets the specified lexicon

merge_lexicon

Merges lexicon (may change...)

get_lexicon_map($lang)

Get the lexicon map for language $lang

set_lexicon_map($lang, \%lexicons)

Set the lexicon map for language $lang

load_from_file

Loads lexicons from specified file

load_from_path

Loads lexicons from specified path. May contain glob()'able expressions.

register

Registers this localizer

parse_metadata

Parse meta data information in .po file

UTF8

Top

Currently, strings are assumed to be utf-8,

AUTHOR

Top

Daisuke Maki <daisuke@endeworks.jp>

Parts of this code stolen from Locale::Maketext::Lexicon::Gettext.

COPYRIGHT

Top


Data-Localize documentation Contained in the Data-Localize distribution.

package Data::Localize::Gettext;
use utf8;
use Any::Moose;
use Carp ();
use Data::Localize::Gettext::Parser;
use File::Temp ();
use Data::Localize::Util qw(_alias_and_deprecate);
use Data::Localize::Storage::Hash;

extends 'Data::Localize::Localizer';
with 'Data::Localize::Trait::WithStorage';

has encoding => (
    is => 'ro',
    isa => 'Str',
    default => 'utf-8',
);

has paths => (
    is => 'ro',
    isa => 'ArrayRef',
    trigger => sub {
        my $self = shift;
        $self->load_from_path($_) for @{$_[0]};
    },
);

has use_fuzzy => (
    is => 'ro',
    isa => 'Bool',
    default => 0,
);

has keep_empty => (
    is => 'ro',
    isa => 'Bool',
    default => 0,
);

has _parser => (
    is => 'ro',
    isa => 'Data::Localize::Gettext::Parser',
    lazy_build => 1,
);

override register => sub {
    my ($self, $loc) = @_;
    super();
    $loc->add_localizer_map('*', $self);
};

no Any::Moose;

sub _build__parser {
    my $self = shift;
    return Data::Localize::Gettext::Parser->new(
        use_fuzzy  => $self->use_fuzzy(),
        keep_empty => $self->keep_empty(),
        encoding   => $self->encoding(),
    );
}

sub BUILDARGS {
    my ($class, %args) = @_;

    my $path = delete $args{path};
    if ($path) {
        $args{paths} ||= [];
        push @{$args{paths}}, $path;
    }
    $class->SUPER::BUILDARGS(%args);
}

sub _build_formatter {
    Any::Moose::load_class( 'Data::Localize::Format::Gettext' );
    return Data::Localize::Format::Gettext->new();
}

sub add_path {
    my $self = shift;
    push @{$self->paths}, @_;
    $self->load_from_path($_) for @_;
}

sub load_from_path {
    my ($self, $path) = @_;

    return unless $path;

    if (Data::Localize::DEBUG()) {
        print STDERR "[Data::Localize::Gettext]: load_from_path - loading from glob($path)\n"
    }

    foreach my $x (glob($path)) {
        $self->load_from_file($x) if -f $x;
    }
}

sub load_from_file {
    my ($self, $file) = @_;

    if (Data::Localize::DEBUG()) {
        print STDERR "[Data::Localize::Gettext]: load_from_file - loading from file $file\n"
    }

    my $lexicon = $self->_parser->parse_file($file);

    my $lang = File::Basename::basename($file);
    $lang =~ s/\.[mp]o$//;

    if (Data::Localize::DEBUG()) {
        print STDERR "[Data::Localize::Gettext]: load_from_file - registering ",
            scalar keys %{$lexicon}, " keys\n"
    }

    # This needs to be merged
    $self->merge_lexicon($lang, $lexicon);
}

_alias_and_deprecate path_add => 'add_path';

__PACKAGE__->meta->make_immutable;

1;

__END__