Data::Localize::Localizer - Localizer Base Class


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

Index


Code Index:

NAME

Top

Data::Localize::Localizer - Localizer Base Class

SYNOPSIS

Top

    package MyLocalizer;
    use Moose;

    extends 'Data::Localize::Localizer';

    no Moose;

METHODS

Top

register

Does basic registration for the localizer. If you're overriding this method, be sure to call the super class' register() method!

localize_for

format_string

AUTHOR

Top

Daisuke Maki <daisuke@endeworks.jp>

COPYRIGHT

Top


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

package Data::Localize::Localizer;
use utf8;
use Any::Moose;
use Carp ();

has _localizer => (
    is => 'rw',
    isa => 'Data::Localize',
    weak_ref => 1,
);

has formatter => (
    is => 'ro',
    isa => 'Data::Localize::Format',
    required => 1,
    lazy_build => 1,
    handles => { format_string => 'format' },
);

no Any::Moose;

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

sub register {
    my ($self, $loc) = @_;
    if ($self->_localizer) {
        Carp::confess("Localizer $self is already attached to another Data::Localize object ($loc)");
    }
    $self->_localizer( $loc );
}

sub localize_for {
    my ($self, %args) = @_;
    my ($lang, $id, $args) = @args{ qw(lang id args) };

    my $value = $self->get_lexicon($lang, $id) or return ();
    if (Data::Localize::DEBUG()) {
        print STDERR "[Data::Localize::Localizer]: localize_for - $id -> ",
            defined($value) ? $value : '(null)', "\n";
    }
    return $self->format_string($lang, $value, @$args) if $value;
    return ();
}

__PACKAGE__->meta->make_immutable();

1;

__END__