File Coverage

blib/lib/Time/Duration/Concise/Localize.pm
Criterion Covered Total %
statement 51 53 96.2
branch 13 20 65.0
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 78 87 89.6


line stmt bran cond sub pod time code
1             package Time::Duration::Concise::Localize;
2              
3 4     4   65177 use 5.006;
  4         11  
4 4     4   13 use strict;
  4         8  
  4         74  
5 4     4   11 use warnings FATAL => 'all';
  4         15  
  4         119  
6 4     4   1903 use utf8;
  4         32  
  4         13  
7              
8 4     4   91 use Carp;
  4         4  
  4         205  
9 4     4   13 use base qw(Time::Duration::Concise);
  4         4  
  4         1476  
10             use Module::Pluggable
11 4         19 search_path => 'Time::Duration::Concise::Locale',
12             sub_name => 'translation_classes',
13 4     4   1610 require => 1;
  4         31521  
14              
15             our $VERSION = '2.6';
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 VERSION
26              
27             Version 2.6
28              
29             =head1 SYNOPSIS
30              
31             use Time::Duration::Concise::Localize;
32              
33             my $duration = Time::Duration::Concise::Localize->new(
34              
35             # concise time interval
36             'interval' => '1.5h',
37              
38             # Locale for translation
39             'locale' => 'en'
40             );
41              
42             $duration->as_string;
43              
44             =head1 FIELDS
45              
46             =head2 interval (REQUIRED)
47              
48             concise interval string
49              
50             =head2 locale
51              
52             Get and set the locale for translation
53              
54             =cut
55              
56             sub locale {
57 25     25 1 23 my ( $self, $locale ) = @_;
58 25 50       44 $self->{'locale'} = lc $locale if $locale;
59 25         51 return $self->{'locale'};
60             }
61              
62             =head1 METHODS
63              
64             =head2 as_string
65              
66             Localized duration string
67              
68             =cut
69              
70             # Map self and plurals for English default. Should use KNOWN_UNITS...
71             my %locale_cache = (en => +{map { $_ => $_ } map { ($_, $_ . 's') } qw(second minute hour day)});
72              
73             sub _load_translation {
74 24     24   26 my ($self, $req_locale) = @_;
75              
76             # If locale already cached do not load
77 24         26 my $cached = exists $locale_cache{$req_locale};
78              
79 24 100       33 if (!$cached) {
80 2         10 foreach my $locale_module ($self->translation_classes) {
81 30         7312 my $load_locale = (split('::', $locale_module))[-1];
82 30 50       51 next if (exists $locale_cache{$load_locale}); # Got this one already.
83 30 50       256 next unless ($locale_module->can('translation')); # Chocolate in our peanut butter.
84 30         76 $locale_cache{$load_locale} = $locale_module->translation();
85 30 100       55 $cached = 1 if ($load_locale eq $req_locale); # Good news, everyone!
86             }
87             }
88              
89 24 50       49 return ($cached) ? $locale_cache{$req_locale} : $locale_cache{en}; # English default.
90             }
91              
92             sub as_string {
93 24     24 1 3170 my ($self, $precision) = @_;
94              
95 24         43 my $translation = $self->_load_translation($self->locale);
96              
97 24         25 return join(' ', map { join ' ', ($_->{'value'}, $translation->{$_->{'unit'}}) } @{$self->duration_array($precision)});
  51         182  
  24         65  
98             }
99              
100             =head2 new
101              
102             Object constructor
103              
104             =cut
105              
106             sub new {
107 10     10 1 78440 my $class = shift;
108 10 50       42 my %params_ref = ref( $_[0] ) ? %{ $_[0] } : @_;
  0         0  
109              
110 10         13 my $interval = $params_ref{'interval'};
111              
112 10         13 my $whatsit = ref($interval);
113              
114 10 50       44 if ($whatsit eq __PACKAGE__) {
115 0         0 $interval = $interval->seconds;
116             }
117              
118 10 100       22 if (not defined $interval) {
119 1         22 confess "Missing required arguments";
120             }
121              
122 9         36 my $self = $class->SUPER::new( interval => $interval );
123              
124             # Set default locale as english
125 9         20 $self->{'locale'} = 'en';
126 9 50       17 if ( exists $params_ref{'locale'} ) {
127 9         13 $self->{'locale'} = lc $params_ref{'locale'};
128             }
129              
130 9         13 my $obj = bless $self, $class;
131 9         17 return $obj;
132             }
133              
134             =head1 AUTHOR
135              
136             Binary.com, C<< >>
137              
138             =head1 BUGS
139              
140             Please report any bugs or feature requests to C, or through
141             the web interface at L. I will be notified, and then you'll
142             automatically be notified of progress on your bug as I make changes.
143              
144             =head1 SUPPORT
145              
146             You can find documentation for this module with the perldoc command.
147              
148             perldoc Time::Duration::Concise::Localize
149              
150              
151             You can also look for information at:
152              
153             =over 4
154              
155             =item * RT: CPAN's request tracker (report bugs here)
156              
157             L
158              
159             =item * AnnoCPAN: Annotated CPAN documentation
160              
161             L
162              
163             =item * CPAN Ratings
164              
165             L
166              
167             =item * Search CPAN
168              
169             L
170              
171             =back
172              
173              
174             =head1 LICENSE AND COPYRIGHT
175              
176             Copyright 2014 Binary.com.
177              
178             This program is free software; you can redistribute it and/or modify it
179             under the terms of the the Artistic License (2.0). You may obtain a
180             copy of the full license at:
181              
182             L
183              
184             Any use, modification, and distribution of the Standard or Modified
185             Versions is governed by this Artistic License. By using, modifying or
186             distributing the Package, you accept this license. Do not use, modify,
187             or distribute the Package, if you do not accept this license.
188              
189             If your Modified Version has been derived from a Modified Version made
190             by someone other than you, you are nevertheless required to ensure that
191             your Modified Version complies with the requirements of this license.
192              
193             This license does not grant you the right to use any trademark, service
194             mark, tradename, or logo of the Copyright Holder.
195              
196             This license includes the non-exclusive, worldwide, free-of-charge
197             patent license to make, have made, use, offer to sell, sell, import and
198             otherwise transfer the Package with respect to any patent claims
199             licensable by the Copyright Holder that are necessarily infringed by the
200             Package. If you institute patent litigation (including a cross-claim or
201             counterclaim) against any party alleging that the Package constitutes
202             direct or contributory patent infringement, then this Artistic License
203             to you shall terminate on the date that such litigation is filed.
204              
205             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
206             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
207             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
208             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
209             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
210             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
211             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
212             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
213              
214              
215             =cut
216              
217             1; # End of Time::Duration::Concise::Localize