CatalystX/I18N/Maketext.pm

filter: Statementstmt 93.7% 75 / 80 Branchbran 60.0% 24 / 40 Conditioncond 42.3% 11 / 26 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 71.1% 121 / 170 CC 35 SCAR 39.4
CC
35
Cov
75%
load_lexicon
62.9
BEGIN
1.0
BEGIN
1.0
CRAP 51.4
↑ Summary CatalystX/I18N/Role/GetLocale.pm »
1 # ============================================================================
2 package CatalystX::I18N::Maketext;
3 # ============================================================================
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use parent qw(Locale::Maketext);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Locale::Maketext::Lexicon;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use CatalystX::I18N::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Locale::Maketext::Lexicon::Gettext;
13 4 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶sub load_lexicon {
Subroutine load_lexicon: called CC=35 SCAR=41.4
Pod documented
16 3 my ( $class, %params ) = @_;
17  
18 3 ▶ my $locales = $params{locales} || [];
Condition50%1/2
$params{'locales'} || []
l!l
30
MC/DC0%0/1
$params{'locales'} || []
$params{'locales'}
Truth table50%1/2
A
A= $params{'locales'}
Aresult
11
00
19 3 my $directories = $params{directories};
20 3 ▶ my $gettext_style = defined $params{gettext_style} ? $params{gettext_style} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'gettext_style'} ? : 3 0 3
21 3 ▶ my $inheritance = $params{inheritance} || {};
Condition50%1/2
$params{'inheritance'} || {}
l!l
30
MC/DC0%0/1
$params{'inheritance'} || {}
$params{'inheritance'}
Truth table50%1/2
A
A= $params{'inheritance'}
Aresult
11
00
22  
23 3 ▶ $directories = [ $directories ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $directories and ref $directories ne "ARRAY" 0 3 3
Condition33%1/3
defined $directories and ref $directories ne "ARRAY"
!ll&&!rl&&r
030
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"
ABresult
0X0
100
111
24 if defined $directories
25 && ref $directories ne 'ARRAY';
26 3 ▶ $directories ||= [];
Condition50%1/2
$directories ||= []
l!l
30
MC/DC0%0/1
$directories ||= []
$directories
Truth table50%1/2
A
A= $directories
Aresult
11
00
27 3 ▶ $locales = [ $locales ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $locales eq "ARRAY" 0 3 3
28 unless ref $locales eq 'ARRAY';
29  
30 die "Invalid locales"
31 unless defined $locales
32 && scalar @$locales > 0
33 3 ▶ && ! grep { $_ !~ $CatalystX::I18N::TypeConstraints::LOCALE_RE } @$locales;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locales and scalar @$locales > 0 and not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales) 0 3 3
Condition33%2/6
defined $locales and scalar @$locales > 0 and not grep({not $_ =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/;} @$locales)
!ll&&!rl&&r
003
defined $locales and scalar @$locales > 0
!ll&&!rl&&r
003
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)
ABCresult
0XX0
10X0
1100
1111
34  
35 {
36 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 my $lexicon_loaded = ${$class.'::LEXICON_LOADED'};
38 3 ▶ if (defined $lexicon_loaded
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lexicon_loaded and $lexicon_loaded == 1) 0 3 3
Condition33%1/3
defined $lexicon_loaded and $lexicon_loaded == 1
!ll&&!rl&&r
300
MC/DC0%0/2
defined $lexicon_loaded and $lexicon_loaded == 1
defined $lexicon_loaded $lexicon_loaded == 1
Truth table33%1/3
A and B
A= defined $lexicon_loadedB= $lexicon_loaded == 1
ABresult
0X0
100
111
39 && $lexicon_loaded == 1) {
40 0 warn "Lexicon has already been loaded for $class";
41 0 return;
42 }
43 }
44  
45 3 my $lexicondata = {
46 _decode => 1,
47 };
48 3 ▶ $lexicondata->{_style} = 'gettext'
Branch: 1 branch
BranchTrueFalseTotal
if $gettext_style 3 0 3
49 if $gettext_style;
50  
51 3 my %locale_loaded;
52  
53 # Loop all directories
54 3 foreach my $directory (@$directories) {
55 next
56 3 ▶ unless defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $directory 0 3 3
57  
58 3 ▶ $directory = Path::Class::Dir->new($directory)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $directory eq "Path::Class::Dir" 0 3 3
59 unless ref $directory eq 'Path::Class::Dir';
60  
61 next
62 3 ▶ unless -d $directory->stringify && -e _ && -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory->stringify and -e _ and -r _ 0 3 3
Condition33%2/6
-d $directory->stringify and -e _ and -r _
!ll&&!rl&&r
003
-d $directory->stringify and -e _
!ll&&!rl&&r
003
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 _
ABCresult
0XX0
10X0
1100
1111
63  
64 3 my @directory_content = $directory->children();
65  
66 # Load all avaliable message files
67 3 foreach my $locale (@$locales) {
68 18 my $lc_locale = lc($locale);
69 18 $lc_locale =~ s/-/_/g;
70 18 my @locale_lexicon;
71 18 foreach my $content (@directory_content) {
72 72 ▶ if ($content->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->is_dir) { } 0 72 72
73 0 ▶ push(@locale_lexicon,'Slurp',$content->stringify)
Branch: 1 branch
BranchTrueFalseTotal
if $content->basename eq $locale 0 0 0
74 if $content->basename eq $locale;
75 } else {
76 72 my $filename = $content->basename;
77 72 ▶ if ($filename =~ m/^$locale\.(mo|po)$/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($filename =~ /^$locale\.(mo|po)$/i) { } 9 63 72
elsif ($filename =~ /^$locale\.m$/i) { } 0 63 63
elsif ($filename =~ /^$locale\.db$/i) { } 0 63 63
elsif ($filename =~ /^$lc_locale\.pm$/) { } 3 60 63
78 9 push(@locale_lexicon,'Gettext',$content->stringify);
79 } elsif ($filename =~ m/^$locale\.m$/i) {
80 0 push(@locale_lexicon,'Msgcat',$content->stringify);
81 } elsif($filename =~ m/^$locale\.db$/i) {
82 0 push(@locale_lexicon,'Tie',[ $class, $content->stringify ]);
83 } elsif ($filename =~ m/^$lc_locale\.pm$/) {
84 3 $locale_loaded{$locale} = 1;
85 3 require $content->stringify;
86 # TODO transform maketext -> gettext syntax if flag is set
87 # Locale::Maketext::Lexicon::Gettext::_gettext_to_maketext
88 }
89 }
90 }
91 18 ▶ $lexicondata->{$locale} = \@locale_lexicon
Branch: 1 branch
BranchTrueFalseTotal
if scalar @locale_lexicon 9 9 18
92 if scalar @locale_lexicon;
93 }
94 }
95  
96 # Fallback lexicon
97 3 foreach my $locale (@$locales) {
98 next
99 18 ▶ if exists $inheritance->{$locale};
Branch: 1 branch
BranchTrueFalseTotal
if exists $inheritance->{$locale} 12 6 18
100 next
101 6 ▶ if exists $locale_loaded{$locale};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locale_loaded{$locale} 0 6 6
102 6 ▶ $lexicondata->{$locale} ||= ['Auto'];
Condition100%2/2
$lexicondata->{$locale} ||= ["Auto"]
l!l
33
MC/DC100%1/1
$lexicondata->{$locale} ||= ["Auto"]
$lexicondata->{$locale}
Truth table100%2/2
A
A= $lexicondata->{$locale}
Aresult
11
00
103 }
104  
105 3 eval qq[
106 package $class;
107 our \$LEXICON_LOADED = 1;
108 Locale::Maketext::Lexicon->import(\$lexicondata)
109 ];
110  
111 3 ▶ while (my ($locale,$inherit) = each %$inheritance) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$inheritance) 12 3 15
112 12 my $locale_class = lc($locale);
113 12 my $inherit_class = lc($inherit);
114 12 $locale_class =~ s/-/_/g;
115 12 $inherit_class =~ s/-/_/g;
116 12 $locale_class = $class.'::'.$locale_class;
117 12 $inherit_class = $class.'::'.$inherit_class;
118 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 12 push(@{$locale_class.'::ISA'},$inherit_class);
120 }
121  
122 3 ▶ die("Could not load Locale::Maketext::Lexicon") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
123 3 return;
124 }
125  
126 #sub set_lexicon {
127 # my ( $class, $locale, $lexicon ) = @_;
128 #
129 # $locale = lc($locale);
130 # $locale =~ s/-/_/g;
131 #
132 # no strict 'refs';
133 # %{$class .'::'.$locale.'::Lexicon'} = %{$lexicon};
134 # return;
135 #}
136  
137 4 1;
138  
139 =encoding utf8
140  
141 =head1 NAME
142  
143 CatalystX::I18N::Maketext - Wrapper around Locale::Maketext
144  
145 =head1 SYNOPSIS
146  
147 package MyApp::Maketext;
148 use parent qw(CatalystX::I18N::Maketext);
149  
150 =head1 DESCRIPTION
151  
152 This class can be used as your Maketext base-class. It is a wrapper around
153 L<Locale::Maketext> and provides methods for auto-loading lexicon files.
154 It is designed to work toghether with L<CatalystX::Model::Maketext>.
155  
156 You need to subclass this package in your project in order to use it.
157  
158 =head1 MEDTHODS
159  
160 =head3 load_lexicon
161  
162 MyApp::Maketext->load_lexicon(
163 locales => ['de','de_AT'], # Required
164 directories => ['/path/to/your/maketext/files'], # Required
165 gettext_style => 0, # Optional, Default 1
166 inheritance => { # Optional
167 de_AT => 'de',
168 },
169 );
170  
171 This method will search the given directories and load all available maketext
172 files for the requested locales
173  
174 =over
175  
176 =item * *.mo, *.po
177  
178 via L<Locale::Maketext::Lexicon::Gettext>
179  
180 =item * *.db
181  
182 via L<Locale::Maketext::Lexicon::Tie> The files will be tied to you Maketext
183 class, thus you need to implement the necessary tie methods in your class.
184  
185 =item * *.m
186  
187 via L<Locale::Maketext::Lexicon::Msgcat>
188  
189 =item * Directories
190  
191 via L<Locale::Maketext::Lexicon::Slurp>
192  
193 =item * Perl Packages
194  
195 Will be loaded (only lowercase locale names e.g. locale 'de_AT' will only
196 load 'de_at.pm'). The packages must have a C<%Lexion> variable.
197  
198 =back
199  
200 If no translation files can be found for a given locale then
201 L<Locale::Maketext::Lexicon::Auto> will be loaded.
202  
203 The following parameters are recognized/required
204  
205 =over
206  
207 =item * locales
208  
209 Array reference of locales.
210  
211 Required
212  
213 =item * directories
214  
215 Array reference of directories. Also accepts L<Path::Class::Dir> objects
216 and single values.
217  
218 Required
219  
220 =item * gettext_style
221  
222 Enable gettext style. C<%quant(%1,document,documents)> instead of
223 C<[quant,_1,document,documents]>
224  
225 Optional, Default TRUE
226  
227 =item * inheritance
228  
229 Set inheritance as as HashRef (e.g. 'en_US' inherits from 'en')
230  
231 Optional
232  
233 =back
234  
235 =head1 SEE ALSO
236  
237 L<Locale::Maketext> and L<Locale::Maketext::Lexicon>
238  
239 =head1 AUTHOR
240  
241 Maroš Kollár
242 CPAN ID: MAROS
243 maros [at] k-1.com
244
245 L<http://www.k-1.com>
246 Í
↑ Summary CatalystX/I18N/Role/GetLocale.pm »