blib/lib/Database/BI/I18N.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 88.8% 16 / 18 Conditioncond 76.4% 26 / 34 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 91.6% 11 / 12 Podpod 100% 6 / 6 totaltotal 84.5% 131 / 155 CC 22 SCAR 14.0
CC
22
Cov
90%
text
6.0
choose
6.0
from_locale
4.1
CRAP 4.0
« blib/lib/Database/BI/Ask.pm ↑ Summary blib/lib/Database/BI/Extract.pm »
1 package Database::BI::I18N;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '0.023.1';
7  
8 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Which language a message is given in, and the text it is given as.
12 #
13 # Every module keeps its messages in British English, in its own %MESSAGES.
14 # Another language is a module of its own, Database::BI::I18N::<code>, holding
15 # for each package only the messages that read differently; a message it does
16 # not list is the British one. American English (en_us) differs in a handful
17 # of spellings, so that is all it holds.
18 #
19 # The language is chosen by CGI::Lingua, in choose(), from an Accept-Language
20 # header or, on the command line, the locale (from_locale()). A request sets
21 # $LANGUAGE for as long as it is being answered (Database::BI's around_dispatch
22 # hook, with local), so a message raised anywhere under it - a model's croak
23 # included - is in the reader's language without the language being passed
24 # down. A script sets it from the locale when it starts. Nothing else does:
25 # a module called from other code gives its messages in British English, so
26 # what it croaks with does not depend on the environment it happens to run in
27 # (a test suite run under LANG=en_US.UTF-8 matches the British messages).
28  
29 # The language the messages are written in, and the ones a module adds.
30 11 Readonly my $BASE => 'en';
31 11 Readonly my @LANGUAGES => ('en', 'en-us');
32  
33 # The language of the request being answered, or the script's; undef
34 # (British) otherwise.
35 11 our $LANGUAGE;
36  
37 11 my %TABLE; # language => { package => { key => text } }, loaded on first use
38 my %FROM_LOCALE; # locale string => language, so a script asks CGI::Lingua once
39  
40 # languages() -> @codes
41 0 ▶sub languages { return @LANGUAGES }
Subroutine languages: not called CC=1 SCAR=6.9
Pod documented
42  
43 # choose(%args) -> $code
44 #
45 # accept => an Accept-Language value (optional)
46 # default => the answer when it names nothing supported (default 'en')
47 #
48 # The supported language that suits it best: 'en-us' for en-US, 'en' for every
49 # other English. CGI::Lingua answers the language and the region apart, and
50 # names a region only when that variant is supported.
51 2195 ▶sub choose {
Subroutine choose: called CC=6 SCAR=17.9
Pod documented
52 2195 my (%args) = @_;
53  
54 2195 my $default = $args{default};
55 2195 ▶ $default = $BASE unless defined $default && grep { $_ eq $default } @LANGUAGES;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default and grep {$_ eq $default;} @LANGUAGES 14 2181 2195
Condition100%3/3
defined $default and grep {$_ eq $default;} @LANGUAGES
!ll&&!rl&&r
1312181
MC/DC100%2/2
defined $default and grep {$_ eq $default;} @LANGUAGES
defined $default grep {$_ eq $default;} @LANGUAGES
Truth table100%3/3
A and B
A= defined $defaultB= grep {$_ eq $default;} @LANGUAGES
ABresult
0X0
100
111
56  
57 2195 my $accept = $args{accept};
58 2195 ▶ return $default unless defined $accept && length $accept;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept and length $accept 2164 31 2195
Condition100%3/3
defined $accept and length $accept
!ll&&!rl&&r
2163131
MC/DC100%2/2
defined $accept and length $accept
defined $accept length $accept
Truth table100%3/3
A and B
A= defined $acceptB= length $accept
ABresult
0X0
100
111
59  
60 31 ▶ return lingua_choice($accept, [ @LANGUAGES ]) // $default;
Condition66%2/3
lingua_choice($accept, [@LANGUAGES]) // $default
l!l&&r!l&&!r
2740
MC/DC0%0/2
lingua_choice($accept, [@LANGUAGES]) // $default
lingua_choice($accept, [@LANGUAGES]) $default
Truth table66%2/3
A // B
A= lingua_choice($accept, [@LANGUAGES])B= $default
ABresult
1X1
011
000
61 }
62  
63 # lingua_choice($accept, \@supported) -> $code | undef
64 #
65 # What CGI::Lingua picks from @supported for an Accept-Language value: the
66 # language's code, or "<code>-<region>" when the value names a region that is
67 # in @supported. undef when it names nothing supported, or CGI::Lingua fails.
68 # The template directories are chosen by this too (Dashboard's
69 # _resolve_language), so pages and messages agree.
70 55 ▶sub lingua_choice {
Subroutine lingua_choice: called CC=3 SCAR=12.0
Pod documented
71 55 my ($accept, $supported) = @_;
72  
73 55 ▶ return undef unless defined $accept && length $accept; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept and length $accept 0 55 55
Condition33%1/3
defined $accept and length $accept
!ll&&!rl&&r
0055
MC/DC0%0/2
defined $accept and length $accept
defined $accept length $accept
Truth table33%1/3
A and B
A= defined $acceptB= length $accept
ABresult
0X0
100
111
74  
75 55 local $ENV{HTTP_ACCEPT_LANGUAGE} = $accept;
76 55 return _lingua($supported);
77 }
78  
79 # _lingua(\@supported) -> $code | undef
80 #
81 # CGI::Lingua's choice from @supported, from whatever it reads: the
82 # Accept-Language header when there is one, else (0.88 and later) the locale.
83 # "<code>-<region>" when the region is offered, else the language's code.
84 66 ▶sub _lingua {
Subroutine _lingua: called CC=4 SCAR=13.9
85 66 my ($supported) = @_;
86  
87 66 my %offered = map { lc($_) => 1 } @{$supported};
88 66 local ($@, $!);
89 66 my $code = eval {
90 66 require CGI::Lingua;
91 66 my $lingua = CGI::Lingua->new(supported => $supported, dont_use_ip => 1);
92 66 my $language = $lingua->language_code_alpha2();
93 66 ▶ my $region = defined $language ? $lingua->sublanguage_code_alpha2() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $language ? : 51 15 66
94 66 ▶ (defined $region && $offered{ lc "$language-$region" }) ? lc "$language-$region" : $language;
Branch: 1 branch
BranchTrueFalseTotal
defined $region && $offered{lc "$language-$region"} ? : 24 42 66
Condition66%2/3
defined $region and $offered{lc "$language-$region"}
!ll&&!rl&&r
42024
MC/DC50%1/2
defined $region and $offered{lc "$language-$region"}
defined $region $offered{lc "$language-$region"}
Truth table66%2/3
A and B
A= defined $regionB= $offered{lc "$language-$region"}
ABresult
0X0
100
111
95 };
96 66 return $code;
97 }
98  
99 # language() -> $code
100 #
101 # The language to give messages in: $LANGUAGE, else British.
102 1016 ▶sub language {
Subroutine language: called CC=2 SCAR=6.9
Pod documented
103 1016 ▶ return $LANGUAGE // $BASE;
Condition100%3/3
$Database::BI::I18N::LANGUAGE // $BASE
l!l&&r!l&&!r
5774345
MC/DC100%2/2
$Database::BI::I18N::LANGUAGE // $BASE
$Database::BI::I18N::LANGUAGE $BASE
Truth table100%3/3
A // B
A= $Database::BI::I18N::LANGUAGEB= $BASE
ABresult
1X1
011
000
104 }
105  
106 # from_locale() -> $code
107 #
108 # The language the locale names, for a script to set $LANGUAGE from.
109 # CGI::Lingua (0.88 and later) reads the locale itself, as POSIX does: LC_ALL,
110 # then LC_MESSAGES, then LANG, so en_US.UTF-8 is en-us and LC_ALL=C names no
111 # language whatever LANG says. No request header is consulted: one left in
112 # the environment by a CGI caller would otherwise win. Remembered per locale.
113 12 ▶sub from_locale {
Subroutine from_locale: called CC=4 SCAR=14.0
Pod documented
114 12 ▶ my $locale = join "\0", map { $_ // '' } @ENV{qw(LC_ALL LC_MESSAGES LANG)};
Condition100%2/2
$_ // ""
l!l
1620
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
115 12 ▶ return $FROM_LOCALE{$locale} //= do {
Condition66%2/3
$FROM_LOCALE{$locale} //= do { delete local $ENV{"HTTP_ACCEPT_LANGUAGE"}; _lingua([@LANGUAGES]) // $BASE }
l!l&&r!l&&!r
1110
MC/DC0%0/2
$FROM_LOCALE{$locale} //= do { delete local $ENV{"HTTP_ACCEPT_LANGUAGE"}; _lingua([@LANGUAGES]) // $BASE }
$FROM_LOCALE{$locale} do { delete local $ENV{"HTTP_ACCEPT_LANGUAGE"}; _lingua([@LANGUAGES]) // $BASE }
Truth table66%2/3
A //= B
A= $FROM_LOCALE{$locale}B= do { delete local $ENV{"HTTP_ACCEPT_LANGUAGE"}; _lingua([@LANGUAGES]) // $BASE }
ABresult
1X1
011
000
116 11 delete local $ENV{HTTP_ACCEPT_LANGUAGE};
117 11 ▶ _lingua([ @LANGUAGES ]) // $BASE;
Condition66%2/3
_lingua([@LANGUAGES]) // $BASE
l!l&&r!l&&!r
650
MC/DC0%0/2
_lingua([@LANGUAGES]) // $BASE
_lingua([@LANGUAGES]) $BASE
Truth table66%2/3
A // B
A= _lingua([@LANGUAGES])B= $BASE
ABresult
1X1
011
000
118 };
119 }
120  
121 # text($package, \%messages, $key) -> $template | undef
122 #
123 # The template for $key in the current language: the language's own text for
124 # that package, else the British one in \%messages (undef when that has none).
125 1016 ▶sub text {
Subroutine text: called CC=6 SCAR=18.0
Pod documented
126 1016 my ($package, $messages, $key) = @_;
127  
128 1016 ▶ return undef unless defined $key; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 2 1014 1016
129  
130 1014 my $language = language();
131 1014 ▶ if ($language ne $BASE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language ne $BASE) 7 1007 1014
132 7 ▶ my $table = $TABLE{$language} //= _load($language);
Condition66%2/3
$TABLE{$language} //= _load($language)
l!l&&r!l&&!r
520
MC/DC0%0/2
$TABLE{$language} //= _load($language)
$TABLE{$language} _load($language)
Truth table66%2/3
A //= B
A= $TABLE{$language}B= _load($language)
ABresult
1X1
011
000
133 7 my $own = $table->{$package};
134 7 ▶ return $own->{$key} if $own && defined $own->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $own and defined $own->{$key} 4 3 7
Condition66%2/3
$own and defined $own->{$key}
!ll&&!rl&&r
304
MC/DC50%1/2
$own and defined $own->{$key}
$own defined $own->{$key}
Truth table66%2/3
A and B
A= $ownB= defined $own->{$key}
ABresult
0X0
100
111
135 }
136 1010 return $messages->{$key};
137 }
138  
139 # _load($language) -> \%table
140 #
141 # Database::BI::I18N::en_us for 'en-us'. A language with no module is
142 # {}: its messages are the British ones.
143 2 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.0
144 2 my ($language) = @_;
145  
146 2 (my $name = $language) =~ tr/-/_/;
147 2 ▶ croak "Unsafe language name '$language'" unless $name =~ /\A[a-z]{2,3}(?:_[a-z0-9]{2,8})?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[a-z]{2,3}(?:_[a-z0-9]{2,8})?\z/ 0 2 2
148 2 my $module = "Database::BI::I18N::$name";
149  
150 2 local ($@, $!);
151 2 my $table = eval "require $module; \\%${module}::MESSAGES"; ## no critic (ProhibitStringyEval): the name is checked above
152 2 ▶ return $table || {};
Condition100%2/2
$table || {}
l!l
11
MC/DC100%1/1
$table || {}
$table
Truth table100%2/2
A
A= $table
Aresult
11
00
153 }
154  
155 11 1;
156  
157 __END__
« blib/lib/Database/BI/Ask.pm ↑ Summary blib/lib/Database/BI/Extract.pm »