1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
package # hide from PAUSE
ModuleGenerator::Locale;
use v5.22;
use strict;
use warnings;
use feature qw( postderef signatures );
use namespace::autoclean;
use DateTime::Locale::Util qw( parse_locale_code );
use JSON::MaybeXS qw( decode_json );
use Moose;
use MooseX::Types::Moose qw( ArrayRef HashRef Int Maybe Str );
use MooseX::Types::Path::Class qw( Dir File );
## no critic (TestingAndDebugging::ProhibitNoWarnings)
no warnings qw( experimental::postderef experimental::signatures );
## use critic
has code => (
is => 'ro',
isa => Str,
required => 1,
);
has _source_data_root => (
is => 'ro',
isa => Dir,
required => 1,
init_arg => 'source_data_root',
);
has _parent_code => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_parent_code',
);
has _parent_locale => (
is => 'ro',
isa => Maybe ['ModuleGenerator::Locale'],
lazy => 1,
builder => '_build_parent_locale',
);
has _json_file => (
is => 'ro',
isa => File,
lazy => 1,
builder => '_build_json_file',
);
has _glibc_file => (
is => 'ro',
isa => File,
lazy => 1,
builder => '_build_glibc_file',
);
has _glibc_data => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => '_build_glibc_data',
);
has _parsed_code => (
is => 'ro',
isa => ArrayRef [ Maybe [Str] ],
lazy => 1,
builder => '_build_parsed_code',
);
has language_code => (
is => 'ro',
isa => Str,
lazy => 1,
default => sub ($self) { $self->_parsed_code->[0] },
);
has script_code => (
is => 'ro',
isa => Maybe [Str],
lazy => 1,
default => sub ($self) { $self->_parsed_code->[1] },
);
has territory_code => (
is => 'ro',
isa => Maybe [Str],
lazy => 1,
default => sub ($self) { $self->_parsed_code->[2] },
);
has variant_code => (
is => 'ro',
isa => Maybe [Str],
lazy => 1,
default => sub ($self) { $self->_parsed_code->[3] },
);
has en_name => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_en_name',
);
has native_name => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_native_name',
);
for my $lang (qw( en native )) {
for my $part (qw( language territory script variant )) {
my $attr = q{_} . $lang . q{_} . $part;
has $attr => (
is => 'ro',
isa => Maybe [Str],
lazy => 1,
builder => '_build' . $attr,
);
}
}
has _cldr_json_data => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => '_build_cldr_json_data',
);
has _first_day_of_week => (
is => 'ro',
isa => Int,
lazy => 1,
builder => '_build_first_day_of_week',
);
has version => (
is => 'ro',
isa => Int,
lazy => 1,
builder => '_build_version',
);
has data_hash => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => '_build_data_hash',
);
{
my %Cache;
sub instance ($class, %p) {
return $Cache{ $p{code} } //= $class->new(%p);
}
}
## no critic (ValuesAndExpressions::ProhibitFiletest_f)
sub source_files ($self) {
return grep {-f} $self->_json_file, $self->_glibc_file;
}
sub _build_cldr_json_data($self) {
my $json = $self->_json_from( $self->_json_file );
my $json_file_id = $self->_json_file->parent->basename;
return $json->{main}{$json_file_id};
}
sub _build_data_hash ($self) {
my $cal_root = $self->_cldr_json_data->{dates}{calendars}{gregorian};
my %data = (
## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements)
am_pm_abbreviated =>
[ $cal_root->{dayPeriods}{format}{abbreviated}->@{ 'am', 'pm' } ],
available_formats => $cal_root->{dateTimeFormats}{availableFormats},
code => $self->code,
first_day_of_week => $self->_first_day_of_week,
version => $self->version,
$self->_glibc_data->%*,
);
for my $thing (qw( name language script territory variant )) {
for my $type (qw( en native )) {
my $meth
= ( $thing eq 'name' ? q{} : q{_} ) . $type . q{_} . $thing;
my $key = join q{_}, ( ( $type eq 'en' ? () : $type ), $thing );
$data{$key} = $self->$meth;
}
}
for my $thing (qw( date time dateTime )) {
for my $length (qw( full long medium short )) {
my $val = $cal_root->{ $thing . 'Formats' }{$length};
$data{ lc $thing . q{_format_} . $length }
= ref $val ? $val->{_value} : $val;
}
}
my %ordering = (
day => [qw( mon tue wed thu fri sat sun )],
month => [ 1 .. 12 ],
quarter => [ 1 .. 4 ],
);
my @lengths = qw( abbreviated narrow wide );
for my $thing (qw( day month quarter )) {
for my $type (qw( format stand-alone )) {
for my $length (@lengths) {
my $key = join q{_}, $thing, ( $type =~ s/-/_/gr ), $length;
$data{$key}
= [ $cal_root->{ $thing . 's' }{$type}{$length}
->@{ $ordering{$thing}->@* } ];
}
}
}
my %eraLength = (
narrow => 'Narrow',
abbreviated => 'Abbr',
wide => 'Names',
);
for my $length (@lengths) {
## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements)
$data{ 'era_' . $length }
= [ $cal_root->{eras}{ 'era' . $eraLength{$length} }->@{ 0, 1 } ];
}
return \%data;
}
sub _build_glibc_data ($self) {
my $parent = $self->_parent_locale;
unless ( -f $self->_glibc_file ) {
return $parent->_glibc_data;
}
my $raw = $self->_glibc_file->slurp;
return {
glibc_datetime_format =>
$self->_extract_glibc_value( 'd_t_fmt', $raw )
// $parent->_glibc_data->{glibc_datetime_format},
glibc_date_format => $self->_extract_glibc_value( 'd_fmt', $raw )
// $parent->_glibc_data->{glibc_date_format},
glibc_date_1_format => $self->_extract_glibc_value( 'date_fmt', $raw )
// $parent->_glibc_data->{glibc_date_1_format},
glibc_time_format => $self->_extract_glibc_value( 't_fmt', $raw )
// $parent->_glibc_data->{glibc_time_format},
glibc_time_12_format =>
$self->_extract_glibc_value( 't_fmt_ampm', $raw )
// $parent->_glibc_data->{glibc_time_12_format},
};
}
sub _build_glibc_file ($self) {
my $glibc_code = join '_', grep {defined} $self->language_code,
$self->territory_code;
if ( my $script = $self->_en_script ) {
$glibc_code .= '@' . lc $script;
}
# This ensures some sort of sanish fallback
$glibc_code = 'POSIX' if $self->code eq 'root';
return $self->_source_data_root->file( 'glibc-locales', $glibc_code );
}
sub _extract_glibc_value ($self, $key, $raw) {
my ($val) = $raw =~ /^\Q$key\E\s+"([^"]+?)"/m
or return;
$val =~ s/[\\\/]\n//g;
$val =~ s/\<U([A-F\d]+)\>/chr(hex($1))/eg;
return $val;
}
sub _build_version ($self) {
return $self->_cldr_json_data->{identity}{version}{_cldrVersion};
}
sub _build_json_file ($self) {
my $code_file = $self->_source_data_root->file(
qw( cldr-dates-full main ),
$self->code, 'ca-gregorian.json'
);
return $code_file if -f $code_file;
my $parent_file = $self->_source_data_root->file(
qw( cldr-dates-full main ),
$self->_parent_code,
'ca-gregorian.json'
);
unless ( -f $parent_file ) {
die "Could not find $code_file or $parent_file for locale ",
$self->code, "\n";
}
return $parent_file;
}
sub _build_parent_code ($self) {
my $explicit_parent = $self->_explicit_parents;
return $explicit_parent->{ $self->code }
if $explicit_parent->{ $self->code };
return
$self->code =~ /-/ ? $self->code =~ s/-[^-]+$//r
: $self->code ne 'root' ? 'root'
: die 'There is no parent for the root locale!';
}
sub _has_parent_code ($self) {
return $self->code ne 'root';
}
sub _build_parent_locale ($self) {
return unless $self->_has_parent_code;
return ModuleGenerator::Locale->instance(
code => $self->_parent_code,
source_data_root => $self->_source_data_root
);
}
sub _explicit_parents ($self) {
state $explicit_parents;
return $explicit_parents if $explicit_parents;
my $json = $self->_json_from(
$self->_source_data_root->file(
qw( cldr-core supplemental parentLocales.json ))
);
return $explicit_parents
= $json->{supplemental}{parentLocales}{parentLocale};
}
sub _build_parsed_code ($self) {
return [ parse_locale_code( $self->code ) ];
}
{
my $i = 1;
my %days = map { $_ => $i++ } qw( mon tue wed thu fri sat sun );
sub _build_first_day_of_week {
my $self = shift;
my $terr = $self->territory_code;
return 1 unless defined $terr;
my $index = $self->_first_day_of_week_index;
return $index->{$terr} ? $days{ $index->{$terr} } : 1;
}
}
sub _first_day_of_week_index ($self) {
state $first_day_of_week_index;
return $first_day_of_week_index if $first_day_of_week_index;
my $json = $self->_json_from(
$self->_source_data_root->file(
qw( cldr-core supplemental weekData.json ))
);
return $first_day_of_week_index
= $json->{supplemental}{weekData}{firstDay};
}
sub _build_en_name ($self) {
return join q{ }, grep {defined} $self->_en_language,
$self->_en_territory, $self->_en_script, $self->_en_variant;
}
sub _build_en_language ($self) {
return unless $self->language_code;
return $self->_en_languages_data->{ $self->language_code };
}
sub _build_en_territory ($self) {
return unless $self->territory_code;
return $self->_en_territories_data->{ $self->territory_code };
}
sub _build_en_script ($self) {
return unless $self->script_code;
return $self->_en_scripts_data->{ $self->script_code };
}
sub _build_en_variant ($self) {
return unless $self->variant_code;
return $self->_en_variants_data->{ $self->variant_code };
}
sub _en_languages_data ($self) {
state $en_languages_data;
return $en_languages_data //= $self->_populate_en_lookup('languages');
}
sub _en_territories_data ($self) {
state $en_territories_data;
return $en_territories_data //= $self->_populate_en_lookup('territories');
}
sub _en_scripts_data ($self) {
state $en_scripts_data;
return $en_scripts_data //= $self->_populate_en_lookup('scripts');
}
sub _en_variants_data ($self) {
state $en_variants_data;
return $en_variants_data //= $self->_populate_en_lookup('variants');
}
sub _populate_en_lookup ($self, $type) {
my $json = $self->_json_from(
$self->_source_data_root->file(
qw( cldr-localenames-full main en ), $type . '.json'
)
);
return $json->{main}{en}{localeDisplayNames}{$type};
}
sub _build_native_name ($self) {
return join q{ }, grep {defined} $self->_native_language,
$self->_native_territory, $self->_native_script,
$self->_native_variant;
}
sub _build_native_language ($self) {
return unless $self->language_code;
return $self->_native_lookup('languages')->{ $self->language_code };
}
sub _build_native_territory ($self) {
return unless $self->territory_code;
return $self->_native_lookup('territories')->{ $self->territory_code };
}
sub _build_native_script ($self) {
return unless $self->script_code;
return $self->_native_lookup('scripts')->{ $self->script_code };
}
sub _build_native_variant ($self) {
return unless $self->variant_code;
return $self->_native_lookup('variants')->{ $self->variant_code };
}
sub _native_lookup ($self, $type) {
my $file;
my $locale = $self;
while ($locale) {
$file = $self->_source_data_root->file(
qw( cldr-localenames-full main ),
$locale->code, $type . '.json'
);
last if -f $file;
$locale = $locale->_parent_locale;
}
return {} unless -f $file;
my $json = $self->_json_from($file);
return $json->{main}{ $locale->code }{localeDisplayNames}{$type};
}
sub _json_from ($self, $file) {
return decode_json( $file->slurp( iomode => '<:raw' ) );
}
__PACKAGE__->meta->make_immutable;
1;
|