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
|
package DateTime::Format::Human::Duration::Locale;
# require DateTime::Format::Locale;
use strict;
use warnings;
sub calc_locale {
my ($span, $loc) = @_;
# DateTime::Format::Locale::
my $final = determine_locale_from({
'base_object' => $span,
'get_locale_from' => $loc,
'locale_ns_path' => 'DateTime/Format/Human/Duration/Locale', # DateTime::Format::Human::Duration::Locale
});
if ($final) {
return $final if ref $final; # returned 'locale_cache' we created below
my $ns = "DateTime::Format::Human::Duration::Locale::$final";
# get_human_span_from_units_array has been deprecated, but we'll
# still support it.
if ( $ns->can('get_human_span_from_units_array') ) {
$span->{'locale_cache'}{ $final } = $ns;
}
elsif ( $ns->can('get_human_span_from_units') ) {
$span->{'locale_cache'}{ $final } = $ns;
}
elsif ( my $sub = $ns->can('get_human_span_hashref') ) {
$span->{'locale_cache'}{ $final } = $sub->();
}
if ( exists $span->{'locale_cache'}{ $final } ) {
return $span->{'locale_cache'}{ $final };
}
}
return '';
}
# DateTime::Format::Locale::
sub determine_locale_from {
my ($args_hr) = @_;
return '' if !$args_hr->{'get_locale_from'};
if (ref $args_hr->{'get_locale_from'}) {
my $locale_obj;
if (UNIVERSAL::can($args_hr->{'get_locale_from'}, 'locale')) {
$locale_obj = $args_hr->{'get_locale_from'}->locale;
}
else {
$locale_obj = $args_hr->{'get_locale_from'};
}
if (UNIVERSAL::can($locale_obj, 'code')) {
$args_hr->{'get_locale_from'} = $locale_obj->code; # DateTime::Locale v1
}
elsif (UNIVERSAL::can($locale_obj, 'id')) {
$args_hr->{'get_locale_from'} = $locale_obj->id; # DateTime::Locale v0
}
else {
my $ns = ref($args_hr->{'get_locale_from'});
($args_hr->{'get_locale_from'}) = reverse split /::/, $ns;
}
}
my ($short) = split(/[-_]+/,$args_hr->{'get_locale_from'});
my $final = '';
my @try = $args_hr->{'get_locale_from'} eq $short ? ($args_hr->{'get_locale_from'}) : ($args_hr->{'get_locale_from'}, $short);
NS:
for my $locale ( @try ) {
if ( exists $args_hr->{'base_object'}{'locale_cache'}{ $locale } ) {
if ( $args_hr->{'base_object'}{'locale_cache'}{ $locale } ) {
return $locale;
}
else {
next NS;
}
}
$args_hr->{'locale_ns_path'} =~ s{/$}{};
my $path = "$args_hr->{'locale_ns_path'}/$locale\.pm";
if( exists $INC{$path} || eval { $args_hr->{'loads'}{$locale}++; require $path } ) {
$final = $locale;
$args_hr->{'base_object'}{'locale_cache'}{ $locale } = 1;
last NS;
}
else {
push @{$args_hr->{'errors'}{$locale}}, $@;
$args_hr->{'base_object'}{'locale_cache'}{ $locale } = '';
}
}
return $final;
}
1;
|