| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Astro::App::Satpass2::Locale; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 22 |  |  | 22 |  | 2212 | use 5.008; | 
|  | 22 |  |  |  |  | 84 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 22 |  |  | 22 |  | 131 | use strict; | 
|  | 22 |  |  |  |  | 46 |  | 
|  | 22 |  |  |  |  | 529 |  | 
| 6 | 22 |  |  | 22 |  | 122 | use warnings; | 
|  | 22 |  |  |  |  | 63 |  | 
|  | 22 |  |  |  |  | 896 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 22 |  |  |  |  | 3603 | use Astro::App::Satpass2::Utils qw{ | 
| 9 |  |  |  |  |  |  | expand_tilde instance | 
| 10 |  |  |  |  |  |  | ARRAY_REF CODE_REF HASH_REF | 
| 11 |  |  |  |  |  |  | @CARP_NOT | 
| 12 | 22 |  |  | 22 |  | 10105 | }; | 
|  | 22 |  |  |  |  | 69 |  | 
| 13 | 22 |  |  | 22 |  | 185 | use Exporter qw{ import }; | 
|  | 22 |  |  |  |  | 49 |  | 
|  | 22 |  |  |  |  | 988 |  | 
| 14 | 22 |  |  | 22 |  | 13087 | use I18N::LangTags (); | 
|  | 22 |  |  |  |  | 67911 |  | 
|  | 22 |  |  |  |  | 599 |  | 
| 15 | 22 |  |  | 22 |  | 11537 | use I18N::LangTags::Detect (); | 
|  | 22 |  |  |  |  | 43508 |  | 
|  | 22 |  |  |  |  | 26611 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $VERSION = '0.051'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | our @EXPORT_OK = qw{ __localize __message __preferred }; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | my @lang; | 
| 22 |  |  |  |  |  |  | my $locale; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | { | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | my %deref = ( | 
| 27 |  |  |  |  |  |  | ARRAY_REF()	=> sub { | 
| 28 |  |  |  |  |  |  | my ( $data, $inx ) = @_; | 
| 29 |  |  |  |  |  |  | defined $inx | 
| 30 |  |  |  |  |  |  | and exists $data->[$inx] | 
| 31 |  |  |  |  |  |  | and return $data->[$inx]; | 
| 32 |  |  |  |  |  |  | return; | 
| 33 |  |  |  |  |  |  | }, | 
| 34 |  |  |  |  |  |  | CODE_REF()	=> sub { | 
| 35 |  |  |  |  |  |  | my ( $code, $key, $arg ) = @_; | 
| 36 |  |  |  |  |  |  | my $rslt = $code->( $key, $arg ); | 
| 37 |  |  |  |  |  |  | return $rslt; | 
| 38 |  |  |  |  |  |  | }, | 
| 39 |  |  |  |  |  |  | HASH_REF()	=> sub { | 
| 40 |  |  |  |  |  |  | my ( $data, $key ) = @_; | 
| 41 |  |  |  |  |  |  | defined $key | 
| 42 |  |  |  |  |  |  | and exists $data->{$key} | 
| 43 |  |  |  |  |  |  | and return $data->{$key}; | 
| 44 |  |  |  |  |  |  | return; | 
| 45 |  |  |  |  |  |  | }, | 
| 46 |  |  |  |  |  |  | ''		=> sub { | 
| 47 |  |  |  |  |  |  | return; | 
| 48 |  |  |  |  |  |  | }, | 
| 49 |  |  |  |  |  |  | ); | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub __localize { | 
| 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 | 17878 |  |  | 17878 |  | 55324 | my %arg = @_; | 
| 60 | 17878 | 50 |  |  |  | 37925 | unless ( $arg{text} ) { | 
| 61 | 0 |  |  |  |  | 0 | require Carp; | 
| 62 | 0 |  |  |  |  | 0 | Carp::confess( q ); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | ref $arg{text} | 
| 65 | 17878 | 100 |  |  |  | 34572 | or $arg{text} = [ $arg{text} ]; | 
| 66 | 17878 |  | 100 |  |  | 67415 | $arg{locale} ||= []; | 
| 67 |  |  |  |  |  |  | HASH_REF eq ref $arg{locale} | 
| 68 | 17878 | 100 |  |  |  | 37210 | and $arg{locale} = [ $arg{locale} ]; | 
| 69 | 17878 |  | 66 |  |  | 38055 | $locale ||= _load(); | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 17878 |  |  |  |  | 26280 | my @rslt; | 
| 72 | 17878 |  |  |  |  | 30382 | foreach my $lc ( @lang ) { | 
| 73 |  |  |  |  |  |  | SOURCE_LOOP: | 
| 74 | 17890 |  |  |  |  | 24263 | foreach my $source ( @{ $locale }, @{ $arg{locale} } ) { | 
|  | 17890 |  |  |  |  | 28108 |  | 
|  | 17890 |  |  |  |  | 35859 |  | 
| 75 | 35778 | 50 |  |  |  | 66789 | unless ( HASH_REF eq ref $source ) { | 
| 76 | 0 |  |  |  |  | 0 | require Carp; | 
| 77 | 0 |  |  |  |  | 0 | Carp::confess( "\$source is '$source'" ); | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 35778 | 100 |  |  |  | 76151 | my $data = $source->{$lc} | 
| 80 |  |  |  |  |  |  | or next; | 
| 81 | 17885 |  |  |  |  | 23537 | foreach my $key ( @{ $arg{text} } ) { | 
|  | 17885 |  |  |  |  | 30120 |  | 
| 82 |  |  |  |  |  |  | my $code = $deref{ ref $data } | 
| 83 | 33846 | 50 |  |  |  | 74311 | or do { | 
| 84 | 0 |  |  |  |  | 0 | require Carp; | 
| 85 | 0 |  |  |  |  | 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 | 33846 | 100 |  |  |  | 78888 | or next SOURCE_LOOP; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | wantarray | 
| 94 | 3582 | 100 |  |  |  | 20646 | or return $data; | 
| 95 | 26 |  |  |  |  | 125 | push @rslt, $data; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | wantarray | 
| 99 | 14322 | 100 |  |  |  | 59690 | or return $arg{default}; | 
| 100 | 25 |  |  |  |  | 163 | 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 |  |  |  |  |  |  | sub __message { | 
| 149 | 24 |  |  | 24 |  | 75 | my ( $msg, @arg ) = @_; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 24 | 100 |  |  |  | 144 | instance( $msg, 'Template::Exception' ) | 
| 152 |  |  |  |  |  |  | and return _message_template_exception( $msg, @arg ); | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 23 |  |  |  |  | 496 | my $lcl = __localize( | 
| 155 |  |  |  |  |  |  | text	=> [ '+message', $msg ], | 
| 156 |  |  |  |  |  |  | default	=> $msg, | 
| 157 |  |  |  |  |  |  | ); | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 23 | 50 |  |  |  | 98 | CODE_REF eq ref $lcl | 
| 160 |  |  |  |  |  |  | and return $lcl->( $msg, @arg ); | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 23 | 50 |  |  |  | 291 | $lcl =~ m/ \[ % /smx | 
| 163 |  |  |  |  |  |  | or return join ' ', $lcl, @arg; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 |  |  |  |  | 0 | my $tt = Template->new(); | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 0 |  |  |  |  | 0 | my $output; | 
| 168 | 0 |  |  |  |  | 0 | $tt->process( \$lcl, { | 
| 169 |  |  |  |  |  |  | arg	=> \@arg, | 
| 170 |  |  |  |  |  |  | }, \$output ); | 
| 171 |  |  |  |  |  |  |  | 
| 172 | 0 |  |  |  |  | 0 | return $output; | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | sub _message_template_exception { | 
| 176 | 1 |  |  | 1 |  | 4 | my ( $ex, @arg ) = @_; | 
| 177 | 1 |  |  |  |  | 6 | 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 | 50 |  |  |  | 9 | m/ \n \z /smx | 
| 181 |  |  |  |  |  |  | and return $ex; | 
| 182 | 1 |  |  |  |  | 6 | $_ = join ' ', $_, @arg; | 
| 183 | 1 | 50 |  |  |  | 9 | m/ [.?|] \z /smx | 
| 184 |  |  |  |  |  |  | or $_ .= '.'; | 
| 185 | 1 |  |  |  |  | 2 | $_ .= "\n"; | 
| 186 | 1 |  |  |  |  | 5 | my $class = ref $ex; | 
| 187 | 1 |  |  |  |  | 5 | return $class->new( $ex->type(), $_ ); | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | sub __preferred { | 
| 191 | 2 |  | 33 | 2 |  | 10 | $locale ||= _load(); | 
| 192 | 2 | 100 |  |  |  | 11 | return wantarray ? @lang : $lang[0]; | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | sub _load { | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | # Pick up the languages from the environment | 
| 198 | 6 |  |  | 6 |  | 67 | @lang = I18N::LangTags::implicate_supers( | 
| 199 |  |  |  |  |  |  | I18N::LangTags::Detect::detect() ); | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | # Normalize the language names. | 
| 202 | 6 |  |  |  |  | 2762 | foreach ( @lang ) { | 
| 203 | 2 | 100 |  |  |  | 459 | s/ ( [^_-]+ ) [_-] (.* ) /\L$1_\U$2/smx | 
| 204 |  |  |  |  |  |  | or $_ = lc $_; | 
| 205 | 2 | 50 |  |  |  | 9 | 'c' eq $_ | 
| 206 |  |  |  |  |  |  | and $_ = uc $_; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | # Append the default locale name. | 
| 210 | 6 | 50 |  |  |  | 48 | grep { 'C' eq $_ } @lang | 
|  | 2 |  |  |  |  | 8 |  | 
| 211 |  |  |  |  |  |  | or push @lang, 'C'; | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | # Accumulator for locale data. | 
| 214 | 6 |  |  |  |  | 16 | my @locales; | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | # Put all the user's data in a hash. | 
| 217 | 6 |  |  |  |  | 21 | push @locales, {}; | 
| 218 | 6 |  |  |  |  | 26 | foreach my $lc ( @lang ) { | 
| 219 | 8 |  |  |  |  | 208 | eval {	## no critic (ErrorHandling::RequireCheckingReturnValueOfEval) | 
| 220 | 8 | 50 |  |  |  | 68 | defined( my $path = expand_tilde( "~~/locale/$lc.pm" ) ) | 
| 221 |  |  |  |  |  |  | or return; | 
| 222 | 3 |  |  |  |  | 6 | my $data; | 
| 223 |  |  |  |  |  |  | $data = do $path | 
| 224 |  |  |  |  |  |  | and HASH_REF eq ref $data | 
| 225 | 3 | 100 | 66 |  |  | 494 | and $locales[-1]{$lc} = $data; | 
| 226 |  |  |  |  |  |  | }; | 
| 227 |  |  |  |  |  |  | } | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | # Put the system-wide data in a hash. | 
| 230 | 6 |  |  |  |  | 402 | push @locales, {}; | 
| 231 | 6 |  |  |  |  | 20 | foreach my $lc ( @lang ) { | 
| 232 | 8 |  |  |  |  | 39 | my $mod_name = __PACKAGE__ . "::$lc"; | 
| 233 | 8 |  |  |  |  | 18 | my $data; | 
| 234 |  |  |  |  |  |  | $data = eval "require $mod_name" | 
| 235 |  |  |  |  |  |  | and HASH_REF eq ref $data | 
| 236 | 8 | 100 | 66 |  |  | 706 | and $locales[-1]{$lc} = $data; | 
| 237 |  |  |  |  |  |  | } | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | # Return a reference to the names of locales. | 
| 240 | 6 |  |  |  |  | 47 | return \@locales; | 
| 241 |  |  |  |  |  |  | } | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | 1; | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | __END__ |