line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Duration::Concise::Localize; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
195786
|
use 5.006; |
|
3
|
|
|
|
|
36
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
62
|
|
5
|
3
|
|
|
3
|
|
11
|
use warnings FATAL => 'all'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
113
|
|
6
|
3
|
|
|
3
|
|
1620
|
use utf8; |
|
3
|
|
|
|
|
38
|
|
|
3
|
|
|
|
|
12
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
82
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
203
|
|
9
|
3
|
|
|
3
|
|
17
|
use base qw(Time::Duration::Concise); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1313
|
|
10
|
|
|
|
|
|
|
use Module::Pluggable |
11
|
3
|
|
|
|
|
20
|
search_path => 'Time::Duration::Concise::Locale', |
12
|
|
|
|
|
|
|
sub_name => 'translation_classes', |
13
|
3
|
|
|
3
|
|
1434
|
require => 1; |
|
3
|
|
|
|
|
28902
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '2.62'; |
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
|
|
|
25
|
1
|
50
|
my ($self, $locale) = @_; |
54
|
25
|
50
|
|
|
|
51
|
$self->{'locale'} = lc $locale if $locale; |
55
|
25
|
|
|
|
|
75
|
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
|
|
|
|
|
|
|
my %locale_cache = (en => +{map { $_ => $_ } map { ($_, $_ . 's') } qw(second minute hour day)}); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _load_translation { |
70
|
24
|
|
|
24
|
|
46
|
my ($self, $req_locale) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# If locale already cached do not load |
73
|
24
|
|
|
|
|
39
|
my $cached = exists $locale_cache{$req_locale}; |
74
|
|
|
|
|
|
|
|
75
|
24
|
100
|
|
|
|
51
|
if (!$cached) { |
76
|
2
|
|
|
|
|
10
|
foreach my $locale_module ($self->translation_classes) { |
77
|
30
|
|
|
|
|
11741
|
my $load_locale = (split('::', $locale_module))[-1]; |
78
|
30
|
50
|
|
|
|
98
|
next if (exists $locale_cache{$load_locale}); # Got this one already. |
79
|
30
|
50
|
|
|
|
296
|
next unless ($locale_module->can('translation')); # Chocolate in our peanut butter. |
80
|
30
|
|
|
|
|
112
|
$locale_cache{$load_locale} = $locale_module->translation(); |
81
|
30
|
100
|
|
|
|
75
|
$cached = 1 if ($load_locale eq $req_locale); # Good news, everyone! |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
24
|
50
|
|
|
|
69
|
return ($cached) ? $locale_cache{$req_locale} : $locale_cache{en}; # English default. |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub as_string { |
89
|
24
|
|
|
24
|
1
|
5383
|
my ($self, $precision) = @_; |
90
|
|
|
|
|
|
|
|
91
|
24
|
|
|
|
|
58
|
my $translation = $self->_load_translation($self->locale); |
92
|
|
|
|
|
|
|
|
93
|
24
|
|
|
|
|
47
|
return join(' ', map { join ' ', ($_->{'value'}, $translation->{$_->{'unit'}}) } @{$self->duration_array($precision)}); |
|
51
|
|
|
|
|
259
|
|
|
24
|
|
|
|
|
85
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 new |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Object constructor |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub new { ## no critic (RequireArgUnpacking) |
103
|
10
|
|
|
10
|
1
|
8878
|
my $class = shift; |
104
|
10
|
50
|
|
|
|
45
|
my %params_ref = ref($_[0]) ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
105
|
|
|
|
|
|
|
|
106
|
10
|
|
|
|
|
22
|
my $interval = $params_ref{'interval'}; |
107
|
|
|
|
|
|
|
|
108
|
10
|
|
|
|
|
16
|
my $whatsit = ref($interval); |
109
|
|
|
|
|
|
|
|
110
|
10
|
50
|
|
|
|
30
|
if ($whatsit eq __PACKAGE__) { |
111
|
0
|
|
|
|
|
0
|
$interval = $interval->seconds; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
10
|
100
|
|
|
|
26
|
if (not defined $interval) { |
115
|
1
|
|
|
|
|
25
|
confess "Missing required arguments"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
9
|
|
|
|
|
41
|
my $self = $class->SUPER::new(interval => $interval); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Set default locale as english |
121
|
9
|
|
|
|
|
28
|
$self->{'locale'} = 'en'; |
122
|
9
|
50
|
|
|
|
21
|
if (exists $params_ref{'locale'}) { |
123
|
9
|
|
|
|
|
19
|
$self->{'locale'} = lc $params_ref{'locale'}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
9
|
|
|
|
|
17
|
my $obj = bless $self, $class; |
127
|
9
|
|
|
|
|
22
|
return $obj; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Binary.com, C<< >> |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 BUGS |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
137
|
|
|
|
|
|
|
the web interface at L. 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 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
L |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * CPAN Ratings |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
L |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item * Search CPAN |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
L |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=back |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
1; # End of Time::Duration::Concise::Localize |