| 1 | # ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package CatalystX::I18N::DataLocalize; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | extends qw(Data::Localize); | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Path::Class; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use CatalystX::I18N::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Data::Localize::Format::Gettext; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | sub gettext_formatter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gettext_formatter: called CC=1 SCAR=0.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Taken from Locale::Maketext | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | my $numf = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | my ($lang, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my $num = shift(@$args); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | if($num < 10_000_000_000 and $num > -10_000_000_000 and $num == int($num)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$num < '10000000000' and $num > '-10000000000' and $num == int $num
$num < '10000000000' and $num > '-10000000000'
MC/DC0%0/3
$num < '10000000000' and $num > '-10000000000' and $num == int $num
$num < '10000000000'
$num > '-10000000000'
$num == int $num
Truth table25%1/4
A and B and C
A= $num < '10000000000'B= $num > '-10000000000'C= $num == int $num
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | $num += 0; # Just use normal integer stringification. | |||||||||||||||||||||||||||||||||||||||||
| 22 | # Specifically, don't let %G turn ten million into 1E+007 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $num = CORE::sprintf('%G', $num); | |||||||||||||||||||||||||||||||||||||||||
| 26 | # "CORE::" is there to avoid confusion with the above sub sprintf. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | while( $num =~ s/^([-+]?\d+)(\d{3})/$1,$2/s ) {1} # right from perlfaq5 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | # The initial \d+ gobbles as many digits as it can, and then we | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # backtrack so it un-eats the rightmost three, and then we | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # insert the comma there. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # This is just a lame hack instead of using Number::Format | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | return $num; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | my $numerate = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.9
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | my ($lang, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | my $num = shift(@$args); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # return this lexical item in a form appropriate to this number | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | my $s = ($num == 1); | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | return '' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | unless scalar(@$args); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | if(scalar(@$args) == 1) { # only the headword form specified | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $s ? $args->[0] : ($args->[0] . 's'); # very cheap hack. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | else { # sing and plural were specified | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | return $s ? $args->[0] : $args->[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | my $formatter = Data::Localize::Format::Gettext->new( | ||||||||||||||||||||||||||||||||||||||||||
| 56 | functions => { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | quant => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | my ($lang, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | my $num = shift(@$args); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | return $num if scalar(@$args) == 0; # what should this mean? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | return $args->[2] if scalar(@$args) > 2 and $num == 0; # special zeroth case | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @$args > 2 and $num == 0
MC/DC0%0/2
scalar @$args > 2 and $num == 0
scalar @$args > 2
$num == 0
Truth table33%1/3
A and B
A= scalar @$args > 2B= $num == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | return $numf->($lang,[$num]) . ' ' . $numerate->($lang,[$num,@$args]); | |||||||||||||||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 3 | numerate => $numerate, | |||||||||||||||||||||||||||||||||||||||||
| 66 | numf => $numf, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | return $formatter; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub add_localizers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_localizers: called CC=16 SCAR=34.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | my $locales = $params{locales} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'locales'} || []
MC/DC0%0/1
$params{'locales'} || []
$params{'locales'}
Truth table50%1/2
A
A= $params{'locales'}
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | my $directories = $params{directories}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | $directories = [ $directories ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $directories and ref $directories ne "ARRAY"
MC/DC0%0/2
defined $directories and ref $directories ne "ARRAY"
defined $directories
ref $directories ne "ARRAY"
Truth table33%1/3
A and B
A= defined $directoriesB= ref $directories ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | if defined $directories | ||||||||||||||||||||||||||||||||||||||||||
| 81 | && ref $directories ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $directories ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$directories ||= []
MC/DC0%0/1
$directories ||= []
$directories
Truth table50%1/2
A
A= $directories
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | $locales = [ $locales ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | unless ref $locales eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | die "Invalid locales" | ||||||||||||||||||||||||||||||||||||||||||
| 87 | unless defined $locales | ||||||||||||||||||||||||||||||||||||||||||
| 88 | && scalar @$locales > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | && ! grep { $_ !~ $CatalystX::I18N::TypeConstraints::LOCALE_RE } @$locales; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $locales and scalar @$locales > 0 and not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales)
defined $locales and scalar @$locales > 0
MC/DC0%0/3
defined $locales and scalar @$locales > 0 and not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales)
defined $locales
scalar @$locales > 0
not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales)
Truth table25%1/4
A and B and not(C)
A= defined $localesB= scalar @$locales > 0C= not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales)
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | my $formatter; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # Loop all directories | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | foreach my $directory (@$directories) { | |||||||||||||||||||||||||||||||||||||||||
| 96 | next | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | unless defined $directory; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | ▶ | $directory = Path::Class::Dir->new($directory) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | unless ref $directory eq 'Path::Class::Dir'; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | next | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | unless -d $directory->stringify && -e _ && -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
-d $directory->stringify and -e _ and -r _
-d $directory->stringify and -e _
MC/DC0%0/3
-d $directory->stringify and -e _ and -r _
-d $directory->stringify
-e _
-r _
Truth table25%1/4
A and B and C
A= -d $directory->stringifyB= -e _C= -r _
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | $formatter ||= $self->gettext_formatter; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$formatter ||= $self->gettext_formatter
MC/DC0%0/2
$formatter ||= $self->gettext_formatter
$formatter
$self->gettext_formatter
Truth table33%1/3
A ||= B
A= $formatterB= $self->gettext_formatter
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | $self->add_localizer( | |||||||||||||||||||||||||||||||||||||||||
| 108 | class => "Gettext", | ||||||||||||||||||||||||||||||||||||||||||
| 109 | path => $directory.'/*.po', | ||||||||||||||||||||||||||||||||||||||||||
| 110 | formatter => $formatter, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | CatalystX::I18N::DataLocalize - Wrapper around Data::Localize | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | package MyApp::DataLocalize; | ||||||||||||||||||||||||||||||||||||||||||
| 128 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | extends qw(CatalystX::I18N::DataLocalize); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | This class can be used as your Data Localize base-class. It is a wrapper around | ||||||||||||||||||||||||||||||||||||||||||
| 134 | L<Data::Localize> and provides methods for auto-loading po files. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | It is designed to work toghether with L<CatalystX::Model::DataLocalize>. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | You need to subclass this package in your project in order to use it. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head1 MEDTHODS | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =head3 load_lexicon | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | MyApp::DataLocalize->add_localizer( | ||||||||||||||||||||||||||||||||||||||||||
| 144 | locales => ['de','de_AT'], # Required | ||||||||||||||||||||||||||||||||||||||||||
| 145 | directories => ['/path/to/your/maketext/files'], # Required | ||||||||||||||||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | This method will search the given directories and load all available *.po | ||||||||||||||||||||||||||||||||||||||||||
| 149 | files. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | This class provides only the most basic functionality and probably needs | ||||||||||||||||||||||||||||||||||||||||||
| 152 | to be re-implemented according to your specific requirements. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | L<Data::Localize> | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | Maroš Kollár | ||||||||||||||||||||||||||||||||||||||||||
| 161 | CPAN ID: MAROS | ||||||||||||||||||||||||||||||||||||||||||
| 162 | maros [at] k-1.com | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | L<http://www.k-1.com> | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||