This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Updated Unic0de-Collate to CPAN version 0.55
[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
7our $VERSION = '0.55';
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
15my %LocaleFile = (
16 'default' => '',
17 'cs' => 'cs',
18 'es' => 'es',
19 'es__traditional' => 'es_trad',
20 'fr' => 'fr',
21 'nn' => 'nn',
22 'pl' => 'pl',
23);
24
25sub _locale {
26 my $locale = shift;
27 if ($locale) {
28 $locale = lc $locale;
29 $locale =~ tr/\-\ \./_/;
30 $LocaleFile{$locale} and return $locale;
31
32 my ($l,$t,$v) = split(/_/, $locale.'__');
33 for my $loc ("${l}_${t}_$v", "${l}_$t", "${l}__$v", $l) {
34 $LocaleFile{$loc} and return $loc;
35 }
36 }
37 return 'default';
38}
39
40sub getlocale {
41 return shift->{accepted_locale};
42}
43
44sub new {
45 my $class = shift;
46 my %hash = @_;
47 my ($href,$file);
48 $hash{accepted_locale} = _locale($hash{locale});
49
50 $file = $LocaleFile{ $hash{accepted_locale} };
51 if ($file) {
52 my $filepath = File::Spec->catfile($ModPath, $file.$PL_EXT);
53 $href = do $filepath;
54 }
55 $href->{table} = $KeyPath;
56
57 while (my($k,$v) = each %$href) {
58 if (exists $hash{$k}) {
59 croak "$k is reserved by $hash{locale}, can't be overwritten";
60 }
61 $hash{$k} = $v;
62 }
63 return $class->SUPER::new(%hash);
64}
65
661;
67__END__
68
69=head1 NAME
70
71Unicode::Collate::Locale - Linguistic tailoring for DUCET via Unicode::Collate
72
73=head1 SYNOPSIS
74
75 use Unicode::Collate::Locale;
76
77 $Collator = Unicode::Collate::Locale->
78 new(locale => $locale_name, %tailoring);
79
80 @sorted = $Collator->sort(@not_sorted);
81
82=head1 DESCRIPTION
83
84This module provides linguistic tailoring for it
85taking advantage of C<Unicode::Collate>.
86
87=head2 Constructor
88
89The C<new> method returns a collator object.
90
91A parameter list for the constructor is a hash, which can include
92a special key C<'locale'> and its value (case-insensitive) standing
93for a two-letter language code (ISO-639) like C<'en'> for English.
94For example, C<Unicode::Collate::Locale-E<gt>new(locale =E<gt> 'FR')>
95returns a collator tailored for French.
96
97C<$locale_name> may be suffixed with a territory(country)
98code or a variant code, which are separated with C<'_'>.
99E.g. C<en_US> for English in USA,
100C<es_ES_traditional> for Spanish in Spain (Traditional),
101
102If C<$localename> is not defined,
103fallback is selected in the following order:
104
105 1. language_territory_variant
106 2. language_territory
107 3. language__variant
108 4. language
109 5. default
110
111Tailoring tags provided by C<Unicode::Collate> are allowed
112as long as they are not used for C<'locale'> support.
113Esp. the C<table> tag is always untailorable
114since it is reserved for DUCET.
115
116E.g. a collator for French, which ignores diacritics and case difference
117(i.e. level 1), with reversed case ordering and no normalization.
118
119 Unicode::Collate::Locale->new(
120 level => 1,
121 locale => 'fr',
122 upper_before_lower => 1,
123 normalization => undef
124 )
125
126=head2 Methods
127
128C<Unicode::Collate::Locale> is a subclass of C<Unicode::Collate>
129and methods other than C<new> are inherited from C<Unicode::Collate>.
130
131Here is a list of additional methods:
132
133=over 4
134
135=item C<$Collator-E<gt>getlocale>
136
137Returns a language code accepted and used actually on collation.
138If linguistic tailoring is not provided for a language code you passed
139(intensionally for some languages, or due to the incomplete implementation),
140this method returns a string C<'default'> meaning no special tailoring.
141
142=back
143
144=head2 A list of tailorable locales
145
146 locale name description
147
148 cs Czech
149 es Spanish
150 es__traditional Spanish ('ch' and 'll' as a grapheme)
151 fr French
152 nn Norwegian Nynorsk
153 pl Polish
154
155=head1 AUTHOR
156
157The Unicode::Collate::Locale module for perl was written
158by SADAHIRO Tomoyuki, <SADAHIRO@cpan.org>.
159This module is Copyright(C) 2004-2010, SADAHIRO Tomoyuki. Japan.
160All rights reserved.
161
162This module is free software; you can redistribute it and/or
163modify it under the same terms as Perl itself.
164
165=head1 SEE ALSO
166
167=over 4
168
169=item Unicode Collation Algorithm - UTS #10
170
171L<http://www.unicode.org/reports/tr10/>
172
173=item The Default Unicode Collation Element Table (DUCET)
174
175L<http://www.unicode.org/Public/UCA/latest/allkeys.txt>
176
177=item CLDR - Unicode Common Locale Data Repository
178
179L<http://cldr.unicode.org/>
180
181=item L<Unicode::Collate>
182
183=item L<Unicode::Normalize>
184
185=back
186
187=cut