1 package HTML::CalendarMonth::Locale;
3 # Front end class around DateTime::Locale. In addition to providing
4 # access to the DT::Locale class and locale-specific instance, this
5 # class prepares some other hashes and lookups utilized by
13 use vars qw($VERSION);
23 my $id = $parms{id} or croak "Locale id required (eg 'en_US')\n";
25 $self->{full_days} = exists $parms{full_days} ? $parms{full_days} : 0;
26 $self->{full_months} = exists $parms{full_months} ? $parms{full_months} : 1;
27 unless ($Register{$id}) {
28 $Register{$id} = $self->locale->load($id)
29 or croak "Problem loading locale '$id'\n";
35 sub locale { 'DateTime::Locale' }
37 sub loc { $Register{shift->id} }
39 sub locales { shift->locale->ids }
41 sub id { shift->{id} }
42 sub full_days { shift->{full_days} }
43 sub full_months { shift->{full_months} }
48 unless ($Register{$id}{days}) {
49 # we've always used Sunday as first day...
50 my $method = $self->full_days ? 'day_names' : 'day_abbreviations';
51 my @days = @{$self->loc->$method};
52 unshift(@days, pop @days);
53 $Register{$id}{days} = \@days;
55 wantarray ? @{$Register{$id}{days}} : $Register{$id}{days};
61 unless ($Register{$id}{months}) {
62 my $method = $self->full_months ? 'month_names' : 'month_abbreviations';
63 $Register{$id}{months} = [@{$self->loc->$method}];
65 wantarray ? @{$Register{$id}{months}} : $Register{$id}{months};
71 unless ($Register{$id}{minmatch}) {
72 $Register{$id}{days_minmatch} =
73 $self->minmatch_hash(@{$self->days});
75 $Register{$id}{days_minmatch};
81 unless ($Register{$id}{daynum}) {
83 my $days = $self->days;
84 $daynum{$days->[$_]} = $_ foreach 0 .. $#$days;
85 $Register{$id}{daynum} = \%daynum;
87 $Register{$id}{daynum};
92 defined $day or croak "day of week label required\n";
93 my $days = $self->days;
94 $days->{$day} or croak "Failed daynum lookup for '$day'\n";
100 unless ($Register{$id}{monthnum}) {
102 my $months = $self->months;
103 $monthnum{$months->[$_]} = $_ foreach 0 .. $#$months;
104 $Register{$id}{monthnum} = \%monthnum;
106 $Register{$id}{monthnum};
110 my($self, $month) = @_;
111 defined $month or croak "month label required\n";
112 my $monthnums = $self->monthnums;
113 $monthnums->{$month} or croak "Failed monthnum lookup for '$month'\n";
121 foreach my $id ($self->locales) {
122 $map{$id} = $self->locale->load($id)->name;
124 wantarray ? %map : \%map;
130 # given a list, provide a reverse lookup of minimal values for each
132 my $whatever = shift;
138 foreach my $i (0 .. $#labels) {
139 my $str = $labels[$i];
140 my $chrs = substr($str, 0, $cc);
141 $scratch{$chrs} ||= [];
142 push(@{$scratch{$chrs}}, $i);
145 foreach (keys %scratch) {
146 if (@{$scratch{$_}} == 1) {
147 $minmatch{$_} = $labels[$scratch{$_}[0]];
150 push(@keep_i, @{$scratch{$_}});
153 @labels = @labels[@keep_i];
159 sub minmatch_pattern { join('|',keys %{shift->minmatch}) }
167 HTML::CalendarMonth::Locale - Front end class for DateTime::Locale
171 use HTML::CalendarMonth::Locale;
173 my $loc = HTML::CalendarMonth::Locale->new( id => 'en_US' );
175 # list of days of the week for locale
176 my @days = $loc->days;
178 # list of months of the year for locale
179 my @months = $loc->months;
181 # the name of the current locale, as supplied the id parameter to
183 my $locale_name = $loc->id;
185 # the actual DateTime::Locale object
192 HTML::CalendarMonth utilizes the powerful locale capabilities of
193 DateTime::Locale for rendering its calendars. The default locale is
194 'en_US' but many others are available. To see this list, invoke the
195 class method HTML::CalendarMonth::Locale->locales() which in turn
196 invokes DateTime::Locale::ids().
198 This module is mostly intended for internal usage within
199 HTML::CalendarMonth, but some of its functionality may be of use for
206 Constructor. Takes the following parameters:
212 Locale id, e.g. 'en_US'.
216 Specifies whether full day names or their abbreviations are desired.
217 Default 0, use abbreviated days.
221 Specifies whether full month names or their abbreviations are desired.
222 Default 1, use full months.
228 Returns the locale id used during object construction.
232 Accessor method for the DateTime::Locale class, which in turn offers
233 several class methods of specific interest. See L<DateTime::Locale>.
237 Accessor method for the DateTime::Locale instance as specified by C<id>.
238 See L<DateTime::Locale>.
242 Lists all available locale ids. Equivalent to locale()->ids(), or
243 DateTime::Locale->ids().
247 Returns a list of days of the week, Sunday first. These are the actual
248 days used for rendering the calendars, so depending on which attributes
249 were provided to C<new()>, this list will either be abbreviations or
250 full names. The default uses abbreviated day names. Returns a list in
251 list context or an array ref in scalar context.
255 Returns a list of months of the year, beginning with January. Depending
256 on which attributes were provided to C<new()>, this list will either be
257 full names or abbreviations. The default uses full names. Returns a list
258 in list context or an array ref in scalar context.
262 Provides a hash reference containing minimal match strings for each
263 month of the year, e.g., 'N' for November, 'Ja' for January, 'Jul' for
264 July, 'Jun' for June, etc.
268 Provides a hash reference containing day of week numbers for each day
273 Provides the day of week number for a particular day name.
277 Provides a hash reference containing month of year numbers for each
280 =item monthnum($month)
282 Provides the month of year number for a particular month name.
284 =item minmatch_hash(@list)
286 This is the method used to generate the minimal match hash referenced
287 above. Given an arbitrary list, a hash reference will be returned with
288 minimal match strings as keys and full names as values.
292 Matthew P. Sisk, E<lt>F<sisk@mojotoad.com>E<gt>
296 Copyright (c) 2005 Matthew P. Sisk. All rights reserved. All wrongs
297 revenged. This program is free software; you can redistribute it and/or
298 modify it under the same terms as Perl itself.
302 HTML::CalendarMonth(3), DateTime::Locale(3)