File Coverage

blib/lib/DateTime/Languages.pm
Criterion Covered Total %
statement 55 69 79.7
branch 8 16 50.0
condition n/a
subroutine 12 25 48.0
pod 14 15 93.3
total 89 125 71.2


line stmt bran cond sub pod time code
1             package DateTime::Languages;
2              
3              
4             BEGIN
5             {
6 1     1   7 use strict;
  1         2  
  1         28  
7 1     1   3 use warnings;
  1         1  
  1         35  
8 1     1   380 use Class::Factory::Util;
  1         556  
  1         5  
9 1     1   543 use Params::Validate qw( validate SCALAR );
  1         2520  
  1         69  
10 1     1   6 use vars qw ( %ISOMap $VERSION );
  1         1  
  1         152  
11              
12 1     1   3 $VERSION = "0.05";
13              
14 1         9 foreach my $set ( [ 'aa', 'aar' => 'Afar' ],
15             [ 'am', 'amh' => 'Amharic' ],
16             [ 'cz', 'ces', 'cze' => 'Czech' ],
17             [ 'de', 'deu', 'ger' => 'German' ],
18             [ 'de-at', 'deu-at', 'ger-at' => 'Austrian' ],
19             [ 'dk', 'dan' => 'Danish' ],
20             [ 'en', 'eng' => 'English' ],
21             [ 'es', 'esl', 'spa' => 'Spanish' ],
22             [ 'fr', 'fra', 'fre' => 'French' ],
23             [ 'x-drs', 'sil-drs' => 'Gedeo' ],
24             [ 'it', 'ita' => 'Italian' ],
25             [ 'nl', 'dut', 'nla' => 'Dutch' ],
26             [ 'no', 'nor' => 'Norwegian' ],
27             [ 'om', 'orm' => 'Oromo' ],
28             # not quite right, but better than failing
29             [ 'pt', 'por' => 'Brazilian' ],
30             [ 'pt-br', 'por-br' => 'Brazilian' ],
31             [ 'sid' => 'Sidama' ],
32             [ 'so', 'som' => 'Somali' ],
33             [ 'sv', 'sve', 'swe' => 'Swedish' ],
34             [ 'ti-et', 'tig-et' => 'TigrinyaEthiopian' ],
35             [ 'ti-er', 'tig-er' => 'TigrinyaEritrean' ],
36             )
37             {
38 21         23 my $module = pop @$set;
39 21         516 @ISOMap{ @$set } = ($module) x @$set;
40             }
41             }
42              
43             # print "ISOMap: $ISOMap{sid}\n";
44              
45             sub new
46             {
47 2     2 1 3 my $class = shift;
48 2         69 my %p = validate( @_,
49             { language => { type => SCALAR } },
50             );
51              
52 2         33 my $real_class = $class->load( $p{language} );
53              
54 2         5 my $obj = bless {}, $real_class;
55              
56 2         7 $obj->_init;
57              
58 2         8 return $obj;
59             }
60              
61 0     0 1 0 sub languages { $_[0]->subclasses }
62 0     0 1 0 sub iso_codes { keys %ISOMap }
63              
64             sub class_base
65             {
66 0     0 0 0 "DateTime::Languages";
67             }
68              
69              
70             sub load
71             {
72 2     2 1 3 my $class = shift;
73 2         24 my $lang = shift;
74              
75 2         4 my $real_lang;
76 2 50       15 if ( $lang =~ /^((?:x-)?\w\w\w?)(?:-\w\w\w?)?$/ )
77             {
78             $real_lang =
79             ( exists $ISOMap{$lang} ?
80             $ISOMap{$lang} :
81             $1 ?
82 2 0       6 $ISOMap{$1} :
    50          
83             undef
84             );
85              
86 2 50       6 die "Unsupported or invalid ISO language code, $lang"
87             unless defined $real_lang;
88             }
89             else
90             {
91 0         0 $real_lang = $lang;
92             }
93              
94 2         6 my $real_class = $class->class_base."::$real_lang";
95 1     1   455 eval "use $real_class";
  1     1   3  
  1         16  
  1         5  
  1         2  
  1         12  
  2         152  
96 2 50       6 die $@ if $@;
97              
98             # print "XLoading $real_class\n";
99 2         5 return $real_class;
100             }
101              
102             sub _init
103             {
104 2     2   13 my $self = shift;
105 2         4 my $class = ref $self;
106              
107 2         4 foreach my $key ( qw( day_names day_abbreviations month_names
108             month_abbreviations am_pm ordinal_suffixes
109             month_numbers day_numbers
110             )
111             )
112             {
113 16         23 my $var_name = join '', map { ucfirst } split /_/, $key;
  32         44  
114 16 100       26 $var_name = 'AMPM' if $var_name eq 'AmPm';
115              
116 1     1   6 no strict 'refs';
  1         1  
  1         238  
117 16 100       45 if ( $key =~ /numbers$/ )
118             {
119 4         6 $self->{$key} = \%{"$class\::$var_name"};
  4         15  
120             }
121             else
122             {
123 12         10 $self->{$key} = \@{"$class\::$var_name"};
  12         38  
124             }
125             }
126             }
127              
128 0     0 1   sub month_names { $_[0]->{month_names} }
129              
130 0     0 1   sub month_name { $_[0]->{month_names}[ $_[1]->month_0 ] }
131              
132 0     0 1   sub day_names { $_[0]->{day_names} }
133              
134 0     0 1   sub day_name { $_[0]->{day_names}[ $_[1]->day_of_week_0 ] }
135              
136 0     0 1   sub month_abbreviations { $_[0]->{month_abbreviations} }
137              
138 0     0 1   sub month_abbreviation { $_[0]->{month_abbreviations}[ $_[1]->month_0 ] }
139              
140 0     0 1   sub day_abbreviations { $_[0]->{day_abbreviations} }
141              
142 0     0 1   sub day_abbreviation { $_[0]->{day_abbreviations}[ $_[1]->day_of_week_0 ] }
143              
144 0     0 1   sub am_pm_list { $_[0]->{am_pm} }
145              
146 0 0   0 1   sub am_pm { $_[0]->{am_pm}[ $_[1]->hour < 12 ? 0 : 1 ] }
147              
148             #sub preferred_datetime_format { '%m/%d/%y %H:%M:%S' }
149             #sub preferred_date_format { '%m/%d/%y' }
150             #sub preferred_time_format { '%H:%M:%S' }
151              
152             1;
153              
154             __END__
155              
156             =head1 NAME
157              
158             DateTime::Languages - base class for DateTime.pm-related language localization
159              
160             =head1 SYNOPSIS
161              
162             package DateTime::Languages::Gibberish;
163              
164             use base qw(DateTime::Languages);
165              
166             =head1 DESCRIPTION
167              
168             This class provides most of the methods needed to implement language
169             localization for DateTime.pm. A subclass of this language simply
170             provides a set of data structures containing things like day and
171             months names.
172              
173             This module is a factory for language subclasses, and can load a class
174             either based on the language portion of its name, such as "English",
175             or based on its ISO code, such as "en" or "eng".
176              
177             =head1 USAGE
178              
179             This module provides the following methods:
180              
181             =over 4
182              
183             =item * new( language => $language )
184              
185             This method loads the requested language and returns an object of the
186             appropriate class. The "language" parameter may be the name of the
187             language subclass to be used, such as "English", as returned by the
188             C<languages()> method. It can also be an ISO 639 two-letter language
189             code. The language code may include an ISO 3166 two-letter country
190             after a dash, so things like "en" or "en-us" are both legal. If a
191             country code is given, then the most specific match is used. For
192             example, if "en-au" (English, Australian) is given, then the nearest
193             match will be "en", which will be used instead.
194              
195             =item * load( $language )
196              
197             This tells the module to load the specified language without creating
198             an object. The language given can be anything accepted by the
199             C<new()> method.
200              
201             =item * languages
202              
203             Returns a list of supported language names.
204              
205             =item * iso_codes
206              
207             Returns a list of supported ISO language codes. See the C<new()>
208             method documentation for details.
209              
210             =back
211              
212             =head1 SUBCLASSING
213              
214             People who want to add support for new languages may be interested in
215             subclassing this module.
216              
217             The simplest way to do this is to simply declare your new module,
218             let's call it C<DateTime::Languages::Inhumi>, a subclass of
219             C<DateTime::Languages>, and to define a set of global variables in your
220             namespace.
221              
222             These globals are:
223              
224             =over 4
225              
226             =item * @DayNames
227              
228             The names of each day, starting with Monday.
229              
230             =item * @DayAbbreviations
231              
232             Abbreviated names for each day.
233              
234             =item * @MonthNames
235              
236             The names of each month, starting with January.
237              
238             =item * @MonthAbbreviations
239              
240             Abbreviated names for each month.
241              
242             =item * @AMPM
243              
244             The terms used for AM and PM in the language you are implementing.
245              
246             =back
247              
248             The C<DateTime::Languages> module implements methods that use these
249             globals as needed. If you need to implement more complex algorithms,
250             you can override the following methods:
251              
252             =over 4
253              
254             =item * month_names
255              
256             Returns a list of month names.
257              
258             =item * month_name( $dt )
259              
260             Given a C<DateTime> object, this method should return the correct
261             month name.
262              
263             =item * month_abbreviations
264              
265             Returns a list of month abbreviations.
266              
267             =item * month_abbreviation( $dt )
268              
269             Given a C<DateTime> object, this method should return the correct
270             month abbreviation.
271              
272             =item * day_names
273              
274             Returns a list of day names.
275              
276             =item * day_name( $dt )
277              
278             Given a C<DateTime> object, this method should return the correct day
279             name.
280              
281             =item * day_abbreviations
282              
283             Returns a list of day abbreviations.
284              
285             =item * day_abbreviation( $dt )
286              
287             Given a C<DateTime> object, this method should return the correct day
288             abbreviation.
289              
290             =item * am_pm_list
291              
292             Returns a list of the AM/PM texts. First item should be the AM, the
293             second should be the PM.
294              
295             =item * am_pm( $dt )
296              
297             Given a C<DateTime> object, returns the correct AM or PM abbreviation.
298              
299             =back
300              
301             =head1 SUPPORT
302              
303             Support for this module is provided via the datetime@perl.org email
304             list. See http://lists.perl.org/ for more details.
305              
306             =head1 AUTHOR
307              
308             Dave Rolsky <autarch@urth.org>
309              
310             However, please see the CREDITS file for more details on who I really
311             stole all the code from.
312              
313             =head1 COPYRIGHT
314              
315             Copyright (c) 2003 David Rolsky. All rights reserved. This program
316             is free software; you can redistribute it and/or modify it under the
317             same terms as Perl itself.
318              
319             Portions of the code in this distribution are derived from other
320             works. Please see the CREDITS file for more details.
321              
322             The full text of the license can be found in the LICENSE file included
323             with this module.
324              
325             =head1 SEE ALSO
326              
327             datetime@perl.org mailing list
328              
329             http://datetime.perl.org/
330              
331             =cut