| 1 | package AI::MicroStructure::Locale; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use AI::MicroStructure (); # do not export metaname 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 | our $VERSION = '1.000'; | ||
| 11 | |||
| 12 | sub init { | ||
| 13 | # alias the older package variable %Locale to %MultiList | ||
| 14 | no strict 'refs'; | ||
| 15 | *{"$_[0]::Locale"} = \%{"$_[0]::MultiList"}; | ||
| 16 | ${"$_[0]::Separator"} = '_'; | ||
| 17 | |||
| 18 | # call the parent class init code | ||
| 19 | goto &AI::MicroStructure::MultiList::init; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | |||
| 25 | no strict 'refs'; | ||
| 26 | my $self = bless { @_, cache => [] }, $class; | ||
| 27 | |||
| 28 | # compute some defaults | ||
| 29 | if( ! exists $self->{category} ) { | ||
| 30 | $self->{category} = | ||
| 31 | exists $self->{lang} | ||
| 32 | ? $self->{lang} | ||
| 33 | : $ENV{LANGUAGE} || $ENV{LANG} || ''; | ||
| 34 | if( !$self->{category} && $^O eq 'MSWin32' ) { | ||
| 35 | eval { require Win32::Locale; }; | ||
| 36 | $self->{category} = Win32::Locale::get_language() unless $@; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | my $cat = $self->{category}; | ||
| 41 | |||
| 42 | # support for territories | ||
| 43 | if ( $cat && $cat ne ':all' ) { | ||
| 44 | ($cat) = $cat =~ /^([-A-Za-z_]+)/; | ||
| 45 | $cat = lc( $cat || '' ); | ||
| 46 | 1 while $cat | ||
| 47 | && !exists ${"$class\::MultiList"}{$cat} | ||
| 48 | && $cat =~ s/_?[^_]*$//; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | # fall back to last resort | ||
| 53 | $self->{category} = $cat || ${"$class\::Default"}; | ||
| 54 | $self->_compute_base(); | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub categories { | ||
| 59 | my $class = shift; | ||
| 60 | $class = ref $class if ref $class; | ||
| 61 | |||
| 62 | no strict 'refs'; | ||
| 63 | my @p = keys %{"$class\::MultiList"}; | ||
| 64 | print @p; | ||
| 65 | return \@p; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub has_category { | ||
| 69 | my ($class, $category) = @_; | ||
| 70 | $class = ref $class if ref $class; | ||
| 71 | |||
| 72 | no strict 'refs'; | ||
| 73 | return exists ${"$class\::MultiList"}{$category}; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ | ||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | AI::MicroStructure::Locale - Base class for multilingual structures | ||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | package AI::MicroStructure::digits; | ||
| 90 | use AI::MicroStructure::Locale; | ||
| 91 | our @ISA = ( AI::MicroStructure::Locale ); | ||
| 92 | __PACKAGE__->init(); | ||
| 93 | 1; | ||
| 94 | |||
| 95 | =head1 NAME | ||
| 96 | |||
| 97 | AI::MicroStructure::digits - The numbers structure | ||
| 98 | |||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | You can count on this module. Almost. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | __DATA__ | ||
| 106 | # default | ||
| 107 | en | ||
| 108 | # names en | ||
| 109 | zero one two three four five six seven eight nine | ||
| 110 | # names fr | ||
| 111 | zero un deux trois quatre cinq six sept huit neuf | ||
| 112 | # names it | ||
| 113 | zero uno due tre quattro cinque sei sette otto nove | ||
| 114 | # names yi | ||
| 115 | nul eyn tsvey dray fir finf zeks zibn akht nayn | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | C<AI::MicroStructure::Locale> is the base class for all structures that are | ||
| 120 | meant to return a random excerpt from a predefined list I<that depends | ||
| 121 | on the language>. | ||
| 122 | |||
| 123 | The language is selected at construction time from: | ||
| 124 | |||
| 125 | =over 4 | ||
| 126 | |||
| 127 | =item 1. | ||
| 128 | |||
| 129 | the given C<lang> or C<category> parameter, | ||
| 130 | |||
| 131 | =item 2. | ||
| 132 | |||
| 133 | the current locale, as given by the environment variables C<LANGUAGE>, | ||
| 134 | C<LANG> or (under Win32) Win32::Locale. | ||
| 135 | |||
| 136 | =item 3. | ||
| 137 | |||
| 138 | the default language for the selected structure. | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | The language codes should conform to the RFC 3066 and ISO 639 standard. | ||
| 143 | |||
| 144 | =head1 METHODS | ||
| 145 | |||
| 146 | AI::MicroStructure::Locale offers several methods, so that the subclasses | ||
| 147 | are easy to write (see full example in L<SYNOPSIS>): | ||
| 148 | |||
| 149 | =over 4 | ||
| 150 | |||
| 151 | =item new( lang => $lang ) | ||
| 152 | |||
| 153 | =item new( category => $lang ) | ||
| 154 | |||
| 155 | The constructor of a single instance. An instance will not repeat items | ||
| 156 | until the list is exhausted. | ||
| 157 | |||
| 158 | The C<lang> or C<category> parameter(both are synonymous) should be | ||
| 159 | expressed as a locale category. If none of those parameters is given | ||
| 160 | AI::MicroStructure::Locale will try to find the user locale (with the | ||
| 161 | help of environment variables C<LANGUAGE>, C<LANG> and the module | ||
| 162 | C<Win32::Locale>). | ||
| 163 | |||
| 164 | POSIX locales are defined as C<language[_territory][.codeset][@modifier]>. | ||
| 165 | If the specific territory is not supported, C<AI::MicroStructure::Locale> | ||
| 166 | will use the language, and if the language isn't supported either, | ||
| 167 | the default is used. | ||
| 168 | |||
| 169 | =item init() | ||
| 170 | |||
| 171 | init() must be called when the subclass is loaded, so as to read the | ||
| 172 | __DATA__ section and fully initialise it. | ||
| 173 | |||
| 174 | =item name( $count ) | ||
| 175 | |||
| 176 | Return $count names (default: C<1>). | ||
| 177 | |||
| 178 | Using C<0> will return the whole list in list context, and the size of the | ||
| 179 | list in scalar context (according to the C<lang> parameter passed to the | ||
| 180 | constructor). | ||
| 181 | |||
| 182 | =item lang() | ||
| 183 | |||
| 184 | =item category() | ||
| 185 | |||
| 186 | Return the selected language for this instance. | ||
| 187 | |||
| 188 | =item languages() | ||
| 189 | |||
| 190 | =item categories() | ||
| 191 | |||
| 192 | Return the languages supported by the structure. | ||
| 193 | |||
| 194 | =item structure() | ||
| 195 | |||
| 196 | Return the structure name. | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =head1 SEE ALSO | ||
| 201 | |||
| 202 | I<Codes for the Representation of Names of Languages>, at | ||
| 203 | L<http://www.loc.gov/standards/iso639-2/langcodes.html>. | ||
| 204 | |||
| 205 | RFC 3066, I<Tags for the Identification of Languages>, at | ||
| 206 | L<http://www.ietf.org/rfc/rfc3066.txt>. | ||
| 207 | |||
| 208 | L<AI::MicroStructure>, L<Acme::MetaSyntactic::MultiList>. | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | Philippe 'BooK' Bruhat, C<< <book@cpan.org> >> | ||
| 213 | |||
| 214 | =head1 COPYRIGHT & LICENSE | ||
| 215 | |||
| 216 | Copyright 2005-2006 Philippe 'BooK' Bruhat, All Rights Reserved. | ||
| 217 | |||
| 218 | This program is free software; you can redistribute it and/or modify it | ||
| 219 | under the same terms as Perl itself. | ||
| 220 | |||
| 221 | =cut |