Jifty/I18N.pm

filter: Statementstmt 28.3% 44 / 155 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 56.5% 13 / 23 Podpod 100% 8 / 8 totaltotal 23.3% 65 / 278 CC 40 SCAR 40.8
CC
40
Cov
19%
get_language_handle
156.0
new
110.0
promote_encoding
56.0
CRAP 59.1
« Jifty/Web/PageRegion.pm ↑ Summary Jifty/Config.pm »
1 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::I18N;
5 113 ▶use base 'Locale::Maketext';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 113 ▶use Locale::Maketext::Lexicon ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use Email::MIME::ContentType;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 113 ▶use Encode::Guess qw(iso-8859-1);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 113 ▶use Jifty::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Jifty::I18N - Internationalization framework for Jifty
14  
15 =head1 SYNOPSIS
16  
17 # Whenever you need an internationalized string:
18 print _('Hello, %1!', 'World');
19  
20 In your Mason templates:
21  
22 <% _('Hello, %1!', 'World') %>
23  
24 =head1 METHODS
25  
26 =head2 C<_>
27  
28 This module provides a method named C<_>, which allows you to quickly and easily include localized strings in your application. The first argument is the string to be translated. If that string contains placeholders, the remaining arguments are used to replace the placeholders. The placeholders in the form of "%1" where the number is the number of the argument used to replace it:
29  
30 _('Welcome %1 to the %2', 'Bob', 'World');
31  
32 This example would return the string "Welcome Bob to the World" if no translation is being performed.
33  
34 =cut
35  
36 =head2 new
37  
38 Set up Jifty's internationalization for your application. This pulls
39 in Jifty's PO files, your PO files and then exports the _ function into
40 the wider world.
41  
42 =cut
43  
44 113 my $DynamicLH;
45  
46 113 our $loaded;
47  
48 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=47.0
Pod documented
49 0 my $class = shift;
50 0 my $self = {};
51 0 bless $self, $class;
52  
53 # XXX: this requires a full review, LML->get_handle is calling new
54 # on I18N::lang each time, but we really shouldn't need to rerun
55 # the import here.
56 0 ▶ return $self if $loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $Jifty::I18N::loaded 0 0 0
57  
58 0 my @import = map {( Gettext => $_ )} _get_file_patterns();
59 0 ++$loaded;
60  
61 0 Locale::Maketext::Lexicon->import(
62 { '*' => \@import,
63 _decode => 1,
64 _auto => 1,
65 _style => 'gettext',
66 }
67 );
68  
69 # Allow hard-coded languages in the config file
70 0 my $lang = Jifty->config->framework('L10N')->{'Lang'};
71 0 ▶ $lang = [defined $lang ? $lang : ()] unless ref($lang) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $lang eq "ARRAY" 0 0 0
defined $lang ? : 0 0 0
72  
73 # Allow hard-coded allowed-languages in the config file
74 0 my $allowed_lang = Jifty->config->framework('L10N')->{'AllowedLang'};
75 0 ▶ $allowed_lang = [defined $allowed_lang ? $allowed_lang : ()] unless ref($allowed_lang) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $allowed_lang eq "ARRAY" 0 0 0
defined $allowed_lang ? : 0 0 0
76  
77 0 ▶ if (@$allowed_lang) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$allowed_lang) 0 0 0
78 my $allowed_regex = join '|', map {
79 0 my $it = $_;
80 0 $it =~ tr<-A-Z><_a-z>; # lc, and turn - to _
81 0 $it =~ tr<_a-z0-9><>cd; # remove all but a-z0-9_
82 0 $it;
83 } @$allowed_lang;
84  
85 0 foreach my $lang ($self->available_languages) {
86 # "AllowedLang: zh" should let both zh_tw and zh_cn survive,
87 # so we just check ^ but not $.
88 0 ▶ $lang =~ /^$allowed_regex/ or delete $Jifty::I18N::{$lang.'::'};
Branch: 1 branch
BranchTrueFalseTotal
$lang =~ /^$allowed_regex/ or delete $Jifty::I18N::{$lang . '::'} 0 0 0
89 }
90 }
91  
92 0 my $lh = $class->get_handle(@$lang);
93  
94 0 ▶ $DynamicLH = \$lh unless @$lang;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lang 0 0 0
95 0 $self->init;
96  
97 0 __PACKAGE__->install_global_loc($DynamicLH);
98 0 return $self;
99 }
100  
101 =head2 install_global_loc
102  
103 =cut
104  
105 0 ▶sub install_global_loc {
Subroutine install_global_loc: not called CC=1 SCAR=3.1
Pod documented
106 0 my ($class, $dlh) = @_;
107 0 ▶ my $loc_method = sub {
Subroutine __ANON__: not called CC=4 SCAR=26.4
108 # Retain compatibility with people using "-e _" etc.
109 0 ▶ return \*_ unless @_; # Needed for perl 5.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
110  
111 # When $_[0] is undef, return undef. When it is '', return ''.
112 113 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 0 ▶ return $_[0] unless (length $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 0 0 0
114  
115 0 local $@;
116 # Force stringification to stop Locale::Maketext from choking on
117 # things like DateTime objects.
118 0 my @stringified_args = map {"$_"} @_;
119 0 my $result = eval { ${$dlh}->maketext(@stringified_args) };
120 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
121 0 warn $@;
122 # Sometimes Locale::Maketext fails to localize a string and throws
123 # an exception instead. In that case, we just return the input.
124 0 return join(' ', @stringified_args);
125 }
126 0 return $result;
127 0 };
128  
129 {
130 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
131 113 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 0 *_ = $loc_method;
133 }
134 }
135  
136 =head2 available_languages
137  
138 Return an array of available languages
139  
140 =cut
141  
142 0 ▶sub available_languages {
Subroutine available_languages: not called CC=2 SCAR=17.9
Pod documented
143 0 ▶ return map { /^(\w+)::/ ? $1 : () } sort keys %Jifty::I18N::;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)::/ ? : 0 0 0
144 }
145  
146 =head2 _get_file_patterns
147  
148 Get list of patterns for all PO files in the project.
149 (Paths are gotten from the configuration variables and plugins).
150  
151 =cut
152  
153 0 ▶sub _get_file_patterns {
Subroutine _get_file_patterns: not called CC=5 SCAR=34.0
154 0 my @ret;
155  
156 0 push(@ret, Jifty->config->framework('L10N')->{'PoDir'});
157 0 push(@ret, Jifty->config->framework('L10N')->{'DefaultPoDir'});
158  
159 # Convert relative paths to absolute ones
160 0 @ret = map { Jifty::Util->absolute_path($_) } @ret;
161  
162 0 foreach my $plugin (Jifty->plugins) {
163 0 my $dir = $plugin->po_root;
164 0 ▶ next unless ($dir and -d $dir and -r $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $dir and -d $dir and -r $dir 0 0 0
Condition0%0/6
$dir and -d $dir and -r $dir
!ll&&!rl&&r
000
$dir and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/3
$dir and -d $dir and -r $dir
$dir -d $dir -r $dir
Truth table0%0/4
A and B and C
A= $dirB= -d $dirC= -r $dir
ABCresult
0XX0
10X0
1100
1111
165 0 push @ret, $dir ;
166 }
167  
168 # Unique-ify paths
169 0 my %seen;
170 0 @ret = grep {not $seen{$_}++} @ret;
171  
172 0 return ( map { $_ . '/*.po' } @ret );
173 }
174  
175 =head2 get_language_handle
176  
177 Get the language handle for this request.
178  
179 =cut
180  
181 0 ▶sub get_language_handle {
Subroutine get_language_handle: not called CC=12 SCAR=50.5
Pod documented
182 # XXX: subrequest should not need to get_handle again.
183 0 my $self = shift;
184 # optional argument makes it easy to disable I18N
185 # while comparing test strings (without loading session)
186 0 ▶ my $lang = shift || Jifty->web->session->get('jifty_lang');
Condition0%0/3
shift() || 'Jifty'->web->session->get('jifty_lang')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || 'Jifty'->web->session->get('jifty_lang')
shift() 'Jifty'->web->session->get('jifty_lang')
Truth table0%0/3
A || B
A= shift()B= 'Jifty'->web->session->get('jifty_lang')
ABresult
1X1
011
000
187  
188 0 ▶ if ( !$lang
Branch: 1 branch
BranchTrueFalseTotal
if (not $lang and "Jifty"->web->current_user and "Jifty"->web->current_user->id) 0 0 0
Condition0%0/6
not $lang and "Jifty"->web->current_user and "Jifty"->web->current_user->id
!ll&&!rl&&r
000
not $lang and "Jifty"->web->current_user
!ll&&!rl&&r
000
MC/DC0%0/3
not $lang and "Jifty"->web->current_user and "Jifty"->web->current_user->id
not $lang "Jifty"->web->current_user "Jifty"->web->current_user->id
Truth table0%0/4
not(A) and B and C
A= not $langB= "Jifty"->web->current_userC= "Jifty"->web->current_user->id
ABCresult
0XX0
10X0
1100
1111
189 && Jifty->web->current_user
190 && Jifty->web->current_user->id )
191 {
192 0 my $user = Jifty->web->current_user->user_object;
193 0 for my $column (qw/language lang/) {
194 0 ▶ if ( $user->can($column) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->can($column)) 0 0 0
195 0 $lang = $user->$column;
196 0 last;
197 }
198 }
199 }
200  
201 # I18N::LangTags::Detect wants these for detecting
202 0 ▶ local $ENV{REQUEST_METHOD} = Jifty->web->request->method
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->request 0 0 0
203 if Jifty->web->request;
204 0 ▶ local $ENV{HTTP_ACCEPT_LANGUAGE} = Jifty->web->request->header("Accept-Language") || ""
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->request 0 0 0
Condition0%0/2
'Jifty'->web->request->header('Accept-Language') || ''
l!l
00
MC/DC0%0/1
'Jifty'->web->request->header('Accept-Language') || ''
'Jifty'->web->request->header('Accept-Language')
Truth table0%0/2
A
A= 'Jifty'->web->request->header('Accept-Language')
Aresult
11
00
205 if Jifty->web->request;
206 0 ▶ $$DynamicLH = $self->get_handle($lang ? $lang : ()) if $DynamicLH;
Branch: 2 branches
BranchTrueFalseTotal
if $DynamicLH 0 0 0
$lang ? : 0 0 0
207 }
208  
209 =head2 get_current_language
210  
211 Get the current language for this request, formatted as a Locale::Maketext
212 subclass string (i.e., C<zh_tw> instead of C<zh-TW>).
213  
214 =cut
215  
216 0 ▶sub get_current_language {
Subroutine get_current_language: not called CC=2 SCAR=17.9
Pod documented
217 0 ▶ return unless $DynamicLH;
Branch: 1 branch
BranchTrueFalseTotal
unless $DynamicLH 0 0 0
218  
219 0 my ($lang) = ref($$DynamicLH) =~ m/::(\w+)$/;
220 0 return $lang;
221 }
222  
223 =head2 refresh
224  
225 Used by L<Jifty::Handler> in DevelMode to reload F<.po> files whenever they
226 are modified on disk.
227  
228 =cut
229  
230 113 my $LAST_MODIFED = '';
231 0 ▶sub refresh {
Subroutine refresh: not called CC=4 SCAR=24.4
Pod documented
232 0 ▶ if ( Jifty->config->framework('L10N')->{'Disable'} && !$loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->config->framework("L10N")->{"Disable"} and not $Jifty::I18N::loaded) 0 0 0
Condition0%0/3
"Jifty"->config->framework("L10N")->{"Disable"} and not $Jifty::I18N::loaded
!ll&&!rl&&r
000
MC/DC0%0/2
"Jifty"->config->framework("L10N")->{"Disable"} and not $Jifty::I18N::loaded
"Jifty"->config->framework("L10N")->{"Disable"} not $Jifty::I18N::loaded
Truth table0%0/3
A and not(B)
A= "Jifty"->config->framework("L10N")->{"Disable"}B= not $Jifty::I18N::loaded
ABresult
0X0
100
111
233 # skip loading po, but still do the translation for maketext
234 0 require Locale::Maketext::Lexicon;
235 0 my $lh = __PACKAGE__->get_handle;
236 0 my $orig = Jifty::I18N::en->can('maketext');
237 113 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 0 *Jifty::I18N::en::maketext = Locale::Maketext::Lexicon->_style_gettext($orig);
239 0 __PACKAGE__->install_global_loc(\$lh);
240 0 ++$loaded;
241 0 return;
242 }
243  
244 my $modified = join(
245 ',',
246 # sort map { $_ => -M $_ } map { glob("$_/*.po") } ( Jifty->config->framework('L10N')->{'PoDir'}, Jifty->config->framework('L10N')->{'DefaultPoDir'}
247 0 ▶ sort map { $_ => -M $_ } map { glob($_) } _get_file_patterns()
Subroutine BEGIN: called CC=1 SCAR=1.2
248 );
249 0 ▶ if ($modified ne $LAST_MODIFED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified ne $LAST_MODIFED) 0 0 0
250 0 Jifty::I18N->new;
251 0 $LAST_MODIFED = $modified;
252 }
253 }
254  
255  
256  
257 =head2 promote_encoding STRING [CONTENT-TYPE]
258  
259 Return STRING promoted to our best-guess of an appropriate
260 encoding. STRING should B<not> have the UTF-8 flag set when passed in.
261  
262 Optionally, you can pass a MIME content-type string as a second
263 argument. If it contains a charset= parameter, we will use that
264 encoding. Failing that, we use Encode::Guess to guess between UTF-8
265 and iso-latin-1. If that fails, and the string validates as UTF-8, we
266 assume that. Finally, we fall back on returning the string as is.
267  
268 =cut
269  
270 # XXX TODO This possibly needs to be more clever and/or configurable
271  
272 0 ▶sub promote_encoding {
Subroutine promote_encoding: not called CC=7 SCAR=40.3
Pod documented
273 0 my $class = shift;
274 0 my $string = shift;
275 0 my $content_type = shift;
276 0 my $charset;
277  
278 # Don't bother parsing the Content-Type header unless it mentions "charset".
279 # This is to avoid the "Unquoted / not allowed in Content-Type" warnings when
280 # the Base64-encoded MIME boundary string contains "/".
281 0 ▶ if ($content_type and $content_type =~ /charset/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type and $content_type =~ /charset/i) 0 0 0
Condition0%0/3
$content_type and $content_type =~ /charset/i
!ll&&!rl&&r
000
MC/DC0%0/2
$content_type and $content_type =~ /charset/i
$content_type $content_type =~ /charset/i
Truth table0%0/3
A and B
A= $content_typeB= $content_type =~ /charset/i
ABresult
0X0
100
111
282 0 $content_type = Email::MIME::ContentType::parse_content_type($content_type);
283 0 $charset = $content_type->{attributes}->{charset};
284 }
285  
286 # XXX TODO Is this the right thing? Maybe we should just return
287 # the string as-is.
288 0 Encode::_utf8_off($string);
289  
290 0 ▶ if($charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset) { } 0 0 0
291 0 $string = Encode::decode($charset, $string);
292 } else {
293 0 my $encoding = Encode::Guess->guess($string);
294 0 ▶ if(!ref($encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $encoding) { } 0 0 0
295 0 local $@;
296 0 eval {
297 # Try utf8
298 0 $string = Encode::decode_utf8($string, 1);
299 };
300 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
301 0 warn "Unknown encoding -- none specified, couldn't guess, not valid UTF-8";
302 }
303 } else {
304 0 ▶ $string = $encoding->decode($string) if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 0 0
305 }
306 }
307  
308 0 return $string;
309 }
310  
311 =head2 maybe_decode_utf8 STRING
312  
313 Attempt to decode STRING as UTF-8. If STRING is not valid UTF-8, or
314 already contains wide characters, return it undecoded.
315  
316 N.B: In an ideal world, we wouldn't need this function, since we would
317 know whether any given piece of input is UTF-8. However, the world is
318 not ideal.
319  
320 =cut
321  
322 0 ▶sub maybe_decode_utf8 {
Subroutine maybe_decode_utf8: not called CC=2 SCAR=17.9
Pod documented
323 0 my $class = shift;
324 0 my $string = shift;
325  
326 0 local $@;
327 0 eval {
328 0 $string = Encode::decode_utf8($string);
329 };
330 0 ▶ Carp::carp "Couldn't decode UTF-8: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
331 0 return $string;
332 }
333  
334 package Jifty::I18N::en;
335 113 ▶use base 'Locale::Maketext';
Subroutine BEGIN: called CC=1 SCAR=0.0
336 113 our %Lexicon = ( _fallback => 1, _AUTO => 1 );
337  
338 113 1;
« Jifty/Web/PageRegion.pm ↑ Summary Jifty/Config.pm »