blib/lib/Getopt/EX/i18n.pm

filter: Statementstmt 27.7% 30 / 108 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 10 totaltotal 17.6% 37 / 210 SCAR 66.5
CC
36
Cov
17%
finalize
306.0
options
56.0
setup
56.0
CRAP 770.6
↑ Summary Build »
1 package Getopt::EX::i18n;
2 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = "0.08";
8  
9 =encoding utf-8
10  
11 =head1 NAME
12  
13 Getopt::EX::i18n - General i18n module
14  
15 =head1 SYNOPSIS
16  
17 command -Mi18n [ options ]
18  
19 =head1 DESCRIPTION
20  
21 This module B<i18n> provide an easy way to set locale environment
22 before executing arbitrary command. Locale list is taken from the
23 system by C<locale -a> command. Next list is a sample locales
24 available on macOS 10.15 (Catalina).
25  
26 af_ZA Afrikaans / South Africa
27 am_ET Amharic / Ethiopia
28 be_BY Belarusian / Belarus
29 bg_BG Bulgarian / Bulgaria
30 ca_ES Catalan; Valencian / Spain
31 cs_CZ Czech / Czech Republic
32 da_DK Danish / Denmark
33 de_AT German / Austria
34 de_CH German / Switzerland
35 de_DE German / Germany
36 el_GR Greek, Modern (1453-) / Greece
37 en_AU English / Australia
38 en_CA English / Canada
39 en_GB English / United Kingdom
40 en_IE English / Ireland
41 en_NZ English / New Zealand
42 en_US English / United States
43 es_ES Spanish / Spain
44 et_EE Estonian / Estonia
45 eu_ES Basque / Spain
46 fi_FI Finnish / Finland
47 fr_BE French / Belgium
48 fr_CA French / Canada
49 fr_CH French / Switzerland
50 fr_FR French / France
51 he_IL Hebrew / Israel
52 hr_HR Croatian / Croatia
53 hu_HU Hungarian / Hungary
54 hy_AM Armenian / Armenia
55 is_IS Icelandic / Iceland
56 it_CH Italian / Switzerland
57 it_IT Italian / Italy
58 ja_JP Japanese / Japan
59 kk_KZ Kazakh / Kazakhstan
60 ko_KR Korean / Korea, Republic of
61 lt_LT Lithuanian / Lithuania
62 nl_BE Dutch / Belgium
63 nl_NL Dutch / Netherlands
64 no_NO Norwegian / Norway
65 pl_PL Polish / Poland
66 pt_BR Portuguese / Brazil
67 pt_PT Portuguese / Portugal
68 ro_RO Romanian / Romania
69 ru_RU Russian / Russian Federation
70 sk_SK Slovak / Slovakia
71 sl_SI Slovenian / Slovenia
72 sr_YU Serbian / Yugoslavia
73 sv_SE Swedish / Sweden
74 tr_TR Turkish / Turkey
75 uk_UA Ukrainian / Ukraine
76 zh_CN Chinese / China
77 zh_HK Chinese / Hong Kong
78 zh_TW Chinese / Taiwan, Province of China
79  
80 As for Japanese locale C<ja_JP>, following options are defined by
81 default, and set C<LANG> environment as C<ja_JP>.
82  
83 LOCALE: --ja_JP (raw)
84 --ja-JP (dash)
85 --jaJP (long)
86 --jajp (long_lc)
87 LANGUAGE: --ja (language)
88 TERRITORY: --JP (territory)
89 --jp (territory_lc)
90  
91 Short language option (C<--ja>) is defined in the alphabetical order
92 of the territory code, so the option C<--en> is assigned to C<en_AU>.
93 But if the same territory name is found as language, it takes
94 precedence; German is used in three locales (C<de_AT>, C<de_CH>,
95 C<de_DE>) but option C<--de> is defined as C<de_DE>.
96  
97 Territory options (C<--JP> and C<--jp>) are defined only when the same
98 language option is not defined by other entry, and only single entry
99 can be found for the territory. Option for Switzerland is not defined
100 because there are three entries (C<de_CH>, C<fr_CH>, C<it_CH>).
101 Territory option C<--AM> is assigned to C<hy_AM>, but language option
102 C<--am> is assigned to C<am_ET>.
103  
104 =head1 OPTION
105  
106 Option parameter can be given with B<setopt> function called with
107 module declaration.
108  
109 command -Mi18n::setopt(name[=value])
110  
111 =over 7
112  
113 =item B<raw>
114  
115 =item B<dash>
116  
117 =item B<long>
118  
119 =item B<long_lc>
120  
121 =item B<lang>
122  
123 =item B<territory>
124  
125 =item B<territory_lc>
126  
127 These parameter tells which option is defined. All options are
128 enabled by default. You can disable territory option like this:
129  
130 command -Mi18n::setopt(territory=0,territory_lc=0)
131  
132 command -Mi18n::setopt=territory=0,territory_lc=0
133  
134 =item B<verbose>
135  
136 Show locale information.
137  
138 $ optex -Mi18n::setopt=verbose date --it
139 LANG=it_IT (Italian / Italy)
140 Gio 4 Giu 2020 16:47:33 JST
141  
142 =item B<list>
143  
144 Show option list.
145  
146 =item B<listopt>=I<option>
147  
148 Set the option to display option list and exit. You can introduce a
149 new option B<-l> to show available option list:
150  
151 -Mi18n::setopt(listopt=-l)
152  
153 =item B<prefix>=I<string>
154  
155 Specify prefix string. Default is C<-->.
156  
157 =back
158  
159 =head1 BUGS
160  
161 Support only UTF-8.
162  
163 =head1 SEE ALSO
164  
165 =over 7
166  
167 =item L<Getopt::EX>
168  
169 L<https://github.com/kaz-utashiro/Getopt-EX>
170  
171 =item L<optex|App::optex>
172  
173 You can execute arbitrary command on the system getting the benefit of
174 B<Getopt::EX> using B<optex>.
175  
176 $ optex -Mi18n cal 2020 --am
177  
178 L<https://github.com/kaz-utashiro/optex>
179  
180 L<https://qiita.com/kaz-utashiro/items/2df8c7fbd2fcb880cee6>
181  
182 =back
183  
184 =head1 LICENSE
185  
186 Copyright (C) 2020 Kazumasa Utashiro.
187  
188 This library is free software; you can redistribute it and/or modify
189 it under the same terms as Perl itself.
190  
191 =head1 AUTHOR
192  
193 Kazumasa Utashiro E<lt>kaz@utashiro.comE<gt>
194  
195 =cut
196  
197 1 my %opt = (
198 raw => 1,
199 dash => 1,
200 long => 1,
201 long_lc => 1,
202 language => 1,
203 territory => 1,
204 territory_lc => 1,
205 verbose => 0,
206 list => 0,
207 prefix => '--',
208 listopt => undef,
209 );
210  
211 1 my $module;
212  
213 sub initialize {
214 0 ▶ return if state $called++;
Subroutine initialize: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if state $called++ 0 0 0
215 0 my($obj, $argv) = @_;
216 0 $module = $obj;
217 0 setup();
218 }
219  
220 my @locale;
221 1 my %locale;
222 1 my %lang;
223 1 my %cc;
224 1 my %opthash;
225  
226 1 package LocaleObj {
227 1 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 1 has [ my @member = qw(name lang cc) ] => (is => 'ro', required => 1);
229 sub create {
230 0 ▶ (my $class, local $_) = @_;
Subroutine create: not called CC=3 SCAR=24.8
Pod undocumented
231 0 ▶ /^(?<name>(?<lang>[a-z][a-z])_(?<cc>[A-Z][A-Z]))/ or die;
Branch: 1 branch
BranchTrueFalseTotal
/^(?<name>(?<lang>[a-z][a-z])_(?<cc>[A-Z][A-Z]))/u or die 0 0 0
232 0 ▶ new $class map { $_ => $+{$_} // '' } @member;
Condition0%0/2
$+{$_} // ""
l!l
00
MC/DC0%0/1
$+{$_} // ""
$+{$_}
Truth table0%0/2
A
A= $+{$_}
Aresult
11
00
233 }
234 1 ▶ use Getopt::EX::i18n::iso639 qw(%iso639);
Subroutine BEGIN: called CC=1 SCAR=0.0
235 1 ▶ use Getopt::EX::i18n::iso3361 qw(%iso3361);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 0 ▶ sub lang_name { $iso639 {+shift->lang} || 'UNKNOWN' }
Subroutine lang_name: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$LocaleObj::iso639{(shift())->lang} or "UNKNOWN" 0 0 0
237 0 ▶ sub cc_name { $iso3361{+shift->cc} || 'UNKNOWN' }
Subroutine cc_name: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$LocaleObj::iso3361{(shift())->cc} or "UNKNOWN" 0 0 0
238 }
239  
240 sub finalize {
241 0 ▶ my($obj, $argv) = @_;
Subroutine finalize: not called CC=17 SCAR=57.2
Pod undocumented
242 0 for my $locale (sort @locale) {
243 0 ▶ $locale =~ /^(?<lang>\w\w)_(?<cc>\w\w)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$locale =~ /^(?<lang>\w\w)_(?<cc>\w\w)$/u or next 0 0 0
244 0 my($lang, $cc) = @+{qw(lang cc)};;
245 0 my @list;
246 0 ▶ push @list, "$locale" if $opt{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"raw"} 0 0 0
247 0 ▶ push @list, "$lang-$cc" if $opt{dash};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"dash"} 0 0 0
248 0 ▶ push @list, "$lang$cc" if $opt{long};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"long"} 0 0 0
249 0 $cc = lc $cc;
250 0 ▶ push @list, "$lang$cc" if $opt{long_lc};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"long_lc"} 0 0 0
251 0 ▶ if ($opt{language}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"language"}) 0 0 0
252 0 ▶ if (!$opthash{$lang} or $lang eq $cc) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opthash{$lang} or $lang eq $cc) 0 0 0
Condition0%0/3
not $opthash{$lang} or $lang eq $cc
l!l&&r!l&&!r
000
MC/DC0%0/2
not $opthash{$lang} or $lang eq $cc
not $opthash{$lang} $lang eq $cc
Truth table0%0/3
not(A) or B
A= not $opthash{$lang}B= $lang eq $cc
ABresult
1X1
011
000
253 0 push @list, $lang;
254 }
255 }
256 0 ▶ if ($lang eq $cc or @{$cc{$cc}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq $cc or @{$cc{$cc};} == 1) 0 0 0
Condition0%0/3
$lang eq $cc or @{$cc{$cc};} == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$lang eq $cc or @{$cc{$cc};} == 1
$lang eq $cc @{$cc{$cc};} == 1
Truth table0%0/3
A or B
A= $lang eq $ccB= @{$cc{$cc};} == 1
ABresult
1X1
011
000
257 0 ▶ push @list, uc $cc if $opt{territory};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"territory"} 0 0 0
258 0 ▶ push @list, $cc if $opt{territory_lc} and !$lang{$cc};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"territory_lc"} and not $lang{$cc} 0 0 0
Condition0%0/3
$opt{"territory_lc"} and not $lang{$cc}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"territory_lc"} and not $lang{$cc}
$opt{"territory_lc"} not $lang{$cc}
Truth table0%0/3
A and not(B)
A= $opt{"territory_lc"}B= not $lang{$cc}
ABresult
0X0
100
111
259 }
260 0 for (@list) {
261 0 $opthash{$_} = LocaleObj->create($locale);
262 }
263 }
264  
265 0 $obj->mode(function => 1);
266 0 ▶ if (my $listopt = $opt{listopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $listopt = $opt{"listopt"}) 0 0 0
267 0 $obj->setopt($listopt, "&options(show,exit)");
268 }
269 0 &options(set => 1, show => $opt{list});
270 0 return;
271 }
272  
273 sub options {
274 0 ▶ my %arg = (
Subroutine options: not called CC=7 SCAR=40.3
Pod undocumented
275 set => 0, # set option
276 show => 0, # print option
277 exit => 0, # exit at the end
278 @_);
279 0 my @keys = do {
280 0 map { $_->[0] }
281 0 ▶ sort { $a->[1] cmp $b->[1] ||
Branch: 1 branch
BranchTrueFalseTotal
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] or lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0] or $Getopt::EX::i18n::a->[0] cmp $Getopt::EX::i18n::b->[0] 0 0 0
Condition0%0/3
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] or lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] or lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0]
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0]
Truth table0%0/3
A or B
A= $Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1]B= lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0]
ABresult
1X1
011
000
282 lc $a->[0] cmp lc $b->[0] || $a->[0] cmp $b->[0] }
283 0 map { [ $_, $opthash{$_}->cc ] }
284 keys %opthash;
285 };
286 0 for my $opt (@keys) {
287 0 my $obj = $opthash{$opt};
288 0 my $option = $opt{prefix} . $opt;
289 0 my $name = $obj->name;
290 0 my $locale = $locale{$name};
291 0 my $call = "&setenv(LANG=$locale)";
292 0 ▶ $module->setopt($option, $call) if $arg{set};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"set"} 0 0 0
293 0 ▶ if ($arg{show}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"show"}) 0 0 0
294 printf "option %-*s %s # %s / %s\n",
295 0 (state $optwidth = length($opt{prefix}) + length($name)),
296 $option, $call,
297 $obj->cc_name, $obj->lang_name;
298 }
299 }
300 0 ▶ exit if $arg{exit};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"exit"} 0 0 0
301 0 return ();
302 }
303  
304 sub setup {
305 0 ▶ return if state $called++;
Subroutine setup: not called CC=7 SCAR=40.3
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if state $called++ 0 0 0
306 0 ▶ grep { -x "$_/locale" } split /:/, $ENV{PATH} or return;
Branch: 1 branch
BranchTrueFalseTotal
grep {-x "$_/locale";} split(/:/u, $ENV{"PATH"}, 0) or return 0 0 0
307 0 for (`locale -a`) {
308 0 chomp;
309 0 ▶ /^(([a-z][a-z])_([A-Z][A-Z]))(?=(?i:$|\.utf))/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^(([a-z][a-z])_([A-Z][A-Z]))(?=(?i:$|\.utf))/u or next 0 0 0
310 0 my($name, $lang, $cc) = ($1, $2, lc $3);
311 0 ▶ if (my $last = $locale{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $locale{$name}) 0 0 0
312 0 ▶ $locale{$name} = $_ if length($_) < length($last);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < length $last 0 0 0
313 0 next;
314 }
315 0 $locale{$name} = $_;
316 0 push @locale, $name;
317 0 push @{ $lang{$lang} }, $name;
318 0 push @{ $cc{$cc} }, $name;
319 }
320 }
321  
322 sub locales {
323 0 ▶ chomp( my @locale = `locale -a` );
Subroutine locales: not called CC=1 SCAR=6.9
Pod undocumented
324 0 grep { /^\w\w_\w\w$/ } @locale;
325 }
326  
327 sub setopt {
328 0 ▶ %opt = (%opt, @_);
Subroutine setopt: not called CC=1 SCAR=6.9
Pod undocumented
329 }
330  
331 sub setenv {
332 0 ▶ while (@_ >= 2) {
Subroutine setenv: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 0 0 0
333 0 my($key, $value) = splice @_, 0, 2;
334 0 ▶ if ($opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"verbose"}) 0 0 0
335 0 my $l = LocaleObj->create($value);
336 0 warn sprintf("%s=%s (%s / %s)\n",
337 $key, $value, $l->lang_name, $l->cc_name);
338 }
339 0 $ENV{$key} = $value;
340 }
341 0 return ();
342 }
343  
344 1 1;
345  
346 __DATA__
↑ Summary Build »