HTML-CalendarMonth

 view release on metacpan or  search on metacpan

lib/HTML/CalendarMonth/Locale.pm  view on Meta::CPAN

use warnings;
use Carp;

use DateTime::Locale 0.45;

sub _locale_version { $DateTime::Locale::VERSION }

my($CODE_METHOD, $CODES_METHOD);
if (_locale_version() > 0.92) {
  $CODE_METHOD  = "code";
  $CODES_METHOD = "codes";
}
else {
  $CODE_METHOD  = "id";
  $CODES_METHOD = "ids";
}

my %Register;

sub new {
  my $class = shift;
  my $self = {};
  bless $self, $class;
  my %parms = @_;
  # id is for backwards compatibility
  my $code = $parms{code} || $parms{id}
    or croak "Locale code required (eg 'en-US')\n";
  $self->{full_days}   = defined $parms{full_days}   ? $parms{full_days}   : 0;
  $self->{full_months} = defined $parms{full_months} ? $parms{full_months} : 1;
  # returned code might be different from given code
  unless ($Register{$code}) {
    my $dtl = $self->locale->load($code)
      or croak "Problem loading locale '$code'";
    $Register{$code} = $Register{$dtl->$CODE_METHOD} = { loc => $dtl };
  }
  $self->{code} = $Register{$code}{loc}->$CODE_METHOD;
  $self;
}

sub locale { 'DateTime::Locale' }

sub loc { $Register{shift->code}{loc} }

sub locales { shift->locale->$CODES_METHOD }

sub code { shift->{code} }
*id = *code;

sub full_days   { shift->{full_days}   }
sub full_months { shift->{full_months} }

sub first_day_of_week { shift->loc->first_day_of_week % 7 }

sub days {
  my $self = shift;
  my $code = $self->code;
  unless ($Register{$code}{days}) {
    my $method = $self->full_days ? 'day_stand_alone_wide'
                                  : 'day_stand_alone_abbreviated';
    # adjust to H::CM standard expectation, 1st day Sun
    # Sunday is first, regardless of what the calendar considers to be
    # the first day of the week
    my @days  = @{$self->loc->$method};
    unshift(@days, pop @days);
    $Register{$code}{days} = \@days;
  }
  wantarray ? @{$Register{$code}{days}} : $Register{$code}{days};
}

sub narrow_days {
  my $self = shift;
  my $code = $self->code;
  unless ($Register{$code}{narrow_days}) {
    # Sunday is first, regardless of what the calendar considers to be
    # the first day of the week
    my @days = @{ $self->loc->day_stand_alone_narrow };
    unshift(@days, pop @days);
    $Register{$code}{narrow_days} = \@days;
  }
  wantarray ? @{$Register{$code}{narrow_days}}
            :   $Register{$code}{narrow_days};
}

sub months {
  my $self = shift;
  my $code = $self->code;
  unless ($Register{$code}{months}) {
    my $method = $self->full_months > 0 ? 'month_stand_alone_wide'
                                        : 'month_stand_alone_abbreviated';
    $Register{$code}{months} = [@{$self->loc->$method}];
  }
  wantarray ? @{$Register{$code}{months}} : $Register{$code}{months};
}

sub narrow_months {
  my $self = shift;
  my $code = $self->code;
  $Register{$code}{narrow_months}
    ||= [@{$self->loc->month_stand_alone_narrow}];
  wantarray ? @{$Register{$code}{narrow_months}}
            :   $Register{$code}{narrow_months};
}

sub days_minmatch {
  my $self = shift;
  $Register{$self->code}{days_mm}
    ||= $self->lc_minmatch_hash($self->days);
}
*minmatch = \&days_minmatch;

sub _days_minmatch_pattern {
  my $dmm = shift->days_minmatch;
  join('|', sort keys %$dmm);
}
*minmatch_pattern = \&_days_minmatch_pattern;

sub months_minmatch {
  my $self = shift;
  $Register{$self->code}{months_mm}
    ||= $self->lc_minmatch_hash($self->months);
}

sub _months_minmatch_pattern {
  my $mmm = shift->months_minmatch;
  join('|', sort keys %$mmm);
}

sub daynums {
  my $self = shift;
  my $code = $self->code;
  unless ($Register{$code}{daynum}) {
    my %daynum;
    my $days = $self->days;
    $daynum{$days->[$_]} = $_ foreach 0 .. $#$days;

lib/HTML/CalendarMonth/Locale.pm  view on Meta::CPAN

  # label in the list
  my $whatever = shift;
  my @labels = @_;
  my $cc = 1;
  my %minmatch;
  while (@labels) {
    my %scratch;
    foreach my $i (0 .. $#labels) {
      my $str = $labels[$i];
      my $chrs = substr($str, 0, $cc);
      $scratch{$chrs} ||= [];
      push(@{$scratch{$chrs}}, $i);
    }
    my @keep_i;
    foreach (keys %scratch) {
      if (@{$scratch{$_}} == 1) {
        $minmatch{$_} = $labels[$scratch{$_}[0]];
      }
      else {
        push(@keep_i, @{$scratch{$_}});
      }
    }
    @labels = @labels[@keep_i];
    ++$cc;
  }
  \%minmatch;
}

1;

__END__

=head1 NAME

HTML::CalendarMonth::Locale - Front end class for DateTime::Locale

=head1 SYNOPSIS

  use HTML::CalendarMonth::Locale;

  my $loc = HTML::CalendarMonth::Locale->new( code => 'en-US' );

  # list of days of the week for locale
  my @days = $loc->days;

  # list of months of the year for locale
  my @months = $loc->months;

  # the name of the current locale, as supplied the code parameter to
  # new()
  my $locale_name = $loc->code;

  # the actual DateTime::Locale object
  my $loc = $loc->loc;

  1;

=head1 DESCRIPTION

HTML::CalendarMonth utilizes the powerful locale capabilities of
DateTime::Locale for rendering its calendars. The default locale is
'en-US' but many others are available. To see this list, invoke the
class method HTML::CalendarMonth::Locale->locales() which in turn
invokes DateTime::Locale::codes().

This module is mostly intended for internal usage within
HTML::CalendarMonth, but some of its functionality may be of use for
developers:

=head1 METHODS

=over

=item new()

Constructor. Takes the following parameters:

=over

=item code

Locale code, e.g. 'en-US'.

=item full_days

Specifies whether full day names or their abbreviations are desired.
Default 0, use abbreviated days.

=item full_months

Specifies whether full month names or their abbreviations are desired.
Default 1, use full months.

=back

=item code()

Returns the locale code used during object construction.

=item locale()

Accessor method for the DateTime::Locale class, which in turn offers
several class methods of specific interest. See L<DateTime::Locale>.

=item locale_map()

Returns a hash of all available locales, mapping their code to their
full name.

=item loc()

Accessor method for the DateTime::Locale instance as specified by C<code>.
See L<DateTime::Locale>.

=item locales()

Lists all available locale codes. Equivalent to locale()->codes(), or
DateTime::Locale->codes().

=item days()

Returns a list of days of the week, Sunday first. These are the actual
unique day strings used for rendering calendars, so depending on which
attributes were provided to C<new()>, this list will either be
abbreviations or full names. The default uses abbreviated day names.
Returns a list in list context or an array ref in scalar context.

=item narrow_days()

Returns a list of short day abbreviations, beginning with Sunday. The
narrow abbreviations are not guaranteed to be unique (i.e. 'S' for both
Sat and Sun).

=item days_minmatch()

Provides a hash reference containing minimal case-insensitive match
strings for each day of the week, e.g., 'sa' for Saturday, 'm' for
Monday, etc.

=item months()

Returns a list of months of the year, beginning with January. Depending
on which attributes were provided to C<new()>, this list will either be
full names or abbreviations. The default uses full names. Returns a list
in list context or an array ref in scalar context.

=item narrow_months()

Returns a list of short month abbreviations, beginning with January. The
narrow abbreviations are not guaranteed to be unique.

=item months_minmatch()

Provides a hash reference containing minimal case-insensitive match
strings for each month of the year, e.g., 'n' for November, 'ja' for
January, 'jul' for July, 'jun' for June, etc.

=item daynums()

Provides a hash reference containing day of week indices for each fully
qualified day name as returned by days().

=item daynum($day)

Provides the day of week index for a particular day name.

=item dayname($day)

Provides the fully qualified day name for a given string or day index.

=item monthnums()

Provides a hash reference containing month of year indices for each
fully qualified month name as returned by months().

=item monthnum($month)

Provides the month of year index for a particular month name.

=item monthname($month)

Provides the month name for a given string or month index.



( run in 0.679 second using v1.01-cache-2.11-cpan-39bf76dae61 )