| 1 | package Time::Duration::Concise::Localize; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use base qw(Time::Duration::Concise); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | use Module::Pluggable | ||||||||||
| 11 | 4 | search_path => 'Time::Duration::Concise::Locale', | |||||||||
| 12 | sub_name => 'translation_classes', | ||||||||||
| 13 | 4 | ▶ | require => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 4 | our $VERSION = '2.61'; | |||||||||
| 16 | |||||||||||
| 17 | =head1 NAME | ||||||||||
| 18 | |||||||||||
| 19 | Time::Duration::Concise::Localize - localize concise time duration string representation. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | Time::Duration::Concise provides localize concise time duration string representation. | ||||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | use Time::Duration::Concise::Localize; | ||||||||||
| 28 | |||||||||||
| 29 | my $duration = Time::Duration::Concise::Localize->new( | ||||||||||
| 30 | |||||||||||
| 31 | # concise time interval | ||||||||||
| 32 | 'interval' => '1.5h', | ||||||||||
| 33 | |||||||||||
| 34 | # Locale for translation | ||||||||||
| 35 | 'locale' => 'en' | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | $duration->as_string; | ||||||||||
| 39 | |||||||||||
| 40 | =head1 FIELDS | ||||||||||
| 41 | |||||||||||
| 42 | =head2 interval (REQUIRED) | ||||||||||
| 43 | |||||||||||
| 44 | concise interval string | ||||||||||
| 45 | |||||||||||
| 46 | =head2 locale | ||||||||||
| 47 | |||||||||||
| 48 | Get and set the locale for translation | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | sub locale { | ||||||||||
| 53 | 25 | ▶ | my ($self, $locale) = @_; | ||||||||
|
Subroutine
locale: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 54 | 25 | ▶ | $self->{'locale'} = lc $locale if $locale; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 25 | return $self->{'locale'}; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | =head1 METHODS | ||||||||||
| 59 | |||||||||||
| 60 | =head2 as_string | ||||||||||
| 61 | |||||||||||
| 62 | Localized duration string | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | # Map self and plurals for English default. Should use KNOWN_UNITS... | ||||||||||
| 67 | 4 | my %locale_cache = (en => +{map { $_ => $_ } map { ($_, $_ . 's') } qw(second minute hour day)}); | |||||||||
| 68 | |||||||||||
| 69 | sub _load_translation { | ||||||||||
| 70 | 24 | ▶ | my ($self, $req_locale) = @_; | ||||||||
|
Subroutine
_load_translation: called CC=7 SCAR=19.7
| |||||||||||
| 71 | |||||||||||
| 72 | # If locale already cached do not load | ||||||||||
| 73 | 24 | my $cached = exists $locale_cache{$req_locale}; | |||||||||
| 74 | |||||||||||
| 75 | 24 | ▶ | if (!$cached) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 2 | foreach my $locale_module ($self->translation_classes) { | |||||||||
| 77 | 30 | my $load_locale = (split('::', $locale_module))[-1]; | |||||||||
| 78 | 30 | ▶ | next if (exists $locale_cache{$load_locale}); # Got this one already. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 30 | ▶ | next unless ($locale_module->can('translation')); # Chocolate in our peanut butter. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 30 | $locale_cache{$load_locale} = $locale_module->translation(); | |||||||||
| 81 | 30 | ▶ | $cached = 1 if ($load_locale eq $req_locale); # Good news, everyone! | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 24 | ▶ | return ($cached) ? $locale_cache{$req_locale} : $locale_cache{en}; # English default. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub as_string { | ||||||||||
| 89 | 24 | ▶ | my ($self, $precision) = @_; | ||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | |||||||||||
| 91 | 24 | my $translation = $self->_load_translation($self->locale); | |||||||||
| 92 | |||||||||||
| 93 | 24 | return join(' ', map { join ' ', ($_->{'value'}, $translation->{$_->{'unit'}}) } @{$self->duration_array($precision)}); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 new | ||||||||||
| 97 | |||||||||||
| 98 | Object constructor | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | sub new { ## no critic (RequireArgUnpacking) | ||||||||||
| 103 | 10 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||
| 104 | 10 | ▶ | my %params_ref = ref($_[0]) ? %{$_[0]} : @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 10 | my $interval = $params_ref{'interval'}; | |||||||||
| 107 | |||||||||||
| 108 | 10 | my $whatsit = ref($interval); | |||||||||
| 109 | |||||||||||
| 110 | 10 | ▶ | if ($whatsit eq __PACKAGE__) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | $interval = $interval->seconds; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 10 | ▶ | if (not defined $interval) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 1 | confess "Missing required arguments"; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 9 | my $self = $class->SUPER::new(interval => $interval); | |||||||||
| 119 | |||||||||||
| 120 | # Set default locale as english | ||||||||||
| 121 | 9 | $self->{'locale'} = 'en'; | |||||||||
| 122 | 9 | ▶ | if (exists $params_ref{'locale'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 9 | $self->{'locale'} = lc $params_ref{'locale'}; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 9 | my $obj = bless $self, $class; | |||||||||
| 127 | 9 | return $obj; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | =head1 AUTHOR | ||||||||||
| 131 | |||||||||||
| 132 | Binary.com, C<< <perl at binary.com> >> | ||||||||||
| 133 | |||||||||||
| 134 | =head1 BUGS | ||||||||||
| 135 | |||||||||||
| 136 | Please report any bugs or feature requests to C<bug-time-duration-concise-localize at rt.cpan.org>, or through | ||||||||||
| 137 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Duration-Concise-Localize>. I will be notified, and then you'll | ||||||||||
| 138 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 139 | |||||||||||
| 140 | =head1 SUPPORT | ||||||||||
| 141 | |||||||||||
| 142 | You can find documentation for this module with the perldoc command. | ||||||||||
| 143 | |||||||||||
| 144 | perldoc Time::Duration::Concise::Localize | ||||||||||
| 145 | |||||||||||
| 146 | |||||||||||
| 147 | You can also look for information at: | ||||||||||
| 148 | |||||||||||
| 149 | =over 4 | ||||||||||
| 150 | |||||||||||
| 151 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 152 | |||||||||||
| 153 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Time-Duration-Concise-Localize> | ||||||||||
| 154 | |||||||||||
| 155 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 156 | |||||||||||
| 157 | L<http://annocpan.org/dist/Time-Duration-Concise-Localize> | ||||||||||
| 158 | |||||||||||
| 159 | =item * CPAN Ratings | ||||||||||
| 160 | |||||||||||
| 161 | L<http://cpanratings.perl.org/d/Time-Duration-Concise-Localize> | ||||||||||
| 162 | |||||||||||
| 163 | =item * Search CPAN | ||||||||||
| 164 | |||||||||||
| 165 | L<http://search.cpan.org/dist/Time-Duration-Concise-Localize/> | ||||||||||
| 166 | |||||||||||
| 167 | =back | ||||||||||
| 168 | |||||||||||
| 169 | =cut | ||||||||||
| 170 | |||||||||||
| 171 | 4 | 1; # End of Time::Duration::Concise::Localize | |||||||||