Changeset 29642

Show
Ignore:
Timestamp:
02/06/09 17:57:54 (4 years ago)
Author:
daisuke
Message:

refactor, add gettext support

Location:
lang/perl/Data-Localize/trunk
Files:
4 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Data-Localize/trunk/lib/Data/Localize/Localizer.pm

    r29633 r29642  
    33package Data::Localize::Localizer; 
    44use Moose::Role; 
     5use Moose::Util::TypeConstraints; 
    56 
    6 requires 'localize_for', 'register'; 
     7requires 'register'; 
     8 
     9has 'style' => ( 
     10    is => 'rw', 
     11    isa => enum([qw(gettext maketext)]), 
     12    default => 'gettext', 
     13); 
    714 
    815no Moose::Role; 
     16no Moose::Util::TypeConstraints; 
     17 
     18sub localize_for { 
     19    my ($self, %args) = @_; 
     20    my ($lang, $id, $args) = @args{ qw(lang id args) }; 
     21 
     22    my $lexicon = $self->lexicon_get($lang) or return (); 
     23    my $value = $lexicon->{ $id }; 
     24    if (&Data::Localize::DEBUG) { 
     25        print STDERR "[Data::Localize::Localizer]: localize_for - $id -> ", 
     26            defined($value) ? $value : '(null)', "\n"; 
     27    } 
     28    return $self->format_string($value, @$args) if $value; 
     29    return (); 
     30} 
     31 
     32sub format_string { 
     33    my ($self, $value, @args) = @_; 
     34    my $style = $self->style; 
     35    if ($style eq 'gettext') { 
     36        $value =~ s/%(\d+)/$args[$1 - 1] || ''/ge; 
     37    } elsif ($style eq 'maketext') { 
     38        $value =~ s/\[_(\d+)\]/$args[$1 - 1] || ''/ge; 
     39    } 
     40    return $value; 
     41} 
    942 
    10431; 
  • lang/perl/Data-Localize/trunk/lib/Data/Localize/Namespace.pm

    r29636 r29642  
    33package Data::Localize::Namespace; 
    44use Moose; 
    5 use Moose::Util::TypeConstraints; 
    65use MooseX::AttributeHelpers; 
    76use Module::Pluggable::Object; 
    87 
    98with 'Data::Localize::Localizer'; 
    10  
    11 has 'style' => ( 
    12     is => 'rw', 
    13     isa => enum([qw(gettext maketext)]), 
    14     default => 'gettext', 
    15 ); 
    169 
    1710has 'namespaces' => ( 
     
    2821 
    2922no Moose; 
    30 no Moose::Util::TypeConstraints; 
    3123 
    3224sub register { 
     
    4739} 
    4840 
    49 sub localize_for { 
    50     my ($self, %args) = @_; 
    51     my ($lang, $id, $args) = @args{ qw(lang id args) }; 
    52  
    53     my $lexicon = $self->lexicon_get($lang) or return (); 
    54     my $value = $lexicon->{ $id }; 
    55     return $self->format_string($value, @$args) if $value; 
    56     return (); 
    57 } 
    58  
    5941our %LOADED; 
    6042sub lexicon_get { 
     
    6446        my $klass = "$namespace\::$lang"; 
    6547        if (&Data::Localize::DEBUG) { 
    66             print STDERR "lexicon_get: Trying $klass\n"; 
     48            print STDERR "[Data::Localize::Namespace]: lexicon_get - Trying $klass\n"; 
    6749        } 
    6850 
     51        # Catch the very weird case where is_class_loaded() returns true 
     52        # but the class really hasn't been loaded yet. 
     53        no strict 'refs'; 
    6954        if (! $LOADED{$klass}) { 
    70             if (! Class::MOP::is_class_loaded($klass)) { 
     55            if (defined %{"$klass\::Lexicon"} && Class::MOP::is_class_loaded($klass)) { 
     56                if (&Data::Localize::DEBUG) { 
     57                    print STDERR "[Data::Localize::Namespace]: lexicon_get - class already loaded\n"; 
     58                } 
     59            } else { 
     60                if (&Data::Localize::DEBUG) { 
     61                    print STDERR "[Data::Localize::Namespace]: lexicon_get - loading $klass\n"; 
     62                } 
    7163                eval "require $klass"; 
    7264                if ($@) { 
     
    7769                } 
    7870            } 
     71            if (&Data::Localize::DEBUG) { 
     72                print STDERR "[Data::Localize::Namespace]: lexicon_get - setting $klass to already loaded\n"; 
     73            } 
    7974            $LOADED{$klass}++; 
    8075        } 
    81         { 
    82             no strict 'refs'; 
    83             return \%{ "$klass\::Lexicon" } 
     76 
     77        if (&Data::Localize::DEBUG) { 
     78            print STDERR "[Data::Localize::Namespace]: returning lexicon from $klass\n"; 
     79            require Data::Dumper; 
     80            print STDERR Data::Dumper::Dumper(\%{"$klass\::Lexicon"}); 
    8481        } 
     82        return \%{ "$klass\::Lexicon" } 
    8583    } 
    8684    return (); 
    87 } 
    88  
    89 sub format_string { 
    90     my ($self, $value, @args) = @_; 
    91     my $style = $self->style; 
    92     if ($style eq 'gettext') { 
    93         $value =~ s/%(\d+)/$args[$1 - 1] || ''/ge; 
    94     } elsif ($style eq 'maketext') { 
    95         $value =~ s/\[_(\d+)\]/$args[$1 - 1] || ''/ge; 
    96     } 
    97     return $value; 
    9885} 
    9986