blib/lib/Getopt/EX/i18n.pm

filter: Statementstmt 42.8% 45 / 105 Branchbran 18.0% 9 / 50 Conditioncond 14.2% 3 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 7 totaltotal 33.6% 72 / 214 CC 38 SCAR 46.4
CC
38
Cov
32%
finalize
306.0
options
56.0
setup
20.7
CRAP 103.5
↑ Summary
1 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Getopt::EX::i18n;
3  
4 2 our $VERSION = '1.02';
5  
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
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> provides 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. The following list shows 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 For Japanese locale C<ja_JP>, the following options are defined by
81 default, and set C<LANG> environment to C<ja_JP>. The environment
82 variable name can be changed by B<env> option.
83  
84 LOCALE: --ja_JP (raw)
85 --ja-JP (dash)
86 --jaJP (long)
87 --jajp (long_lc)
88 LANGUAGE: --ja (language)
89 TERRITORY: --JP (territory)
90 --jp (territory_lc)
91  
92 Short language option (C<--ja>) is defined in alphabetical order
93 of the territory code, so the option C<--en> is assigned to C<en_AU>.
94 However, if the same territory name is found as language, it takes
95 precedence; German is used in three locales (C<de_AT>, C<de_CH>,
96 C<de_DE>) but option C<--de> is defined as C<de_DE>.
97  
98 Territory options (C<--JP> and C<--jp>) are defined only when the same
99 language option is not defined by other entry, and only a single entry
100 can be found for the territory. Options for Switzerland are not defined
101 because there are three entries (C<de_CH>, C<fr_CH>, C<it_CH>).
102 Territory option C<--AM> is assigned to C<hy_AM>, but language option
103 C<--am> is assigned to C<am_ET>.
104  
105 =head1 OPTION
106  
107 Option parameter can be given with B<setopt> function called with
108 module declaration.
109  
110 command -Mi18n::setopt(name[=value])
111  
112 =over 7
113  
114 =item B<raw>
115  
116 =item B<dash>
117  
118 =item B<long>
119  
120 =item B<long_lc>
121  
122 =item B<language>
123  
124 =item B<territory>
125  
126 =item B<territory_lc>
127  
128 These parameters tell which options are defined. All options are
129 enabled by default. You can disable territory options like this:
130  
131 command -Mi18n::setopt(territory=0,territory_lc=0)
132  
133 command -Mi18n::setopt=territory=0,territory_lc=0
134  
135 =item B<verbose>
136  
137 Show locale information.
138  
139 $ optex -Mi18n::setopt=verbose date --it
140 LANG=it_IT (Italian / Italy)
141 Gio 4 Giu 2020 16:47:33 JST
142  
143 =item B<list>
144  
145 Show option list.
146  
147 =item B<listopt>=I<option>
148  
149 Set the option to display the option list and exit. You can introduce a
150 new option B<-l> to show the available option list:
151  
152 -Mi18n::setopt(listopt=-l)
153  
154 =item B<prefix>=I<string>
155  
156 Specify prefix string. Default is C<-->.
157  
158 =item B<env>=I<string>
159  
160 Specify environment variable name to be set. Default is C<LANG>.
161  
162 =back
163  
164 =head1 DEPENDENCIES
165  
166 This module uses L<Locale::Codes::Language> and L<Locale::Codes::Country>
167 to provide language and country names for locale codes.
168  
169 =head1 BUGS
170  
171 Support only UTF-8.
172  
173 =head1 SEE ALSO
174  
175 =over 7
176  
177 =item L<Getopt::EX>
178  
179 L<https://github.com/kaz-utashiro/Getopt-EX>
180  
181 =item L<optex|App::optex>
182  
183 You can execute arbitrary command on the system getting the benefit of
184 B<Getopt::EX> using B<optex>.
185  
186 $ optex -Mi18n cal 2020 --am
187  
188 L<https://github.com/kaz-utashiro/optex>
189  
190 L<https://qiita.com/kaz-utashiro/items/2df8c7fbd2fcb880cee6>
191  
192 =back
193  
194 =head1 LICENSE
195  
196 Copyright (C) 2020-2025 Kazumasa Utashiro.
197  
198 This library is free software; you can redistribute it and/or modify
199 it under the same terms as Perl itself.
200  
201 =head1 AUTHOR
202  
203 Kazumasa Utashiro
204  
205 =cut
206  
207 2 my %opt = (
208 env => 'LANG',
209 raw => 1,
210 dash => 1,
211 long => 1,
212 long_lc => 1,
213 language => 1,
214 territory => 1,
215 territory_lc => 1,
216 verbose => 0,
217 list => 0,
218 prefix => '--',
219 listopt => undef,
220 );
221  
222 2 my $module;
223  
224 0 ▶sub initialize {
Subroutine initialize: not called CC=2 SCAR=17.9
Pod undocumented
225 0 ▶ return if state $called++;
Branch: 1 branch
BranchTrueFalseTotal
if state $called++ 0 0 0
226 0 my($obj, $argv) = @_;
227 0 $module = $obj;
228 0 setup();
229 }
230  
231 my @locale;
232 2 my %locale;
233 2 my %lang;
234 2 my %cc;
235 2 my %opthash;
236  
237 package #
238 2 Local::LocaleObj {
239 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
240 1 my($class, %hash) = @_;
241 1 bless \%hash, $class;
242 }
243 1 ▶ sub name { $_[0]->{name} // '' }
Subroutine name: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"name"} // ""
l!l
10
MC/DC0%0/1
$_[0]{"name"} // ""
$_[0]{"name"}
Truth table50%1/2
A
A= $_[0]{"name"}
Aresult
11
00
244 3 ▶ sub lang { $_[0]->{lang} // '' }
Subroutine lang: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"lang"} // ""
l!l
30
MC/DC0%0/1
$_[0]{"lang"} // ""
$_[0]{"lang"}
Truth table50%1/2
A
A= $_[0]{"lang"}
Aresult
11
00
245 3 ▶ sub cc { $_[0]->{cc} // '' }
Subroutine cc: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"cc"} // ""
l!l
30
MC/DC0%0/1
$_[0]{"cc"} // ""
$_[0]{"cc"}
Truth table50%1/2
A
A= $_[0]{"cc"}
Aresult
11
00
246 1 ▶ sub create {
Subroutine create: called CC=2 SCAR=7.1
247 1 (my $class, local $_) = @_;
248 1 ▶ /^(?<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 1 0 1
249 1 $class->new(%+);
250 }
251  
252 2 ▶ use Locale::Codes::Language;
Subroutine BEGIN: called CC=1 SCAR=0.0
253 2 ▶ sub lang_name { code2language(+shift->lang) || 'UNKNOWN' }
Subroutine lang_name: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
code2language((shift())->lang) or "UNKNOWN" 2 0 2
254  
255 2 ▶ use Locale::Codes::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 2 ▶ sub cc_name { code2country(+shift->cc) || 'UNKNOWN' }
Subroutine cc_name: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
code2country((shift())->cc) or "UNKNOWN" 2 0 2
257 }
258  
259 0 ▶sub finalize {
Subroutine finalize: not called CC=17 SCAR=57.2
Pod undocumented
260 0 my($obj, $argv) = @_;
261 0 for my $locale (sort @locale) {
262 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
263 0 my($lang, $cc) = @+{qw(lang cc)};;
264 0 my @list;
265 0 ▶ push @list, "$locale" if $opt{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"raw"} 0 0 0
266 0 ▶ push @list, "$lang-$cc" if $opt{dash};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"dash"} 0 0 0
267 0 ▶ push @list, "$lang$cc" if $opt{long};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"long"} 0 0 0
268 0 $cc = lc $cc;
269 0 ▶ push @list, "$lang$cc" if $opt{long_lc};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"long_lc"} 0 0 0
270 0 ▶ if ($opt{language}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"language"}) 0 0 0
271 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
272 0 push @list, $lang;
273 }
274 }
275 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
276 0 ▶ push @list, uc $cc if $opt{territory};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"territory"} 0 0 0
277 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
278 }
279 0 for (@list) {
280 0 $opthash{$_} = Local::LocaleObj->create($locale);
281 }
282 }
283  
284 0 $obj->mode(function => 1);
285 0 ▶ if (my $listopt = $opt{listopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $listopt = $opt{"listopt"}) 0 0 0
286 0 $obj->setopt($listopt, "&options(show,exit)");
287 }
288 0 &options(set => 1, show => $opt{list});
289 0 return;
290 }
291  
292 0 ▶sub options {
Subroutine options: not called CC=7 SCAR=40.3
Pod undocumented
293 0 my %arg = (
294 set => 0, # set option
295 show => 0, # print option
296 exit => 0, # exit at the end
297 @_);
298 0 my @keys = do {
299 0 map { $_->[0] }
300 0 ▶ sort { $a->[1] cmp $b->[1] ||
Condition0%0/6
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] || lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0] || $Getopt::EX::i18n::a->[0] cmp $Getopt::EX::i18n::b->[0]
l!l&&r!l&&!r
000
$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/3
$Getopt::EX::i18n::a->[1] cmp $Getopt::EX::i18n::b->[1] || lc $Getopt::EX::i18n::a->[0] cmp lc $Getopt::EX::i18n::b->[0] || $Getopt::EX::i18n::a->[0] cmp $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] $Getopt::EX::i18n::a->[0] cmp $Getopt::EX::i18n::b->[0]
Truth table0%0/4
A or B || C
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]C= $Getopt::EX::i18n::a->[0] cmp $Getopt::EX::i18n::b->[0]
ABCresult
1XX1
01X1
0011
0000
301 lc $a->[0] cmp lc $b->[0] || $a->[0] cmp $b->[0] }
302 0 map { [ $_, $opthash{$_}->cc ] }
303 keys %opthash;
304 };
305 0 for my $opt (@keys) {
306 0 my $obj = $opthash{$opt};
307 0 my $option = $opt{prefix} . $opt;
308 0 my $name = $obj->name;
309 0 my $locale = $locale{$name};
310 0 my $call = "&setenv($opt{env}=$locale)";
311 0 ▶ $module->setopt($option, $call) if $arg{set};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"set"} 0 0 0
312 0 ▶ if ($arg{show}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"show"}) 0 0 0
313 printf "option %-*s %s # %s / %s\n",
314 0 (state $optwidth = length($opt{prefix}) + length($name)),
315 $option, $call,
316 $obj->cc_name, $obj->lang_name;
317 }
318 }
319 0 ▶ exit if $arg{exit};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"exit"} 0 0 0
320 0 return ();
321 }
322  
323 1 ▶sub setup {
Subroutine setup: called CC=7 SCAR=30.3
Pod undocumented
324 1 ▶ return if state $called++;
Branch: 1 branch
BranchTrueFalseTotal
if state $called++ 0 1 1
325 1 ▶ grep { -x "$_/locale" } split /:/, $ENV{PATH} or return;
Branch: 1 branch
BranchTrueFalseTotal
grep {-x "$_/locale";} split(/:/u, $ENV{"PATH"}, 0) or return 1 0 1
326 1 for (`locale -a`) {
327 3 chomp;
328 3 ▶ /^(([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 3 3
329 0 my($name, $lang, $cc) = ($1, $2, lc $3);
330 0 ▶ if (my $last = $locale{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $locale{$name}) 0 0 0
331 0 ▶ $locale{$name} = $_ if length($_) < length($last);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < length $last 0 0 0
332 0 next;
333 }
334 0 $locale{$name} = $_;
335 0 push @locale, $name;
336 0 push @{ $lang{$lang} }, $name;
337 0 push @{ $cc{$cc} }, $name;
338 }
339 }
340  
341 0 ▶sub locales {
Subroutine locales: not called CC=1 SCAR=6.9
Pod undocumented
342 0 chomp( my @locale = `locale -a` );
343 0 grep { /^\w\w_\w\w$/ } @locale;
344 }
345  
346 2 ▶sub setopt {
Subroutine setopt: called CC=1 SCAR=0.0
Pod undocumented
347 2 %opt = (%opt, @_);
348 }
349  
350 2 ▶sub setenv {
Subroutine setenv: called CC=3 SCAR=11.6
Pod undocumented
351 2 ▶ while (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 3 2 5
352 3 my($key, $value) = splice @_, 0, 2;
353 3 ▶ if ($opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"verbose"}) 0 3 3
354 0 my $l = Local::LocaleObj->create($value);
355 0 warn sprintf("%s=%s (%s / %s)\n",
356 $key, $value, $l->lang_name, $l->cc_name);
357 }
358 3 $ENV{$key} = $value;
359 }
360 2 return ();
361 }
362  
363 2 1;
364  
365 __DATA__
↑ Summary