| 1 | package AI::MicroStructure::Locale; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use AI::MicroStructure (); # do not export microname and friends | ||
| 5 | use AI::MicroStructure::MultiList; | ||
| 6 | use List::Util qw( shuffle ); | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | our @ISA = qw( AI::MicroStructure::MultiList ); | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | # alias the older package variable %Locale to %MultiList | ||
| 13 | no strict 'refs'; | ||
| 14 | *{"$_[0]::Locale"} = \%{"$_[0]::MultiList"}; | ||
| 15 | ${"$_[0]::Separator"} = '_'; | ||
| 16 | |||
| 17 | # call the parent class init code | ||
| 18 | goto &AI::MicroStructure::MultiList::init; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | |||
| 24 | no strict 'refs'; | ||
| 25 | my $self = bless { @_, cache => [] }, $class; | ||
| 26 | |||
| 27 | # compute some defaults | ||
| 28 | if( ! exists $self->{category} ) { | ||
| 29 | $self->{category} = | ||
| 30 | exists $self->{lang} | ||
| 31 | ? $self->{lang} | ||
| 32 | : $ENV{LANGUAGE} || $ENV{LANG} || ''; | ||
| 33 | if( !$self->{category} && $^O eq 'MSWin32' ) { | ||
| 34 | #eval { require Win32::Locale; }; | ||
| 35 | #$self->{category} = Win32::Locale::get_language() unless $@; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | my $cat = $self->{category}; | ||
| 40 | |||
| 41 | # support for territories | ||
| 42 | if ( $cat && $cat ne ':all' ) { | ||
| 43 | ($cat) = $cat =~ /^([-A-Za-z_]+)/; | ||
| 44 | $cat = lc( $cat || '' ); | ||
| 45 | 1 while $cat | ||
| 46 | && !exists ${"$class\::MultiList"}{$cat} | ||
| 47 | && $cat =~ s/_?[^_]*$//; | ||
| 48 | } | ||
| 49 | |||
| 50 | # fall back to last resort | ||
| 51 | $self->{category} = $cat || ${"$class\::Default"}; | ||
| 52 | $self->_compute_base(); | ||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | *lang = \&AI::MicroStructure::MultiList::category; | ||
| 57 | *languages = \&AI::MicroStructure::MultiList::categories; | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ | ||
| 62 |