Changeset 29642
- Timestamp:
- 02/06/09 17:57:54 (4 years ago)
- Location:
- lang/perl/Data-Localize/trunk
- Files:
-
- 4 added
- 2 modified
-
lib/Data/Localize/Gettext.pm (added)
-
lib/Data/Localize/Localizer.pm (modified) (1 diff)
-
lib/Data/Localize/Namespace.pm (modified) (5 diffs)
-
t/03_gettext.t (added)
-
t/lib/Test/Data/Localize/Gettext (added)
-
t/lib/Test/Data/Localize/Gettext/ja.po (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Data-Localize/trunk/lib/Data/Localize/Localizer.pm
r29633 r29642 3 3 package Data::Localize::Localizer; 4 4 use Moose::Role; 5 use Moose::Util::TypeConstraints; 5 6 6 requires 'localize_for', 'register'; 7 requires 'register'; 8 9 has 'style' => ( 10 is => 'rw', 11 isa => enum([qw(gettext maketext)]), 12 default => 'gettext', 13 ); 7 14 8 15 no Moose::Role; 16 no Moose::Util::TypeConstraints; 17 18 sub 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 32 sub 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 } 9 42 10 43 1; -
lang/perl/Data-Localize/trunk/lib/Data/Localize/Namespace.pm
r29636 r29642 3 3 package Data::Localize::Namespace; 4 4 use Moose; 5 use Moose::Util::TypeConstraints;6 5 use MooseX::AttributeHelpers; 7 6 use Module::Pluggable::Object; 8 7 9 8 with 'Data::Localize::Localizer'; 10 11 has 'style' => (12 is => 'rw',13 isa => enum([qw(gettext maketext)]),14 default => 'gettext',15 );16 9 17 10 has 'namespaces' => ( … … 28 21 29 22 no Moose; 30 no Moose::Util::TypeConstraints;31 23 32 24 sub register { … … 47 39 } 48 40 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 59 41 our %LOADED; 60 42 sub lexicon_get { … … 64 46 my $klass = "$namespace\::$lang"; 65 47 if (&Data::Localize::DEBUG) { 66 print STDERR " lexicon_get:Trying $klass\n";48 print STDERR "[Data::Localize::Namespace]: lexicon_get - Trying $klass\n"; 67 49 } 68 50 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'; 69 54 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 } 71 63 eval "require $klass"; 72 64 if ($@) { … … 77 69 } 78 70 } 71 if (&Data::Localize::DEBUG) { 72 print STDERR "[Data::Localize::Namespace]: lexicon_get - setting $klass to already loaded\n"; 73 } 79 74 $LOADED{$klass}++; 80 75 } 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"}); 84 81 } 82 return \%{ "$klass\::Lexicon" } 85 83 } 86 84 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;98 85 } 99 86
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)