This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add default safe guesses for static_inline to plan9 files.
[perl5.git] / cpan / Unicode-Collate / Collate / Locale.pm
CommitLineData
00e00351
CBW
1package Unicode::Collate::Locale;
2
3use strict;
4use Carp;
5use base qw(Unicode::Collate);
6
f1a7422f 7our $VERSION = '0.59';
00e00351
CBW
8
9use File::Spec;
10
11(my $ModPath = $INC{'Unicode/Collate/Locale.pm'}) =~ s/\.pm$//;
12my $KeyPath = File::Spec->catfile('allkeys.txt');
13my $PL_EXT = '.pl';
14
64dc7822 15my %LocaleFile = map { ($_, $_) } qw(
f1a7422f
CBW
16 af az ca cs cy da eo es et fi fil fo fr ha haw
17 is kl lt lv mt nn pl ro sk sl sv sw tr wo yo
64dc7822 18);
456a1446
CBW
19 $LocaleFile{'default'} = '';
20 $LocaleFile{'es__traditional'} = 'es_trad';
21 $LocaleFile{'nb'} = 'nn';
00e00351
CBW
22
23sub _locale {
24 my $locale = shift;
25 if ($locale) {
26 $locale = lc $locale;
27 $locale =~ tr/\-\ \./_/;
456a1446 28 $locale =~ s/_trad\z/_traditional/;
00e00351
CBW
29 $LocaleFile{$locale} and return $locale;
30
31 my ($l,$t,$v) = split(/_/, $locale.'__');
456a1446 32 for my $loc ("${l}_${t}_$v", "${l}_$t", "${l}__$v", "${l}__$t", $l) {
00e00351
CBW
33 $LocaleFile{$loc} and return $loc;
34 }
35 }
36 return 'default';
37}
38
39sub getlocale {
40 return shift->{accepted_locale};
41}
42
f1a7422f 43sub _fetchpl {
6484f676
CBW
44 my $accepted = shift;
45 my $f = $LocaleFile{$accepted};
46 return if !$f;
47 $f .= $PL_EXT;
48 my $path = File::Spec->catfile($ModPath, $f);
49 my $h = do $path;
50 croak "Unicode/Collate/Locale/$f can't be found" if !$h;
51 return $h;
52}
53
00e00351
CBW
54sub new {
55 my $class = shift;
56 my %hash = @_;
00e00351
CBW
57 $hash{accepted_locale} = _locale($hash{locale});
58
64dc7822
CBW
59 if (exists $hash{table}) {
60 croak "your table can't be used with Unicode::Collate::Locale";
61 }
6484f676 62 $hash{table} = $KeyPath;
00e00351 63
f1a7422f 64 my $href = _fetchpl($hash{accepted_locale});
00e00351
CBW
65 while (my($k,$v) = each %$href) {
66 if (exists $hash{$k}) {
67 croak "$k is reserved by $hash{locale}, can't be overwritten";
68 }
69 $hash{$k} = $v;
70 }
71 return $class->SUPER::new(%hash);
72}
73
741;
75__END__
76
77=head1 NAME
78
79Unicode::Collate::Locale - Linguistic tailoring for DUCET via Unicode::Collate
80
81=head1 SYNOPSIS
82
83 use Unicode::Collate::Locale;
84
85 $Collator = Unicode::Collate::Locale->
86 new(locale => $locale_name, %tailoring);
87
88 @sorted = $Collator->sort(@not_sorted);
89
90=head1 DESCRIPTION
91
92This module provides linguistic tailoring for it
93taking advantage of C<Unicode::Collate>.
94
95=head2 Constructor
96
97The C<new> method returns a collator object.
98
99A parameter list for the constructor is a hash, which can include
100a special key C<'locale'> and its value (case-insensitive) standing
101for a two-letter language code (ISO-639) like C<'en'> for English.
102For example, C<Unicode::Collate::Locale-E<gt>new(locale =E<gt> 'FR')>
103returns a collator tailored for French.
104
105C<$locale_name> may be suffixed with a territory(country)
106code or a variant code, which are separated with C<'_'>.
107E.g. C<en_US> for English in USA,
108C<es_ES_traditional> for Spanish in Spain (Traditional),
109
110If C<$localename> is not defined,
111fallback is selected in the following order:
112
64dc7822
CBW
113 1. language_territory_variant
114 2. language_territory
115 3. language__variant
116 4. language
117 5. default
00e00351
CBW
118
119Tailoring tags provided by C<Unicode::Collate> are allowed
120as long as they are not used for C<'locale'> support.
121Esp. the C<table> tag is always untailorable
122since it is reserved for DUCET.
123
124E.g. a collator for French, which ignores diacritics and case difference
125(i.e. level 1), with reversed case ordering and no normalization.
126
127 Unicode::Collate::Locale->new(
64dc7822
CBW
128 level => 1,
129 locale => 'fr',
130 upper_before_lower => 1,
131 normalization => undef
00e00351
CBW
132 )
133
134=head2 Methods
135
136C<Unicode::Collate::Locale> is a subclass of C<Unicode::Collate>
137and methods other than C<new> are inherited from C<Unicode::Collate>.
138
139Here is a list of additional methods:
140
141=over 4
142
143=item C<$Collator-E<gt>getlocale>
144
145Returns a language code accepted and used actually on collation.
146If linguistic tailoring is not provided for a language code you passed
147(intensionally for some languages, or due to the incomplete implementation),
148this method returns a string C<'default'> meaning no special tailoring.
149
150=back
151
152=head2 A list of tailorable locales
153
64dc7822
CBW
154 locale name description
155 ----------------------------------------------------------
6484f676 156 af Afrikaans
f1a7422f 157 az Azerbaijani (Azeri)
64dc7822 158 ca Catalan
00e00351 159 cs Czech
6484f676
CBW
160 cy Welsh
161 da Danish
456a1446 162 eo Esperanto
00e00351
CBW
163 es Spanish
164 es__traditional Spanish ('ch' and 'll' as a grapheme)
64dc7822
CBW
165 et Estonian
166 fi Finnish
f1a7422f 167 fil Filipino
6484f676 168 fo Faroese
00e00351 169 fr French
f1a7422f 170 ha Hausa
6484f676
CBW
171 haw Hawaiian
172 is Icelandic
173 kl Kalaallisut
f1a7422f 174 lt Lithuanian
64dc7822 175 lv Latvian
f1a7422f 176 mt Maltese
456a1446 177 nb Norwegian Bokmal
00e00351
CBW
178 nn Norwegian Nynorsk
179 pl Polish
456a1446 180 ro Romanian
64dc7822
CBW
181 sk Slovak
182 sl Slovenian
456a1446 183 sv Swedish
6484f676 184 sw Swahili
f1a7422f
CBW
185 tr Turkish
186 wo Wolof
187 yo Yoruba
188
189=head1 INSTALL
190
191Installation of Unicode::Collate::Locale requires F<Collate/Locale.pm>,
192F<Collate/Locale/*.pm> and F<Collate/allkeys.txt>. On building,
193Unicode::Collate::Locale doesn't require F<data/*.txt> and F<mklocale>.
194Tests for Unicode::Collate::Locale are named F<t/loc_*.t>.
00e00351
CBW
195
196=head1 AUTHOR
197
198The Unicode::Collate::Locale module for perl was written
199by SADAHIRO Tomoyuki, <SADAHIRO@cpan.org>.
200This module is Copyright(C) 2004-2010, SADAHIRO Tomoyuki. Japan.
201All rights reserved.
202
203This module is free software; you can redistribute it and/or
204modify it under the same terms as Perl itself.
205
206=head1 SEE ALSO
207
208=over 4
209
210=item Unicode Collation Algorithm - UTS #10
211
212L<http://www.unicode.org/reports/tr10/>
213
214=item The Default Unicode Collation Element Table (DUCET)
215
216L<http://www.unicode.org/Public/UCA/latest/allkeys.txt>
217
218=item CLDR - Unicode Common Locale Data Repository
219
220L<http://cldr.unicode.org/>
221
222=item L<Unicode::Collate>
223
224=item L<Unicode::Normalize>
225
226=back
227
228=cut