| 1 | package Astro::App::Satpass2::Locale; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Astro::App::Satpass2::Utils qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | expand_tilde instance | ||||||||||||||||||||||||||||
| 10 | ARRAY_REF CODE_REF HASH_REF | ||||||||||||||||||||||||||||
| 11 | @CARP_NOT | ||||||||||||||||||||||||||||
| 12 | 22 | }; | |||||||||||||||||||||||||||
| 13 | 22 | ▶ | use Exporter qw{ import }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 22 | ▶ | use I18N::LangTags (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 22 | ▶ | use I18N::LangTags::Detect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 22 | our $VERSION = '0.059'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | our @EXPORT_OK = qw{ __localize __message __preferred }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 22 | my @lang; | |||||||||||||||||||||||||||
| 22 | my $locale; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my %deref = ( | ||||||||||||||||||||||||||||
| 27 | 51 | ▶ | ARRAY_REF() => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 28 | 51 | my ( $data, $inx ) = @_; | |||||||||||||||||||||||||||
| 29 | 51 | ▶ | defined $inx | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $inx and exists $data->[$inx]
MC/DC0%0/2
defined $inx and exists $data->[$inx]
defined $inx
exists $data->[$inx]
Truth table33%1/3
A and B
A= defined $inxB= exists $data->[$inx]
| |||||||||||||||||||||||||||||
| 30 | and exists $data->[$inx] | ||||||||||||||||||||||||||||
| 31 | and return $data->[$inx]; | ||||||||||||||||||||||||||||
| 32 | 0 | return; | |||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | CODE_REF() => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | my ( $code, $key, $arg ) = @_; | |||||||||||||||||||||||||||
| 36 | 1 | my $rslt = $code->( $key, $arg ); | |||||||||||||||||||||||||||
| 37 | 1 | return $rslt; | |||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | 33799 | ▶ | HASH_REF() => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 40 | 33799 | my ( $data, $key ) = @_; | |||||||||||||||||||||||||||
| 41 | defined $key | ||||||||||||||||||||||||||||
| 42 | and exists $data->{$key} | ||||||||||||||||||||||||||||
| 43 | 33799 | ▶ | and return $data->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and exists $data->{$key}
MC/DC50%1/2
defined $key and exists $data->{$key}
defined $key
exists $data->{$key}
Truth table66%2/3
A and B
A= defined $keyB= exists $data->{$key}
| |||||||||||||||||||||||||||||
| 44 | 14305 | return; | |||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | '' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 22 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 17881 | ▶ | sub __localize { | ||||||||||||||||||||||||||
|
Subroutine
__localize: called CC=15 SCAR=28.2
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Keys used: | ||||||||||||||||||||||||||||
| 54 | # {argument} = argument for code reference | ||||||||||||||||||||||||||||
| 55 | # {default} = the default value | ||||||||||||||||||||||||||||
| 56 | # {text} = the text to localize, as scalar or array ref. REQUIRED. | ||||||||||||||||||||||||||||
| 57 | # {locale} = fallback locales, as hash ref or ref to array of hash refs. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 17881 | my %arg = @_; | |||||||||||||||||||||||||||
| 60 | 17881 | ▶ | unless ( $arg{text} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | require Carp; | |||||||||||||||||||||||||||
| 62 | 0 | Carp::confess( q<Argument 'text' is required> ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | ref $arg{text} | ||||||||||||||||||||||||||||
| 65 | 17881 | ▶ | or $arg{text} = [ $arg{text} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 17881 | ▶ | $arg{locale} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"locale"} ||= []
MC/DC100%1/1
$arg{"locale"} ||= []
$arg{"locale"}
Truth table100%2/2
A
A= $arg{"locale"}
| |||||||||||||||||||||||||||||
| 67 | HASH_REF eq ref $arg{locale} | ||||||||||||||||||||||||||||
| 68 | 17881 | ▶ | and $arg{locale} = [ $arg{locale} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 17881 | ▶ | $locale ||= _load(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$locale ||= _load()
MC/DC0%0/2
$locale ||= _load()
$locale
_load()
Truth table66%2/3
A ||= B
A= $localeB= _load()
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 17881 | my @rslt; | |||||||||||||||||||||||||||
| 72 | 17881 | foreach my $lc ( @lang ) { | |||||||||||||||||||||||||||
| 73 | SOURCE_LOOP: | ||||||||||||||||||||||||||||
| 74 | 17893 | foreach my $source ( @{ $locale }, @{ $arg{locale} } ) { | |||||||||||||||||||||||||||
| 75 | 35784 | ▶ | unless ( HASH_REF eq ref $source ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | require Carp; | |||||||||||||||||||||||||||
| 77 | 0 | Carp::confess( "\$source is '$source'" ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 35784 | ▶ | my $data = $source->{$lc} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | or next; | ||||||||||||||||||||||||||||
| 81 | 17888 | foreach my $key ( @{ $arg{text} } ) { | |||||||||||||||||||||||||||
| 82 | my $code = $deref{ ref $data } | ||||||||||||||||||||||||||||
| 83 | 33851 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | require Carp; | |||||||||||||||||||||||||||
| 85 | 0 | Carp::confess( | |||||||||||||||||||||||||||
| 86 | 'Programming error - Locale systen can ', | ||||||||||||||||||||||||||||
| 87 | 'not handle ', ref $data, ' as a container' | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||
| 90 | ( $data ) = $code->( $data, $key, $arg{argument} ) | ||||||||||||||||||||||||||||
| 91 | 33851 | ▶ | or next SOURCE_LOOP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | wantarray | ||||||||||||||||||||||||||||
| 94 | 3583 | ▶ | or return $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 27 | push @rslt, $data; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | wantarray | ||||||||||||||||||||||||||||
| 99 | 14325 | ▶ | or return $arg{default}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 26 | return ( @rslt, $arg{default} ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =begin comment | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | my %stringify_ref = map { $_ => 1 } qw{ Template::Exception }; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub __message { | ||||||||||||||||||||||||||||
| 112 | # My OpenBSD 5.5 system seems not to stringify the arguments in | ||||||||||||||||||||||||||||
| 113 | # the normal course of events, though my Mac OS 10.9 system | ||||||||||||||||||||||||||||
| 114 | # does. The OpenBSD system gives instead a stringified hash | ||||||||||||||||||||||||||||
| 115 | # reference (i.e. "HASH{0x....}"). | ||||||||||||||||||||||||||||
| 116 | my @raw_arg = @_; | ||||||||||||||||||||||||||||
| 117 | my ( $msg, @arg ) = | ||||||||||||||||||||||||||||
| 118 | map { $stringify_ref{ ref $_ } ? '' . $_ : $_ } @raw_arg; | ||||||||||||||||||||||||||||
| 119 | my $lcl = __localize( | ||||||||||||||||||||||||||||
| 120 | text => [ '+message', $msg ], | ||||||||||||||||||||||||||||
| 121 | default => $msg, | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | CODE_REF eq ref $lcl | ||||||||||||||||||||||||||||
| 125 | and return $lcl->( $msg, @arg ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | $lcl =~ m/ \[ % /smx | ||||||||||||||||||||||||||||
| 128 | or return join ' ', $lcl, @arg; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | grep { instance( $_, 'Template::Exception' ) } @raw_arg | ||||||||||||||||||||||||||||
| 131 | and return join ' ', $lcl, @arg; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | my $tt = Template->new(); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $output; | ||||||||||||||||||||||||||||
| 136 | $tt->process( \$lcl, { | ||||||||||||||||||||||||||||
| 137 | arg => \@arg, | ||||||||||||||||||||||||||||
| 138 | }, \$output ); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | return $output; | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =end comment | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 22 | ▶ | sub __message { | ||||||||||||||||||||||||||
|
Subroutine
__message: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||
| 149 | 28 | my ( $msg, @arg ) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 28 | ▶ | instance( $msg, 'Template::Exception' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | and return _message_template_exception( $msg, @arg ); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 27 | my $lcl = __localize( | |||||||||||||||||||||||||||
| 155 | text => [ '+message', $msg ], | ||||||||||||||||||||||||||||
| 156 | default => $msg, | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 27 | ▶ | CODE_REF eq ref $lcl | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | and return $lcl->( $msg, @arg ); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 27 | ▶ | $lcl =~ m/ \[ % /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | or return join ' ', $lcl, @arg; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $tt = Template->new(); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $output; | |||||||||||||||||||||||||||
| 168 | 0 | $tt->process( \$lcl, { | |||||||||||||||||||||||||||
| 169 | arg => \@arg, | ||||||||||||||||||||||||||||
| 170 | }, \$output ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | return $output; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | ▶ | sub _message_template_exception { | ||||||||||||||||||||||||||
|
Subroutine
_message_template_exception: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 176 | 1 | my ( $ex, @arg ) = @_; | |||||||||||||||||||||||||||
| 177 | 1 | local $_ = $ex->info(); | |||||||||||||||||||||||||||
| 178 | # NOTE we're assuming that if there is a return we caught a | ||||||||||||||||||||||||||||
| 179 | # re-thrown exception that had already been munged. | ||||||||||||||||||||||||||||
| 180 | 1 | ▶ | m/ \n \z /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | and return $ex; | ||||||||||||||||||||||||||||
| 182 | 1 | $_ = join ' ', $_, @arg; | |||||||||||||||||||||||||||
| 183 | 1 | ▶ | m/ [.?|] \z /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | or $_ .= '.'; | ||||||||||||||||||||||||||||
| 185 | 1 | $_ .= "\n"; | |||||||||||||||||||||||||||
| 186 | 1 | my $class = ref $ex; | |||||||||||||||||||||||||||
| 187 | 1 | return $class->new( $ex->type(), $_ ); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 2 | ▶ | sub __preferred { | ||||||||||||||||||||||||||
|
Subroutine
__preferred: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 191 | 2 | ▶ | $locale ||= _load(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$locale ||= _load()
MC/DC0%0/2
$locale ||= _load()
$locale
_load()
Truth table33%1/3
A ||= B
A= $localeB= _load()
| |||||||||||||||||||||||||||||
| 192 | 2 | ▶ | return wantarray ? @lang : $lang[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 8 | ▶ | sub _load { | ||||||||||||||||||||||||||
|
Subroutine
_load: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # Pick up the languages from the environment | ||||||||||||||||||||||||||||
| 198 | 8 | @lang = I18N::LangTags::implicate_supers( | |||||||||||||||||||||||||||
| 199 | I18N::LangTags::Detect::detect() ); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Normalize the language names. | ||||||||||||||||||||||||||||
| 202 | 8 | foreach ( @lang ) { | |||||||||||||||||||||||||||
| 203 | 2 | ▶ | s/ ( [^_-]+ ) [_-] (.* ) /\L$1_\U$2/smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | or $_ = lc $_; | ||||||||||||||||||||||||||||
| 205 | 2 | ▶ | 'c' eq $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | and $_ = uc $_; | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # Append the default locale name. | ||||||||||||||||||||||||||||
| 210 | 8 | ▶ | grep { 'C' eq $_ } @lang | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | or push @lang, 'C'; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Accumulator for locale data. | ||||||||||||||||||||||||||||
| 214 | 8 | my @locales; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Put all the user's data in a hash. | ||||||||||||||||||||||||||||
| 217 | 8 | push @locales, {}; | |||||||||||||||||||||||||||
| 218 | 8 | foreach my $lc ( @lang ) { | |||||||||||||||||||||||||||
| 219 | 10 | eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval) | |||||||||||||||||||||||||||
| 220 | 10 | ▶ | defined( my $path = expand_tilde( "~~/locale/$lc.pm" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | or return; | ||||||||||||||||||||||||||||
| 222 | 3 | my $data; | |||||||||||||||||||||||||||
| 223 | $data = do $path | ||||||||||||||||||||||||||||
| 224 | and HASH_REF eq ref $data | ||||||||||||||||||||||||||||
| 225 | 3 | ▶ | and $locales[-1]{$lc} = $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data = do $path and "HASH" eq ref $data
MC/DC50%1/2
$data = do $path and "HASH" eq ref $data
$data = do $path
"HASH" eq ref $data
Truth table66%2/3
A and B
A= $data = do $pathB= "HASH" eq ref $data
| |||||||||||||||||||||||||||||
| 226 | }; | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Put the system-wide data in a hash. | ||||||||||||||||||||||||||||
| 230 | 8 | push @locales, {}; | |||||||||||||||||||||||||||
| 231 | 8 | foreach my $lc ( @lang ) { | |||||||||||||||||||||||||||
| 232 | 10 | my $mod_name = __PACKAGE__ . "::$lc"; | |||||||||||||||||||||||||||
| 233 | 10 | my $data; | |||||||||||||||||||||||||||
| 234 | $data = eval "require $mod_name" | ||||||||||||||||||||||||||||
| 235 | and HASH_REF eq ref $data | ||||||||||||||||||||||||||||
| 236 | 10 | ▶ | and $locales[-1]{$lc} = $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data = eval "require $mod_name" and "HASH" eq ref $data
MC/DC50%1/2
$data = eval "require $mod_name" and "HASH" eq ref $data
$data = eval "require $mod_name"
"HASH" eq ref $data
Truth table66%2/3
A and B
A= $data = eval "require $mod_name"B= "HASH" eq ref $data
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Return a reference to the names of locales. | ||||||||||||||||||||||||||||
| 240 | 8 | return \@locales; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 22 | 1; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | __END__ | ||||||||||||||||||||||||||||