This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In bodies_by_type in sv.c, no need to store for values for HEs.
[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
456a1446 7our $VERSION = '0.56';
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
456a1446
CBW
15my %LocaleFile = map { ($_, $_) } qw(cs eo es fr nn pl ro sv);
16 $LocaleFile{'default'} = '';
17 $LocaleFile{'es__traditional'} = 'es_trad';
18 $LocaleFile{'nb'} = 'nn';
00e00351
CBW
19
20sub _locale {
21 my $locale = shift;
22 if ($locale) {
23 $locale = lc $locale;
24 $locale =~ tr/\-\ \./_/;
456a1446 25 $locale =~ s/_trad\z/_traditional/;
00e00351
CBW
26 $LocaleFile{$locale} and return $locale;
27
28 my ($l,$t,$v) = split(/_/, $locale.'__');
456a1446 29 for my $loc ("${l}_${t}_$v", "${l}_$t", "${l}__$v", "${l}__$t", $l) {
00e00351
CBW
30 $LocaleFile{$loc} and return $loc;
31 }
32 }
33 return 'default';
34}
35
36sub getlocale {
37 return shift->{accepted_locale};
38}
39
40sub new {
41 my $class = shift;
42 my %hash = @_;
43 my ($href,$file);
44 $hash{accepted_locale} = _locale($hash{locale});
45
46 $file = $LocaleFile{ $hash{accepted_locale} };
47 if ($file) {
48 my $filepath = File::Spec->catfile($ModPath, $file.$PL_EXT);
49 $href = do $filepath;
50 }
51 $href->{table} = $KeyPath;
52
53 while (my($k,$v) = each %$href) {
54 if (exists $hash{$k}) {
55 croak "$k is reserved by $hash{locale}, can't be overwritten";
56 }
57 $hash{$k} = $v;
58 }
59 return $class->SUPER::new(%hash);
60}
61
621;
63__END__
64
65=head1 NAME
66
67Unicode::Collate::Locale - Linguistic tailoring for DUCET via Unicode::Collate
68
69=head1 SYNOPSIS
70
71 use Unicode::Collate::Locale;
72
73 $Collator = Unicode::Collate::Locale->
74 new(locale => $locale_name, %tailoring);
75
76 @sorted = $Collator->sort(@not_sorted);
77
78=head1 DESCRIPTION
79
80This module provides linguistic tailoring for it
81taking advantage of C<Unicode::Collate>.
82
83=head2 Constructor
84
85The C<new> method returns a collator object.
86
87A parameter list for the constructor is a hash, which can include
88a special key C<'locale'> and its value (case-insensitive) standing
89for a two-letter language code (ISO-639) like C<'en'> for English.
90For example, C<Unicode::Collate::Locale-E<gt>new(locale =E<gt> 'FR')>
91returns a collator tailored for French.
92
93C<$locale_name> may be suffixed with a territory(country)
94code or a variant code, which are separated with C<'_'>.
95E.g. C<en_US> for English in USA,
96C<es_ES_traditional> for Spanish in Spain (Traditional),
97
98If C<$localename> is not defined,
99fallback is selected in the following order:
100
101 1. language_territory_variant
102 2. language_territory
103 3. language__variant
104 4. language
105 5. default
106
107Tailoring tags provided by C<Unicode::Collate> are allowed
108as long as they are not used for C<'locale'> support.
109Esp. the C<table> tag is always untailorable
110since it is reserved for DUCET.
111
112E.g. a collator for French, which ignores diacritics and case difference
113(i.e. level 1), with reversed case ordering and no normalization.
114
115 Unicode::Collate::Locale->new(
116 level => 1,
117 locale => 'fr',
118 upper_before_lower => 1,
119 normalization => undef
120 )
121
122=head2 Methods
123
124C<Unicode::Collate::Locale> is a subclass of C<Unicode::Collate>
125and methods other than C<new> are inherited from C<Unicode::Collate>.
126
127Here is a list of additional methods:
128
129=over 4
130
131=item C<$Collator-E<gt>getlocale>
132
133Returns a language code accepted and used actually on collation.
134If linguistic tailoring is not provided for a language code you passed
135(intensionally for some languages, or due to the incomplete implementation),
136this method returns a string C<'default'> meaning no special tailoring.
137
138=back
139
140=head2 A list of tailorable locales
141
142 locale name description
143
144 cs Czech
456a1446 145 eo Esperanto
00e00351
CBW
146 es Spanish
147 es__traditional Spanish ('ch' and 'll' as a grapheme)
148 fr French
456a1446 149 nb Norwegian Bokmal
00e00351
CBW
150 nn Norwegian Nynorsk
151 pl Polish
456a1446
CBW
152 ro Romanian
153 sv Swedish
00e00351
CBW
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