blib/lib/Dancer/Plugin/I18N.pm

filter: Statementstmt 31.3% 62 / 198 Branchbran 3.7% 3 / 80 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 40.5% 15 / 37 Podpod n/a totaltotal 21.1% 80 / 378 SCAR 75.3
CC
60
Cov
21%
_setup_i18n
156.0
__ANON__
90.0
__ANON__
42.0
CRAP 1868.5
↑ Summary
1 package Dancer::Plugin::I18N;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Dancer::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use I18N::LangTags::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use I18N::LangTags::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Locale::Maketext::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '0.42';
20 2 our %options = (
21 Decode => 1,
22 Export => '_loc',
23  
24 #Encoding => 'utf-8',
25 );
26  
27 # Handler of struct
28 2 my $handle = undef;
29  
30 # Own subs definition
31 2 our @array_subs = ();
32  
33 # Settings
34 2 my $settings = undef;
35  
36 sub _load_i18n_settings {
37 2 ▶ $settings = plugin_setting();
Subroutine _load_i18n_settings: called CC=8 SCAR=37.1
38 2 my $n = $settings->{func};
39 2 ▶ if (!defined($n)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $n) { } 2 0 2
elsif (not ref $n and length $n) { } 0 0 0
elsif (ref $n eq 'ARRAY') { } 0 0 0
40 ▶ } elsif (!ref($n) && length($n)) {
Condition0%0/3
not ref $n and length $n
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $n and length $n
not ref $n length $n
Truth table0%0/3
not(A) and B
A= not ref $nB= length $n
ABresult
0X0
100
111
41 0 ▶ register $n => sub { _localize(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 push(@array_subs, $n);
43 } elsif (ref($n) eq "ARRAY") {
44 0 foreach my $k (@$n) {
45 0 ▶ register $k => sub { _localize(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 push(@array_subs, $k);
47 }
48 }
49 2 ▶ if ($settings->{setlocale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"setlocale"}) 0 2 2
50 0 eval { require Locale::Util; 1 };
51 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
52 0 $settings->{setlocale} = undef;
53 0 error("Couldn't initialize Locale::Util ... ", "$@");
54 }
55 }
56 }
57  
58 # Hook definitions
59 add_hook(
60 before => sub {
61 0 ▶ _setup_i18n();
Subroutine __ANON__: not called CC=9 SCAR=45.0
62  
63 # Changing and setting language
64 0 ▶ my $np = $settings->{name_param} || "lang";
Condition0%0/2
$settings->{'name_param'} || 'lang'
l!l
00
MC/DC0%0/1
$settings->{'name_param'} || 'lang'
$settings->{'name_param'}
Truth table0%0/2
A
A= $settings->{'name_param'}
Aresult
11
00
65 0 ▶ my $ns = $settings->{name_session} || "language";
Condition0%0/2
$settings->{'name_session'} || 'language'
l!l
00
MC/DC0%0/1
$settings->{'name_session'} || 'language'
$settings->{'name_session'}
Truth table0%0/2
A
A= $settings->{'name_session'}
Aresult
11
00
66 0 ▶ if ($np && $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($np and $ns) 0 0 0
Condition0%0/3
$np and $ns
!ll&&!rl&&r
000
MC/DC0%0/2
$np and $ns
$np $ns
Truth table0%0/3
A and B
A= $npB= $ns
ABresult
0X0
100
111
67 0 ▶ if (my $p = param $np) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $p = param($np)) { } 0 0 0
elsif (not session($ns)) { } 0 0 0
elsif (not language_tag(session($ns))) { } 0 0 0
68  
69 #my $s = session($ns);
70 #if ((!$s || $p ne $s) && (installed_languages($p))) {
71 0 ▶ if (installed_languages($p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (installed_languages($p)) 0 0 0
72 0 session $ns => $p;
73 0 languages($p);
74 }
75 } elsif (!session($ns)) {
76 0 session $ns => language_tag();
77 } elsif (!language_tag(session($ns))) {
78 0 languages(session($ns));
79 }
80 }
81 },
82 2 );
83  
84 add_hook(
85 before_template => sub {
86 0 ▶ my $tokens = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 0 ▶ $tokens->{l} = sub { l(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ $tokens->{localize} = sub { localize(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ $tokens->{language} = sub { language(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ $tokens->{language_tag} = sub { language_tag(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 ▶ $tokens->{languages} = sub { languages(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 ▶ $tokens->{installed_languages} = sub { installed_languages(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
93  
94 0 foreach my $k (@{Dancer::Plugin::I18N::array_subs}) {
95 0 ▶ $tokens->{$k} = sub { localize(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 }
97 },
98 2 );
99  
100 =encoding utf8
101  
102 =head1 NAME
103  
104 Dancer::Plugin::I18N - Internationalization for Dancer
105  
106 =head1 SYNOPSIS
107  
108 # MyApp/I18N/de.po
109 msgid "Hello Dancer"
110 msgstr "Hallo Tänzerin"
111
112 # MyApp/I18N/i_default.po
113 msgid "messages.hello.dancer"
114 msgstr "Hello Dancer - fallback translation"
115
116 # MyApp/I18N/fr.pm
117 package myapp::I18N::fr;
118 use base 'myapp::I18N';
119 our %Lexicon = ( hello => 'bonjour' );
120 1;
121  
122 package myapp;
123 use Dancer;
124 use Dancer::Plugin::I18N;
125 get '/' => sub {
126 my $lang = languages ;
127 print @$lang . "\n";
128 languages( ['de'] );
129 print STDERR localize('Hello Dancer');
130  
131 template 'index'
132 };
133  
134 # index.tt
135 hello in <% languages %> => <% l('hello') %>
136 # or
137 <% languages('fr') %>This is an <% l('hello') %>
138 # or
139 <% l('Hello Dancer') %>
140 <% l('Hello [_1]', 'Dancer') %>
141 <% l('lalala[_1]lalala[_2]', ['test', 'foo']) %>
142 <% l('messages.hello.dancer') %>
143 # or for big texts
144 <% IF language_tag('fr') %>
145 ...
146 <% ELSE %>
147 ...
148 <% ENDIF %>
149  
150  
151 =head1 DESCRIPTION
152  
153 Supports mo/po files and Maketext classes under your application's I18N namespace.
154  
155 Dancer::Plugin::I18N add L<Locale::Maketext::Simple> to your L<Dancer> application
156  
157 =cut
158  
159 sub _setup_i18n {
160  
161 0 ▶ return if (defined($handle) && ref($handle) eq "HASH");
Subroutine _setup_i18n: not called CC=12 SCAR=50.5
Branch: 1 branch
BranchTrueFalseTotal
if defined $handle and ref $handle eq "HASH" 0 0 0
Condition0%0/3
defined $handle and ref $handle eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $handle and ref $handle eq "HASH"
defined $handle ref $handle eq "HASH"
Truth table0%0/3
A and B
A= defined $handleB= ref $handle eq "HASH"
ABresult
0X0
100
111
162  
163 0 ▶ _load_i18n_settings() if (!$settings);
Branch: 1 branch
BranchTrueFalseTotal
unless $settings 0 0 0
164  
165 0 ▶ my $lang_path = $settings->{directory} || path(setting('appdir'), 'I18N');
Condition0%0/3
$settings->{'directory'} || path(setting('appdir'), 'I18N')
l!l&&r!l&&!r
000
MC/DC0%0/2
$settings->{'directory'} || path(setting('appdir'), 'I18N')
$settings->{'directory'} path(setting('appdir'), 'I18N')
Truth table0%0/3
A || B
A= $settings->{'directory'}B= path(setting('appdir'), 'I18N')
ABresult
1X1
011
000
166  
167 0 ▶ my $user_opts = $settings->{maketext_options} || {};
Condition0%0/2
$settings->{'maketext_options'} || {}
l!l
00
MC/DC0%0/1
$settings->{'maketext_options'} || {}
$settings->{'maketext_options'}
Truth table0%0/2
A
A= $settings->{'maketext_options'}
Aresult
11
00
168  
169 # Option should be defined as local, because we don't want to change global definition for this
170 0 local %options = (%options, Path => $lang_path, %$user_opts);
171  
172 #Locale::Maketext::Simple->import( %options );
173 0 my $self = __PACKAGE__;
174 0 eval <<END;
175 package $self;
176 Locale::Maketext::Simple->import( \%Dancer\::Plugin\::I18N\::options );
177 END
178  
179 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
180 0 error("Couldn't initialize i18n", "$@");
181 } else {
182 0 debug("Initialized i18n");
183 }
184  
185 =head1 CONFIGURATION
186  
187 You can override any parameter sent to L<Locale::Maketext::Simple> by specifying
188 a C<maketext_options> hashref to the C<Plugin::I18N> in you Dancer application
189 config file section. For example, the following configuration will override
190 the C<Decode> parameter which normally defaults to C<1>:
191  
192 plugins:
193 I18N:
194 directory: I18N
195 lang_default: en
196 maketext_options:
197 Decode: 0
198  
199 All languages fallback to MyApp::I18N which is mapped onto the i-default
200 language tag or change this via options 'language_default'.
201 If you use arbitrary message keys, use i_default.po to translate
202 into English, otherwise the message key itself is returned.
203  
204 Standart directory is in C<I18N>. In this directory are stored every lang files (*.pm|po|mo).
205  
206 You can defined own function for call locale via settings name C<func>.
207  
208 plugins:
209 I18N:
210 func: "N_"
211  
212 Or defined as array:
213  
214 plugins:
215 I18N:
216 func: ["N_", "_"]
217  
218 Now you can call this function in template or in libs.
219  
220 # index.tt
221 hello in <% languages %> => <% N_('hello') %>
222  
223  
224 Automaticaly change language via param 'lang', can be change in setting
225 via 'name_param' and will be stored in session in tag 'language' or
226 can be changed via 'name_session'. When you use this settings, this plugin automaticaly
227 setting language when you call param 'name_param'. Now if you call every page with
228 param 'lang=en' now plugin automatically set new locale.
229  
230 plugins:
231 I18N:
232 name_param: lang
233 name_session: language
234  
235  
236 Automaticaly settings locales must installed L<libintl-perl> in version 1.17 or newer.
237  
238 plugins:
239 I18N:
240 setlocale: "LC_TIME"
241  
242 Or defined as array:
243  
244 plugins:
245 I18N:
246 setlocale: ["LC_TIME","LC_NUMERIC"]
247  
248 When you set LC_TIME and use time function for print day name or month name, then will be printed in localed name.
249  
250 =cut
251  
252 # We re-read the list of files in $lang_path
253 # Originally tried to detect via namespaces, but this lists the currently set LANG envvar, which may not
254 # be a supported language. Also misses out .pm files
255 # Is acceptable to re-read this directory once on setup
256 0 my $languages_list = {};
257 0 ▶ if (opendir my $langdir, $lang_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $langdir, $lang_path) 0 0 0
258 0 foreach my $entry (readdir $langdir) {
259 0 ▶ next unless $entry =~ m/\A (\w+)\.(?:pm|po|mo) \z/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /\A (\w+)\.(?:pm|po|mo) \z/msx 0 0 0
260 0 my $langtag = $1;
261 0 ▶ next if $langtag eq "i_default";
Branch: 1 branch
BranchTrueFalseTotal
if $langtag eq "i_default" 0 0 0
262 0 my $language_tag = $langtag;
263  
264 #my $language_tag = "$class\::I18N"->get_handle( $langtag )->language_tag;
265 # Did use the get_handle, but that caused problems because en became "Default (Fallthru) Language"
266 # Just do a simple convert instead
267 0 $language_tag =~ s/_/-/g;
268 0 $languages_list->{$langtag} = I18N::LangTags::List::name($language_tag);
269 }
270 0 closedir $langdir;
271 }
272  
273 0 $handle = {};
274 0 $handle->{installed_languages} = $languages_list;
275  
276 # Setting language
277 0 my $request = request;
278 0 my @languages = ();
279 0 push @languages,
280 I18N::LangTags::implicate_supers(I18N::LangTags::Detect->http_accept_langs(scalar $request->accept_language));
281 0 ▶ push(@languages, $settings->{lang_default} || 'i-default');
Condition0%0/2
$settings->{'lang_default'} || 'i-default'
l!l
00
MC/DC0%0/1
$settings->{'lang_default'} || 'i-default'
$settings->{'lang_default'}
Truth table0%0/2
A
A= $settings->{'lang_default'}
Aresult
11
00
282 0 $handle->{languages} = \@languages;
283 0 _setup_lang();
284 }
285  
286 # Problem is where settings is codepage UTF8 and must be encode to ASCII
287 sub _txt2ascii {
288 0 ▶ return $_[0] ? Encode::encode("ISO-8859-1", $_[0]) : '';
Subroutine _txt2ascii: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
289 }
290  
291 # Setting locale
292 sub _set_locale {
293 0 ▶ my $lang = shift || return;
Subroutine _set_locale: not called CC=6 SCAR=33.1
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
294 0 my $charset = shift;
295  
296 0 foreach my $l (@_) {
297 0 my $s = &_txt2ascii($l);
298 0 foreach my $k ("ALL", "COLLATE", "CTYPE", "MESSAGES", "MONETARY", "NUMERIC", "TIME") {
299 0 ▶ if ($s eq ("LC_" . $k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq 'LC_' . $k) 0 0 0
300 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301 0 my $c = &{"POSIX::LC_" . $k};
302 0 ▶ Locale::Util::web_set_locale($lang, $charset, $c)
Branch: 1 branch
BranchTrueFalseTotal
if defined $c 0 0 0
303 if (defined($c));
304 0 last;
305 }
306 }
307 }
308 }
309  
310 sub _setup_lang {
311 0 ▶ return if (!$handle || !exists($handle->{languages}));
Subroutine _setup_lang: not called CC=7 SCAR=36.8
Branch: 1 branch
BranchTrueFalseTotal
unless $handle and exists $handle->{"languages"} 0 0 0
Condition0%0/3
$handle and exists $handle->{"languages"}
!ll&&!rl&&r
000
MC/DC0%0/2
$handle and exists $handle->{"languages"}
$handle exists $handle->{"languages"}
Truth table0%0/3
A and B
A= $handleB= exists $handle->{"languages"}
ABresult
0X0
100
111
312 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 0 my $c = __PACKAGE__;
314 0 &{$c . '::_loc_lang'}(@{$handle->{languages}});
315  
316 #loc_lang( @{ $handle->{languages} } );
317  
318 0 ▶ _load_i18n_settings() if (!$settings);
Branch: 1 branch
BranchTrueFalseTotal
unless $settings 0 0 0
319  
320 # Set locale from config
321 0 ▶ if (my $s = $settings->{setlocale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $settings->{"setlocale"}) 0 0 0
322 0 ▶ &_set_locale(language_tag(), &_txt2ascii(setting('charset')) || undef, (ref($s) eq "ARRAY" ? @$s : $s));
Branch: 1 branch
BranchTrueFalseTotal
ref $s eq 'ARRAY' ? : 0 0 0
Condition0%0/2
&_txt2ascii(setting('charset')) || undef
l!l
00
MC/DC0%0/1
&_txt2ascii(setting('charset')) || undef
&_txt2ascii(setting('charset'))
Truth table0%0/2
A
A= &_txt2ascii(setting('charset'))
Aresult
11
00
323 }
324 }
325  
326 =head1 METHODS
327  
328 =head2 languages
329  
330 Contains languages.
331  
332 languages(['de_DE']);
333 my $lang = languages;
334 print join '', @$lang;
335  
336 =head3 1. Putting new language as first in finded
337  
338 languages('de_DE');
339  
340 =head3 2. Erase all and putting new languages as in arrayref
341  
342 languages(['de_DE',....,'en']);
343  
344 =head3 3. Return putted languages
345  
346 languages();
347  
348 =cut
349  
350 register languages => sub {
351 0 ▶ my $lang = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
352  
353 0 _setup_i18n();
354 0 ▶ return if (!$handle);
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 0 0
355  
356 0 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) { } 0 0 0
357 0 ▶ if (ref($lang) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lang eq 'ARRAY') { } 0 0 0
358 0 $handle->{languages} = $lang;
359 } else {
360 0 ▶ for (my $i = 0; $i < scalar(@{$handle->{languages}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$handle->{"languages"};}) 0 0 0
361 0 ▶ if ($handle->{languages}->[$i] eq $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"languages"}[$i] eq $lang) 0 0 0
362 0 splice(@{$handle->{languages}}, $i, 1);
363 0 last;
364 }
365 }
366 0 unshift(@{$handle->{languages}}, $lang);
367 }
368  
369 0 _setup_lang();
370 } else {
371 0 return $handle->{languages};
372 }
373 0 return;
374 2 };
375  
376 =head2 language
377  
378 return selected locale in your locales list or check if given locale is used(same as language_tag).
379  
380 =cut
381  
382 register language => sub {
383 0 ▶ my $lang_test = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
384  
385 0 ▶ return language_tag($lang_test) if (defined($lang_test));
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang_test 0 0 0
386  
387 0 my $c = __PACKAGE__;
388 0 ▶ my $class = ref $c || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
389 0 ▶ my $lang = $handle ? "$class\::I18N"->get_handle(@{$handle->{languages}}) : "";
Branch: 1 branch
BranchTrueFalseTotal
$handle ? : 0 0 0
390 0 $lang =~ s/.*:://;
391  
392 0 return $lang;
393 2 };
394  
395 =head2 language_tag
396  
397 return language tag for current locale. The most notable difference from this
398 method in comparison to C<language()> is typically that languages and regions
399 are joined with a dash and not an underscore.
400  
401 language(); # en_us
402 language_tag(); # en-us
403  
404 =head3 1. Returning selected locale
405  
406 print language_tag();
407  
408 =head3 2. Test if given locale used
409  
410 if (language_tag('en')) {}
411  
412 =cut
413  
414 register language_tag => sub {
415 0 ▶ my $lang_test = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
416  
417 0 my $c = __PACKAGE__;
418 0 ▶ my $class = ref $c || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
419 my $ret =
420 $handle
421 0 ▶ ? "$class\::I18N"->get_handle(@{$handle->{languages}})->language_tag
Branch: 1 branch
BranchTrueFalseTotal
$handle ? : 0 0 0
422 : "";
423  
424 0 ▶ if (defined($lang_test)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang_test) 0 0 0
425 0 ▶ return 1 if ($ret eq $lang_test || $ret =~ /^$lang_test/);
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq $lang_test or $ret =~ /^$lang_test/ 0 0 0
Condition0%0/3
$ret eq $lang_test or $ret =~ /^$lang_test/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ret eq $lang_test or $ret =~ /^$lang_test/
$ret eq $lang_test $ret =~ /^$lang_test/
Truth table0%0/3
A or B
A= $ret eq $lang_testB= $ret =~ /^$lang_test/
ABresult
1X1
011
000
426 0 return 0;
427 }
428  
429 0 return $ret;
430 2 };
431  
432 =head2 installed_languages
433  
434 Returns a hash of { langtag => "descriptive name for language" } based on language files
435 in your application's I18N directory. The descriptive name is based on I18N::LangTags::List information.
436 If the descriptive name is not available, will be undef.
437  
438 =head3 1. Returning hashref installed language files
439  
440 my $l = installed_languages();
441  
442 =head3 2. Test if given locale is installed in hashref
443  
444 my $t = installed_languages('en');
445
446  
447 =cut
448  
449 register installed_languages => sub {
450 0 ▶ if (defined($handle)) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handle) 0 0 0
451 0 ▶ if (defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
452 return 1
453 if ( $handle->{installed_languages}
454 0 ▶ && $handle->{installed_languages}->{$_[0]});
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"installed_languages"} and $handle->{"installed_languages"}{$_[0]} 0 0 0
Condition0%0/3
$handle->{"installed_languages"} and $handle->{"installed_languages"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$handle->{"installed_languages"} and $handle->{"installed_languages"}{$_[0]}
$handle->{"installed_languages"} $handle->{"installed_languages"}{$_[0]}
Truth table0%0/3
A and B
A= $handle->{"installed_languages"}B= $handle->{"installed_languages"}{$_[0]}
ABresult
0X0
100
111
455 0 return 0;
456 }
457 0 return $handle->{installed_languages};
458 }
459 2 };
460  
461 =head2 localize | l
462  
463 Localize text.
464  
465 print localize( 'Welcome to Dancer, [_1]', 'sri' );
466  
467 is same as
468  
469 print l( 'Welcome to Dancer, [_1]', 'sri' );
470  
471 or in template
472
473 <% l('Welcome to Dancer, [_1]', 'sri' ) %>
474  
475 =cut
476  
477 0 ▶register localize => sub { _localize(@_); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
478 0 ▶register l => sub { _localize(@_); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
479  
480 sub _localize {
481 0 ▶ _setup_i18n();
Subroutine _localize: not called CC=4 SCAR=25.8
482 0 ▶ return '' if (scalar(@_) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 0 0 0 0
483 0 ▶ return join '', @_ if (!defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 0 0
484  
485 #return loc( $_[0], @{ $_[1] } ) if ( ref $_[1] eq 'ARRAY' );
486 #return loc(@_);
487 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
488 0 my $c = __PACKAGE__;
489 0 ▶ return &{$c . '::_loc'}($_[0], @{$_[1]}) if (ref $_[1] eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "ARRAY" 0 0 0
490 0 return &{$c . '::_loc'}(@_);
491 }
492  
493 =head1 OUTLINE
494  
495 $ dancer -a MyAPP
496 $ cd MyAPP
497 $ mkdir I18N
498 $ xgettext.pl --output=I18N/messages.pot --directory=lib/
499 $ ls I18N/
500 messages.pot
501  
502 $ msginit --input=messages.pot --output=sv.po --locale=sv.utf8
503 Created I18N/sv.po.
504  
505 $ vim I18N/sv.po
506  
507 "Content-Type: text/plain; charset=utf-8\n"
508  
509 #: lib/MyApp.pm:50
510 msgid "Guest"
511 msgstr "Gäst"
512
513 #. ($name)
514 #: lib/MyApp.pm:54
515 msgid "Welcome %1!"
516 msgstr "Välkommen %1!"
517  
518 $ xgettext.pl --output=I18N/messages.pot --directory=view/
519 $ msgmerge --update I18N/sv.po I18N/messages.pot
520 . done.
521  
522 # compile message catalog to binary format
523 $ msgfmt --output-file=I18N/sv.mo I18N/sv.po
524  
525 =head1 SEE ALSO
526  
527 L<Dancer>
528  
529 L<Catalyst::Plugin::I18N>
530  
531 =head1 AUTHOR
532  
533 Igor Bujna E<lt>igor.bujna@post.czE<gt>
534  
535 =head1 ACKNOWLEDGEMENTS
536  
537 Thanks for authors of L<Catalyst::Plugin::I18N> with idea how make it.
538  
539 Franck Cuny E<lt>franck@lumberjaph.netE<gt> for L<Dancer::Plugin:i18n>
540  
541 Alexandre (Midnite) Jousset
542  
543 =head1 LICENSE
544  
545 This library is free software; you can redistribute it and/or modify
546 it under the same terms as Perl itself.
547  
548 =cut
549  
550 2 ▶_load_i18n_settings() if (!$settings);
Branch: 1 branch
BranchTrueFalseTotal
unless $settings 2 0 2
551 2 register_plugin;
552  
553 2 1;
↑ Summary