blib/lib/Database/BI/I18N.pm

filter: Statementstmt 100% 71 / 71 Branchbran 100% 20 / 20 Conditioncond 87.5% 28 / 32 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 93.2% 152 / 163 CC 22 SCAR 13.6
CC
22
Cov
97%
choose
6.0
text
6.0
from_locale
4.1
CRAP 3.9
« blib/lib/Database/BI/Extract.pm ↑ Summary blib/lib/Database/BI/Tutorial.pm »
1 package Database::BI::I18N;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 our $VERSION = '0.024.0';
7  
8 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶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 16 Readonly my $BASE => 'en';
31 16 Readonly my @LANGUAGES => ('en', 'en-us');
32  
33 # The language of the request being answered, or the script's; undef
34 # (British) otherwise.
35 16 our $LANGUAGE;
36  
37 16 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 3 ▶sub languages { return @LANGUAGES }
Subroutine languages: called CC=1 SCAR=0.0
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 3415 ▶sub choose {
Subroutine choose: called CC=6 SCAR=17.9
Pod documented
52 3415 my (%args) = @_;
53  
54 3415 my $default = $args{default};
55 3415 ▶ $default = $BASE unless defined $default && grep { $_ eq $default } @LANGUAGES;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default and grep {$_ eq $default;} @LANGUAGES 475 2940 3415
Condition100%3/3
defined $default and grep {$_ eq $default;} @LANGUAGES
!ll&&!rl&&r
429462940
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 3415 my $accept = $args{accept};
58 3415 ▶ return $default unless defined $accept && length $accept;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept and length $accept 3297 118 3415
Condition100%3/3
defined $accept and length $accept
!ll&&!rl&&r
32889118
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 118 ▶ return lingua_choice($accept, [ @LANGUAGES ]) // $default;
Condition66%2/3
lingua_choice($accept, [@LANGUAGES]) // $default
l!l&&r!l&&!r
64540
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 156 ▶sub lingua_choice {
Subroutine lingua_choice: called CC=3 SCAR=11.0
Pod documented
71 156 my ($accept, $supported) = @_;
72  
73 156 ▶ return undef unless defined $accept && length $accept; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept and length $accept 2 154 156
Condition100%3/3
defined $accept and length $accept
!ll&&!rl&&r
11154
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
74  
75 154 local $ENV{HTTP_ACCEPT_LANGUAGE} = $accept;
76 # Premise 1: a request must never take the server's locale. Premise 2:
77 # CGI::Lingua falls back to the locale when the header names nothing it can
78 # read, and reads LC_ALL first, where "C" means no language. Conclusion:
79 # masking LC_ALL makes an unreadable header pick nothing, not the server's.
80 154 local $ENV{LC_ALL} = 'C';
81 154 return _lingua($supported);
82 }
83  
84 # _lingua(\@supported) -> $code | undef
85 #
86 # CGI::Lingua's choice from @supported, from whatever it reads: the
87 # Accept-Language header when there is one, else (0.88 and later) the locale.
88 # "<code>-<region>" when the region is offered, else the language's code.
89 180 ▶sub _lingua {
Subroutine _lingua: called CC=4 SCAR=13.9
90 180 my ($supported) = @_;
91  
92 180 my %offered = map { lc($_) => 1 } @{$supported};
93 180 local ($@, $!);
94 180 my $code = eval {
95 180 require CGI::Lingua;
96 180 my $lingua = CGI::Lingua->new(supported => $supported, dont_use_ip => 1);
97 180 my $language = $lingua->language_code_alpha2();
98 180 ▶ my $region = defined $language ? $lingua->sublanguage_code_alpha2() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $language ? : 99 81 180
99 180 ▶ (defined $region && $offered{ lc "$language-$region" }) ? lc "$language-$region" : $language;
Branch: 1 branch
BranchTrueFalseTotal
defined $region && $offered{lc "$language-$region"} ? : 43 137 180
Condition66%2/3
defined $region and $offered{lc "$language-$region"}
!ll&&!rl&&r
137043
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
100 };
101 180 return $code;
102 }
103  
104 # language() -> $code
105 #
106 # The language to give messages in: $LANGUAGE, else British.
107 1839 ▶sub language {
Subroutine language: called CC=2 SCAR=6.9
Pod documented
108 1839 ▶ return $LANGUAGE // $BASE;
Condition100%3/3
$Database::BI::I18N::LANGUAGE // $BASE
l!l&&r!l&&!r
11846478
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
109 }
110  
111 # from_locale() -> $code
112 #
113 # The language the locale names, for a script to set $LANGUAGE from.
114 # CGI::Lingua (0.88 and later) reads the locale itself, as POSIX does: LC_ALL,
115 # then LC_MESSAGES, then LANG, so en_US.UTF-8 is en-us and LC_ALL=C names no
116 # language whatever LANG says. No request header is consulted: one left in
117 # the environment by a CGI caller would otherwise win. Remembered per locale.
118 35 ▶sub from_locale {
Subroutine from_locale: called CC=4 SCAR=14.0
Pod documented
119 35 ▶ my $locale = join "\0", map { $_ // '' } @ENV{qw(LC_ALL LC_MESSAGES LANG)};
Condition100%2/2
$_ // ""
l!l
3273
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
120 35 ▶ 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
9260
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
121 26 delete local $ENV{HTTP_ACCEPT_LANGUAGE};
122 26 ▶ _lingua([ @LANGUAGES ]) // $BASE;
Condition66%2/3
_lingua([@LANGUAGES]) // $BASE
l!l&&r!l&&!r
9170
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
123 };
124 }
125  
126 # text($package, \%messages, $key) -> $template | undef
127 #
128 # The template for $key in the current language: the language's own text for
129 # that package, else the British one in \%messages (undef when that has none).
130 1836 ▶sub text {
Subroutine text: called CC=6 SCAR=17.9
Pod documented
131 1836 my ($package, $messages, $key) = @_;
132  
133 1836 ▶ return undef unless defined $key; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 4 1832 1836
134  
135 1832 my $language = language();
136 1832 ▶ if ($language ne $BASE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language ne $BASE) 20 1812 1832
137 20 ▶ my $table = $TABLE{$language} //= _load($language);
Condition100%3/3
$TABLE{$language} //= _load($language)
l!l&&r!l&&!r
848
MC/DC100%2/2
$TABLE{$language} //= _load($language)
$TABLE{$language} _load($language)
Truth table100%3/3
A //= B
A= $TABLE{$language}B= _load($language)
ABresult
1X1
011
000
138 12 my $own = $table->{$package};
139 12 ▶ return $own->{$key} if $own && defined $own->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $own and defined $own->{$key} 6 6 12
Condition100%3/3
$own and defined $own->{$key}
!ll&&!rl&&r
516
MC/DC100%2/2
$own and defined $own->{$key}
$own defined $own->{$key}
Truth table100%3/3
A and B
A= $ownB= defined $own->{$key}
ABresult
0X0
100
111
140 }
141 1818 return $messages->{$key};
142 }
143  
144 # _load($language) -> \%table
145 #
146 # Database::BI::I18N::en_us for 'en-us'. A language with no module is
147 # {}: its messages are the British ones.
148 31 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.0
149 31 my ($language) = @_;
150  
151 31 (my $name = $language) =~ tr/-/_/;
152 31 ▶ 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/ 21 10 31
153 10 my $module = "Database::BI::I18N::$name";
154  
155 # Premise 1: $name matched the pattern above, so it is letters, digits and
156 # underscores only.
157 # Premise 2: a block eval of require on a file name runs no text as code.
158 # Conclusion: the module loads without a string eval.
159 10 local ($@, $!);
160 10 my $loaded = eval { require "Database/BI/I18N/$name.pm"; 1 };
161 10 ▶ return {} unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 5 5 10
162 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 5 return \%{"${module}::MESSAGES"};
164 }
165  
166 16 1;
167  
168 __END__
« blib/lib/Database/BI/Extract.pm ↑ Summary blib/lib/Database/BI/Tutorial.pm »