File Coverage

File:blib/lib/Database/BI/I18N.pm
Coverage:96.2%

linestmtbrancondsubtimecode
1package Database::BI::I18N;
2
3
207
207
207
1298
937
2588
use strict;
4
207
207
207
196
119
3869
use warnings;
5
6our $VERSION = '0.025.0';
7
8
207
207
207
206
116
2332
use Carp qw(croak);
9
207
207
207
14814
136343
46107
use Readonly;
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.
30Readonly my $BASE      => 'en';
31Readonly my @LANGUAGES => ('en', 'en-us');
32
33# The language of the request being answered, or the script's; undef
34# (British) otherwise.
35our $LANGUAGE;
36
37my %TABLE;              # language => { package => { key => text } }, loaded on first use
38my %FROM_LOCALE;        # locale string => language, so a script asks CGI::Lingua once
39
40# languages() -> @codes
41
3
16654
sub languages { return @LANGUAGES }
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.
51sub choose {
52
5166
87074
        my (%args) = @_;
53
54
5166
4841
        my $default = $args{default};
55
5166
6452
19707
16803
        $default = $BASE unless defined $default && grep { $_ eq $default } @LANGUAGES;
56
57
5166
15954
        my $accept = $args{accept};
58
5166
12442
        return $default unless defined $accept && length $accept;
59
60
122
146
        return lingua_choice($accept, [ @LANGUAGES ]) // $default;
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.
70sub lingua_choice {
71
164
875
        my ($accept, $supported) = @_;
72
73
164
405
        return undef unless defined $accept && length $accept;  ## no critic (ProhibitExplicitReturnUndef)
74
75
162
1198
        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
162
497
        local $ENV{LC_ALL} = 'C';
81
162
182
        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.
89sub _lingua {
90
283
1084
        my ($supported) = @_;
91
92
283
568
283
237
686
258
        my %offered = map { lc($_) => 1 } @{$supported};
93
283
561
        local ($@, $!);
94
283
200
        my $code = eval {
95
283
31110
                require CGI::Lingua;
96
283
5328670
                my $lingua = CGI::Lingua->new(supported => $supported, dont_use_ip => 1);
97
283
1600113
                my $language = $lingua->language_code_alpha2();
98
283
2308252
                my $region   = defined $language ? $lingua->sublanguage_code_alpha2() : undef;
99
283
1198
                (defined $region && $offered{ lc "$language-$region" }) ? lc "$language-$region" : $language;
100        };
101
283
7737
        return $code;
102}
103
104# language() -> $code
105#
106# The language to give messages in: $LANGUAGE, else British.
107sub language {
108
3138
5331
        return $LANGUAGE // $BASE;
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.
118sub from_locale {
119
129
387
9005
1135
        my $locale = join "\0", map { $_ // '' } @ENV{qw(LC_ALL LC_MESSAGES LANG)};
120
129
1144
        return $FROM_LOCALE{$locale} //= do {
121
121
525
                delete local $ENV{HTTP_ACCEPT_LANGUAGE};
122
121
432
                _lingua([ @LANGUAGES ]) // $BASE;
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).
130sub text {
131
3135
66921
        my ($package, $messages, $key) = @_;
132
133
3135
3134
        return undef unless defined $key;       ## no critic (ProhibitExplicitReturnUndef)
134
135
3131
2896
        my $language = language();
136
3131
5385
        if ($language ne $BASE) {
137
20
64
                my $table = $TABLE{$language} //= _load($language);
138
12
18
                my $own   = $table->{$package};
139
12
42
                return $own->{$key} if $own && defined $own->{$key};
140        }
141
3117
17068
        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.
148sub _load {
149
31
5791
        my ($language) = @_;
150
151
31
38
        (my $name = $language) =~ tr/-/_/;
152
31
178
        croak "Unsafe language name '$language'" unless $name =~ /\A[a-z]{2,3}(?:_[a-z0-9]{2,8})?\z/;
153
10
9
        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
32
        local ($@, $!);
160
10
10
5
10
528
7
        my $loaded = eval { require "Database/BI/I18N/$name.pm"; 1 };
161
10
23
        return {} unless $loaded;
162
207
207
207
344
125
4995
        no strict 'refs';
163
5
5
6
19
        return \%{"${module}::MESSAGES"};
164}
165
1661;
167