line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Duration::Concise::Localize; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
59015
|
use 5.006; |
|
3
|
|
|
|
|
9
|
|
4
|
3
|
|
|
3
|
|
11
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
53
|
|
5
|
3
|
|
|
3
|
|
8
|
use warnings FATAL => 'all'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
99
|
|
6
|
3
|
|
|
3
|
|
2152
|
use utf8; |
|
3
|
|
|
|
|
24
|
|
|
3
|
|
|
|
|
9
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
82
|
use Carp; |
|
3
|
|
|
|
|
2
|
|
|
3
|
|
|
|
|
172
|
|
9
|
3
|
|
|
3
|
|
12
|
use base qw(Time::Duration::Concise); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1052
|
|
10
|
|
|
|
|
|
|
use Module::Pluggable |
11
|
3
|
|
|
|
|
17
|
search_path => 'Time::Duration::Concise::Locale', |
12
|
|
|
|
|
|
|
sub_name => 'translation_classes', |
13
|
3
|
|
|
3
|
|
1203
|
require => 1; |
|
3
|
|
|
|
|
24476
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
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
|
|
|
25
|
1
|
26
|
my ($self, $locale) = @_; |
54
|
25
|
50
|
|
|
|
46
|
$self->{'locale'} = lc $locale if $locale; |
55
|
25
|
|
|
|
|
55
|
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
|
|
26
|
my ($self, $req_locale) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# If locale already cached do not load |
73
|
24
|
|
|
|
|
29
|
my $cached = exists $locale_cache{$req_locale}; |
74
|
|
|
|
|
|
|
|
75
|
24
|
100
|
|
|
|
36
|
if (!$cached) { |
76
|
2
|
|
|
|
|
10
|
foreach my $locale_module ($self->translation_classes) { |
77
|
30
|
|
|
|
|
7850
|
my $load_locale = (split('::', $locale_module))[-1]; |
78
|
30
|
50
|
|
|
|
60
|
next if (exists $locale_cache{$load_locale}); # Got this one already. |
79
|
30
|
50
|
|
|
|
261
|
next unless ($locale_module->can('translation')); # Chocolate in our peanut butter. |
80
|
30
|
|
|
|
|
76
|
$locale_cache{$load_locale} = $locale_module->translation(); |
81
|
30
|
100
|
|
|
|
53
|
$cached = 1 if ($load_locale eq $req_locale); # Good news, everyone! |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
24
|
50
|
|
|
|
54
|
return ($cached) ? $locale_cache{$req_locale} : $locale_cache{en}; # English default. |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub as_string { |
89
|
24
|
|
|
24
|
1
|
4438
|
my ($self, $precision) = @_; |
90
|
|
|
|
|
|
|
|
91
|
24
|
|
|
|
|
42
|
my $translation = $self->_load_translation($self->locale); |
92
|
|
|
|
|
|
|
|
93
|
24
|
|
|
|
|
27
|
return join(' ', map { join ' ', ($_->{'value'}, $translation->{$_->{'unit'}}) } @{$self->duration_array($precision)}); |
|
51
|
|
|
|
|
187
|
|
|
24
|
|
|
|
|
90
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 new |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Object constructor |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub new { ## no critic (RequireArgUnpacking) |
103
|
10
|
|
|
10
|
1
|
90841
|
my $class = shift; |
104
|
10
|
50
|
|
|
|
47
|
my %params_ref = ref($_[0]) ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
105
|
|
|
|
|
|
|
|
106
|
10
|
|
|
|
|
14
|
my $interval = $params_ref{'interval'}; |
107
|
|
|
|
|
|
|
|
108
|
10
|
|
|
|
|
44
|
my $whatsit = ref($interval); |
109
|
|
|
|
|
|
|
|
110
|
10
|
50
|
|
|
|
27
|
if ($whatsit eq __PACKAGE__) { |
111
|
0
|
|
|
|
|
0
|
$interval = $interval->seconds; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
10
|
100
|
|
|
|
20
|
if (not defined $interval) { |
115
|
1
|
|
|
|
|
17
|
confess "Missing required arguments"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
9
|
|
|
|
|
46
|
my $self = $class->SUPER::new(interval => $interval); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Set default locale as english |
121
|
9
|
|
|
|
|
24
|
$self->{'locale'} = 'en'; |
122
|
9
|
50
|
|
|
|
18
|
if (exists $params_ref{'locale'}) { |
123
|
9
|
|
|
|
|
15
|
$self->{'locale'} = lc $params_ref{'locale'}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
9
|
|
|
|
|
11
|
my $obj = bless $self, $class; |
127
|
9
|
|
|
|
|
15
|
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 |