This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta - move split change to other perlfunc changes and add issue link
[perl5.git] / t / loc_tools.pl
CommitLineData
a3815e44 1# Common tools for test files to find the locales which exist on the
13022195 2# system. Caller should have verified that this isn't miniperl before calling
abfe20b2 3# the functions.
73fc293b
KW
4
5# Note that it's okay that some languages have their native names
6# capitalized here even though that's not "right". They are lowercased
7# anyway later during the scanning process (and besides, some clueless
8# vendor might have them capitalized erroneously anyway).
9
bb31ec6a
KW
10# Functions whose names begin with underscore are internal helper functions
11# for this file, and are not to be used by outside callers.
12
99617871 13use Config;
e1f89022 14use strict;
27526f3a 15use warnings;
a3e69330 16use feature 'state';
e1f89022 17
a274778c 18eval { require POSIX; import POSIX 'locale_h'; };
e1f89022 19my $has_locale_h = ! $@;
a274778c 20
83858d2d
KW
21my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
22 LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
36dbc955
KW
23 LC_MEASUREMENT LC_PAPER LC_TELEPHONE LC_SYNTAX
24 LC_TOD));
83858d2d
KW
25my @platform_categories;
26
8aed2978
KW
27sub is_category_valid($) {
28 my $cat_name = shift =~ s/^LC_//r;
29
30 # Recognize Configure option to exclude a category
31 return $Config{ccflags} !~ /\bD?NO_LOCALE_$cat_name\b/;
32}
33
6fa6c80b
KW
34# LC_ALL can be -1 on some platforms. And, in fact the implementors could
35# legally use any integer to represent any category. But it makes the most
36# sense for them to have used small integers. Below, we create new locale
37# numbers for ones missing from this machine. We make them very negative,
38# hopefully more negative than anything likely to be a valid category on the
39# platform, but also below is a check to be sure that our guess is valid.
40my $max_bad_category_number = -1000000;
41
42# Initialize this hash so that it looks like e.g.,
43# 6 => 'CTYPE',
44# where 6 is the value of &POSIX::LC_CTYPE
45my %category_name;
cd1f7080 46my %category_number;
86e85139 47if ($has_locale_h) {
6fa6c80b 48 my $number_for_missing_category = $max_bad_category_number;
83858d2d
KW
49 foreach my $name (@known_categories) {
50 my $number = eval "&POSIX::$name";
6fa6c80b
KW
51 if ($@) {
52 # Use a negative number (smaller than any legitimate category
53 # number) if the platform doesn't support this category, so we
54 # have an entry for all the ones that might be specified in calls
55 # to us.
829290ef 56 $number = $number_for_missing_category--;
6fa6c80b
KW
57 }
58 elsif ( $number !~ / ^ -? \d+ $ /x
59 || $number <= $max_bad_category_number)
60 {
61 # We think this should be an int. And it has to be larger than
62 # any of our synthetic numbers.
83858d2d
KW
63 die "Unexpected locale category number '$number' for $name"
64 }
65 else {
66 push @platform_categories, $name;
6fa6c80b
KW
67 }
68
83858d2d 69 $name =~ s/LC_//;
6fa6c80b 70 $category_name{$number} = "$name";
cd1f7080 71 $category_number{$name} = $number;
6fa6c80b
KW
72 }
73}
74
759b442c
KW
75sub _my_diag($) {
76 my $message = shift;
77 if (defined &main::diag) {
78 diag($message);
79 }
80 else {
81 local($\, $", $,) = (undef, ' ', '');
82 print STDERR $message, "\n";
83 }
84}
85
13022195
KW
86sub _my_fail($) {
87 my $message = shift;
88 if (defined &main::fail) {
89 fail($message);
90 }
91 else {
92 local($\, $", $,) = (undef, ' ', '');
93 print "not ok 0 $message\n";
94 }
95}
96
bb31ec6a
KW
97sub _trylocale ($$$$) { # For use only by other functions in this file!
98
99 # Adds the locale given by the first parameter to the list given by the
cd1f7080
KW
100 # 3rd iff the platform supports the locale in each of the category numbers
101 # given by the 2nd parameter, which is either a single category or a
fcabef25 102 # reference to a list of categories.
cd1f7080
KW
103 #
104 # The 4th parameter is true if to accept locales that aren't apparently
105 # fully compatible with Perl.
bb31ec6a 106
73fc293b 107 my $locale = shift;
9b0711ee 108 my $categories = shift;
73fc293b 109 my $list = shift;
2c6c88ec 110 my $allow_incompatible = shift;
f079f9b7 111
162eb2b8 112 return if ! $locale || grep { $locale eq $_ } @$list;
9b0711ee 113
577d3e04
KW
114 # This is a toy (pig latin) locale that is not fully implemented on some
115 # systems
116 return if $locale =~ / ^ pig $ /ix;
117
8e13243a
KW
118 # Certain platforms have a crippled locale system in which setlocale
119 # returns success for just about any possible locale name, but if anything
120 # actually happens as a result of the call, it is that the underlying
121 # locale is set to a system default, likely C or C.UTF-8. We can't test
122 # such systems fully, but we shouldn't disable the user from using
123 # locales, as it may work out for them (or not).
124 return if defined $Config{d_setlocale_accepts_any_locale_name}
125 && $locale !~ / ^ (?: C | POSIX | C\.UTF-8 ) $/ix;
99617871 126
9b0711ee
KW
127 $categories = [ $categories ] unless ref $categories;
128
f079f9b7
KW
129 my $badutf8 = 0;
130 my $plays_well = 1;
131
132 use warnings 'locale';
133
134 local $SIG{__WARN__} = sub {
973072b2 135 $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
1649de0c
KW
136 $plays_well = 0 if grep {
137 /Locale .* may not work well(?#
ef5d49f0 138 )|The Perl program will use the expected meanings/i
1649de0c 139 } @_;
f079f9b7
KW
140 };
141
9c5fc553
KW
142 # Incompatible locales aren't warned about unless using locales.
143 use locale;
144
fcabef25
KW
145 # Sort the input so CTYPE is first, COLLATE comes after all but ALL. This
146 # is because locale.c detects bad locales only with CTYPE, and COLLATE on
147 # some platforms can core dump if it is a bad locale.
148 my @sorted;
149 my $has_ctype = 0;
150 my $has_all = 0;
151 my $has_collate = 0;
9b0711ee 152 foreach my $category (@$categories) {
cd1f7080
KW
153 die "category '$category' must instead be a number"
154 unless $category =~ / ^ -? \d+ $ /x;
fcabef25
KW
155 if ($category_name{$category} eq 'CTYPE') {
156 $has_ctype = 1;
157 }
158 elsif ($category_name{$category} eq 'ALL') {
159 $has_all = 1;
160 }
161 elsif ($category_name{$category} eq 'COLLATE') {
162 $has_collate = 1;
163 }
164 else {
165 push @sorted, $category unless grep { $_ == $category } @sorted;
166 }
167 }
168 push @sorted, $category_number{'COLLATE'} if $has_collate;
169 push @sorted, $category_number{'ALL'} if $has_all;
170 unshift @sorted, $category_number{'CTYPE'} if $has_ctype || ! $allow_incompatible;
cd1f7080 171
fcabef25 172 foreach my $category (@sorted) {
9b0711ee 173 return unless setlocale($category, $locale);
caf06cdc 174 last if $badutf8 || ! $plays_well;
73fc293b
KW
175 }
176
177 if ($badutf8) {
13022195 178 _my_fail("Verify locale name doesn't contain malformed utf8");
73fc293b
KW
179 return;
180 }
caf06cdc 181 push @$list, $locale if $plays_well || $allow_incompatible;
73fc293b
KW
182}
183
bb31ec6a 184sub _decode_encodings { # For use only by other functions in this file!
73fc293b
KW
185 my @enc;
186
187 foreach (split(/ /, shift)) {
188 if (/^(\d+)$/) {
189 push @enc, "ISO8859-$1";
190 push @enc, "iso8859$1"; # HP
191 if ($1 eq '1') {
192 push @enc, "roman8"; # HP
193 }
194 push @enc, $_;
195 push @enc, "$_.UTF-8";
d646bffe
KW
196 push @enc, "$_.65001"; # Windows UTF-8
197 push @enc, "$_.ACP"; # Windows ANSI code page
198 push @enc, "$_.OCP"; # Windows OEM code page
cf34c81f 199 push @enc, "$_.1252"; # Windows
73fc293b
KW
200 }
201 }
202 if ($^O eq 'os390') {
203 push @enc, qw(IBM-037 IBM-819 IBM-1047);
204 }
205 push @enc, "UTF-8";
d646bffe 206 push @enc, "65001"; # Windows UTF-8
73fc293b
KW
207
208 return @enc;
209}
210
83858d2d
KW
211sub valid_locale_categories() {
212 # Returns a list of the locale categories (expressed as strings, like
213 # "LC_ALL) known to this program that are available on this platform.
214
8aed2978 215 return grep { is_category_valid($_) } @platform_categories;
83858d2d
KW
216}
217
128e4113 218sub locales_enabled(;$) {
23f9d84e
KW
219 # If no parameter is specified, the function returns 1 if there is any
220 # "safe" locale handling available to the caller; otherwise 0. Safeness
221 # is defined here as the caller operating in the main thread of a program,
222 # or if threaded locales are safe on the platform and Configured to be
223 # used. This sub is used for testing purposes, and for those, this
224 # definition of safety is sufficient, and necessary to get some tests to
225 # run on certain configurations on certain platforms. But beware that the
226 # main thread can change the locale of any subthreads unless
227 # ${^SAFE_LOCALES} is non-zero.
128e4113 228 #
23f9d84e
KW
229 # Use the optional parameter to discover if a particular category or
230 # categories are available on the system. 1 is returned if the global
231 # criteria described in the previous paragraph are true, AND if all the
232 # specified categories are available on the platform and Configured to be
233 # used. Otherwise 0 is returned. The parameter is either a single POSIX
234 # locale category or a reference to a list of them. Each category must be
235 # its name as a string, like 'LC_TIME' (the initial 'LC_' is optional), or
236 # the number this platform uses to signify the category (e.g.,
237 # 'locales_enabled(&POSIX::LC_CTYPE)'
128e4113 238 #
23f9d84e
KW
239 # When the function returns 1 and a parameter was specified as a list
240 # reference, the reference will be altered on return to point to an
241 # equivalent list such that the categories are numeric instead of strings
242 # and sorted to meet the input expectations of _trylocale().
cd1f7080 243 #
23f9d84e
KW
244 # It is a fatal error to call this with something that isn't a known
245 # category to this file. If this happens, look first for a typo, and
246 # second if you are using a category unknown to Perl. In the latter case
247 # a bug report should be submitted.
128e4113 248
aa8a2baa
KW
249 # khw cargo-culted the '?' in the pattern on the next line.
250 return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE\b/;
251
d373bd46
KW
252 # If we can't load the POSIX XS module, we can't have locales even if they
253 # normally would be available
254 return 0 if ! defined &DynaLoader::boot_DynaLoader;
255
b0441c5b
KW
256 # Don't test locales where they aren't safe. On systems with unsafe
257 # threads, for the purposes of testing, we consider the main thread safe,
258 # and all other threads unsafe.
259 if (! ${^SAFE_LOCALES}) {
260 require threads;
261 return 0 if threads->tid() != 0;
262 }
39481c55
KW
263
264 # If no setlocale, we need the POSIX 2008 alternatives
aa8a2baa
KW
265 if (! $Config{d_setlocale}) {
266 return 0 if $Config{ccflags} =~ /\bD?NO_POSIX_2008_LOCALE\b/;
267 return 0 unless $Config{d_newlocale};
268 return 0 unless $Config{d_uselocale};
269 return 0 unless $Config{d_duplocale};
270 return 0 unless $Config{d_freelocale};
271 }
128e4113
KW
272
273 # Done with the global possibilities. Now check if any passed in category
274 # is disabled.
cd1f7080 275
23f9d84e 276 my $categories_ref = $_[0];
cd1f7080
KW
277 my $return_categories_numbers = 0;
278 my @categories_numbers;
279 my $has_LC_ALL = 0;
280 my $has_LC_COLLATE = 0;
281
e6965c14 282 if (defined $categories_ref) {
cd1f7080
KW
283 my @local_categories_copy;
284
23f9d84e
KW
285 my $reftype = ref $categories_ref;
286 if ($reftype eq 'ARRAY') {
287 @local_categories_copy = @$categories_ref;
cd1f7080
KW
288 $return_categories_numbers = 1;
289 }
23f9d84e
KW
290 elsif ($reftype ne "") {
291 die "Parameter to locales_enabled() must be an ARRAY;"
292 . " instead you used a $reftype";
293 }
cd1f7080
KW
294 else { # Single category passed in
295 @local_categories_copy = $categories_ref;
296 }
297
308482c2
KW
298 for my $category_name_or_number (@local_categories_copy) {
299 my $name;
300 my $number;
301 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
302 $number = $category_name_or_number;
303 die "Invalid locale category number '$number'"
304 unless grep { $number == $_ } keys %category_name;
305 $name = $category_name{$number};
e6965c14
KW
306 }
307 else {
308482c2
KW
308 $name = $category_name_or_number;
309 $name =~ s/ ^ LC_ //x;
310 foreach my $trial (keys %category_name) {
311 if ($category_name{$trial} eq $name) {
312 $number = $trial;
313 last;
314 }
315 }
316 die "Invalid locale category name '$name'"
317 unless defined $number;
e6965c14
KW
318 }
319
8aed2978
KW
320 return 0 if $number <= $max_bad_category_number
321 || ! is_category_valid($name);
322
cfe9fa91
KW
323
324 eval "defined &POSIX::LC_$name";
325 return 0 if $@;
cd1f7080
KW
326
327 if ($return_categories_numbers) {
328 if ($name eq 'CTYPE') {
329 unshift @categories_numbers, $number; # Always first
330 }
331 elsif ($name eq 'ALL') {
332 $has_LC_ALL = 1;
333 }
334 elsif ($name eq 'COLLATE') {
335 $has_LC_COLLATE = 1;
336 }
337 else {
338 push @categories_numbers, $number;
339 }
340 }
341 }
342 }
343
344 if ($return_categories_numbers) {
345
346 # COLLATE comes after all other locales except ALL, which comes last
347 if ($has_LC_COLLATE) {
348 push @categories_numbers, $category_number{'COLLATE'};
128e4113 349 }
cd1f7080
KW
350 if ($has_LC_ALL) {
351 push @categories_numbers, $category_number{'ALL'};
352 }
16791992
KW
353
354 @$categories_ref = @categories_numbers;
128e4113
KW
355 }
356
357 return 1;
358}
359
360
cd1f7080
KW
361sub find_locales ($;$) {
362
363 # Returns an array of all the locales we found on the system. If the
364 # optional 2nd parameter is non-zero, the list includes all found locales;
365 # otherwise it is restricted to those locales that play well with Perl, as
366 # far as we can easily determine.
367 #
368 # The first parameter is either a single locale category or a reference to
369 # a list of categories to find valid locales for it (or in the case of
370 # multiple) for all of them. Each category can be a name (like 'LC_ALL'
371 # or simply 'ALL') or the C enum value for the category.
372
f2956899 373 my $input_categories = shift;
2c6c88ec 374 my $allow_incompatible = shift // 0;
73fc293b 375
f2956899
KW
376 my @categories = (ref $input_categories) ? $input_categories->@* : $input_categories;
377 return unless locales_enabled(\@categories);
cd1f7080
KW
378
379 # Note, the subroutine call above converts the $categories into a form
380 # suitable for _trylocale().
d369fd5b
KW
381
382 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
383 # and mingw32 uses said silly CRT
384 # This doesn't seem to be an issue any more, at least on Windows XP,
385 # so re-enable the tests for Windows XP onwards.
386 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
387 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
128e4113
KW
388 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
389 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
d369fd5b
KW
390
391 # UWIN seems to loop after taint tests, just skip for now
128e4113 392 return if ($^O =~ /^uwin/);
d369fd5b 393
e1f89022 394 my @Locale;
f2956899
KW
395 _trylocale("C", \@categories, \@Locale, $allow_incompatible);
396 _trylocale("POSIX", \@categories, \@Locale, $allow_incompatible);
201f75a9 397
960e29a3 398 if ($Config{d_has_C_UTF8} && $Config{d_has_C_UTF8} eq 'true') {
f2956899 399 _trylocale("C.UTF-8", \@categories, \@Locale, $allow_incompatible);
201f75a9
KW
400 }
401
8e13243a
KW
402 # There's no point in looking at anything more if we know that setlocale
403 # will return success on any garbage or non-garbage name.
404 return sort @Locale if defined $Config{d_setlocale_accepts_any_locale_name};
405
82b1f618 406 foreach (1..16) {
f2956899
KW
407 _trylocale("ISO8859-$_", \@categories, \@Locale, $allow_incompatible);
408 _trylocale("iso8859$_", \@categories, \@Locale, $allow_incompatible);
409 _trylocale("iso8859-$_", \@categories, \@Locale, $allow_incompatible);
410 _trylocale("iso_8859_$_", \@categories, \@Locale, $allow_incompatible);
411 _trylocale("isolatin$_", \@categories, \@Locale, $allow_incompatible);
412 _trylocale("isolatin-$_", \@categories, \@Locale, $allow_incompatible);
413 _trylocale("iso_latin_$_", \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
414 }
415
416 # Sanitize the environment so that we can run the external 'locale'
417 # program without the taint mode getting grumpy.
418
419 # $ENV{PATH} is special in VMS.
8b76dc1d 420 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
73fc293b
KW
421
422 # Other subversive stuff.
8b76dc1d 423 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
73fc293b
KW
424
425 if (-x "/usr/bin/locale"
1ae6ead9 426 && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null"))
73fc293b
KW
427 {
428 while (<LOCALES>) {
429 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
430 # ain't great when we're running this testPERL_UNICODE= so that utf8
431 # locales will cause all IO hadles to default to (assume) utf8
432 next unless utf8::valid($_);
433 chomp;
f2956899 434 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
435 }
436 close(LOCALES);
437 } elsif ($^O eq 'VMS'
438 && defined($ENV{'SYS$I18N_LOCALE'})
439 && -d 'SYS$I18N_LOCALE')
440 {
441 # The SYS$I18N_LOCALE logical name search list was not present on
442 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
443 opendir(LOCALES, "SYS\$I18N_LOCALE:");
444 while ($_ = readdir(LOCALES)) {
445 chomp;
f2956899 446 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
447 }
448 close(LOCALES);
449 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
450
7e1e0d99
KW
451 # OpenBSD doesn't have a locale executable, so reading
452 # /usr/share/locale is much easier and faster than the last resort
453 # method.
73fc293b
KW
454
455 opendir(LOCALES, '/usr/share/locale');
456 while ($_ = readdir(LOCALES)) {
457 chomp;
f2956899 458 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
459 }
460 close(LOCALES);
461 } else { # Final fallback. Try our list of locales hard-coded here
462
463 # This is going to be slow.
464 my @Data;
465
589c4ae0
KW
466 # Locales whose name differs if the utf8 bit is on are stored in these
467 # two files with appropriate encodings.
468 my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
9969000e
TK
469 ? _source_location() . "/lib/locale/utf8"
470 : _source_location() . "/lib/locale/latin1";
589c4ae0
KW
471 if (-e $data_file) {
472 @Data = do $data_file;
473 }
474 else {
475 _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
73fc293b
KW
476 }
477
478 # The rest of the locales are in this file.
a3e69330
TC
479 state @my_data = <DATA>; close DATA if fileno DATA;
480 push @Data, @my_data;
73fc293b
KW
481
482 foreach my $line (@Data) {
f670c2bb 483 chomp $line;
73fc293b
KW
484 my ($locale_name, $language_codes, $country_codes, $encodings) =
485 split /:/, $line;
759b442c
KW
486 _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
487 unless defined $locale_name;
73fc293b
KW
488 my @enc = _decode_encodings($encodings);
489 foreach my $loc (split(/ /, $locale_name)) {
f2956899 490 _trylocale($loc, \@categories, \@Locale, $allow_incompatible);
73fc293b 491 foreach my $enc (@enc) {
f2956899 492 _trylocale("$loc.$enc", \@categories, \@Locale,
2c6c88ec 493 $allow_incompatible);
73fc293b
KW
494 }
495 $loc = lc $loc;
496 foreach my $enc (@enc) {
f2956899 497 _trylocale("$loc.$enc", \@categories, \@Locale,
2c6c88ec 498 $allow_incompatible);
73fc293b
KW
499 }
500 }
501 foreach my $lang (split(/ /, $language_codes)) {
f2956899 502 _trylocale($lang, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
503 foreach my $country (split(/ /, $country_codes)) {
504 my $lc = "${lang}_${country}";
f2956899 505 _trylocale($lc, \@categories, \@Locale, $allow_incompatible);
73fc293b 506 foreach my $enc (@enc) {
f2956899 507 _trylocale("$lc.$enc", \@categories, \@Locale,
2c6c88ec 508 $allow_incompatible);
73fc293b
KW
509 }
510 my $lC = "${lang}_\U${country}";
f2956899 511 _trylocale($lC, \@categories, \@Locale, $allow_incompatible);
73fc293b 512 foreach my $enc (@enc) {
f2956899 513 _trylocale("$lC.$enc", \@categories, \@Locale,
2c6c88ec 514 $allow_incompatible);
73fc293b
KW
515 }
516 }
517 }
518 }
519 }
520
521 @Locale = sort @Locale;
522
523 return @Locale;
73fc293b
KW
524}
525
ab8b8bcc 526sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
9b0711ee 527 # is a UTF-8 locale
92c0a900
KW
528
529 # On z/OS, even locales marked as UTF-8 aren't.
530 return 0 if ord "A" != 65;
531
cfe9fa91 532 return 0 unless locales_enabled('LC_CTYPE');
21732d5c 533
ab8b8bcc
KW
534 my $locale = shift;
535
31f05a37 536 use locale;
f079f9b7 537 no warnings 'locale'; # We may be trying out a weird locale
31f05a37
KW
538
539 my $save_locale = setlocale(&POSIX::LC_CTYPE());
540 if (! $save_locale) {
541 ok(0, "Verify could save previous locale");
542 return 0;
543 }
544
545 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
546 ok(0, "Verify could setlocale to $locale");
547 return 0;
548 }
549
550 my $ret = 0;
551
552 # Use an op that gives different results for UTF-8 than any other locale.
553 # If a platform has UTF-8 locales, there should be at least one locale on
554 # most platforms with UTF-8 in its name, so if there is a bug in the op
555 # giving a false negative, we should get a failure for those locales as we
556 # go through testing all the locales on the platform.
557 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
558 if ($locale =~ /UTF-?8/i) {
559 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
560 }
561 }
562 else {
563 $ret = 1;
564 }
565
566 die "Couldn't restore locale '$save_locale'"
567 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
568
569 return $ret;
ab8b8bcc
KW
570}
571
e0bfe19f
KW
572sub find_utf8_ctype_locales (;$) { # Return the names of the locales that core
573 # Perl thinks are UTF-8 LC_CTYPE locales.
9b0711ee
KW
574 # Optional parameter is a reference to a
575 # list of locales to try; if omitted, this
576 # tries all locales it can find on the
577 # platform
cfe9fa91
KW
578 return unless locales_enabled('LC_CTYPE');
579
ab8b8bcc 580 my $locales_ref = shift;
e0bfe19f 581 my @return;
21732d5c 582
ab8b8bcc 583 if (! defined $locales_ref) {
21732d5c 584
2c6c88ec 585 my @locales = find_locales(&POSIX::LC_CTYPE());
ab8b8bcc
KW
586 $locales_ref = \@locales;
587 }
588
589 foreach my $locale (@$locales_ref) {
e0bfe19f 590 push @return, $locale if is_locale_utf8($locale);
ab8b8bcc
KW
591 }
592
e0bfe19f
KW
593 return @return;
594}
595
596
597sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
b5954e7e 598 # thinks is a UTF-8 LC_CTYPE non-turkic
e0bfe19f
KW
599 # locale.
600 # Optional parameter is a reference to a
601 # list of locales to try; if omitted, this
602 # tries all locales it can find on the
603 # platform
604 my $try_locales_ref = shift;
605
606 my @utf8_locales = find_utf8_ctype_locales($try_locales_ref);
b5954e7e 607 my @turkic_locales = find_utf8_turkic_locales($try_locales_ref);
e0bfe19f 608
b5954e7e
KW
609 my %seen_turkic;
610
611 # Create undef elements in the hash for turkic locales
612 @seen_turkic{@turkic_locales} = ();
613
614 foreach my $locale (@utf8_locales) {
615 return $locale unless exists $seen_turkic{$locale};
616 }
e0bfe19f 617
ab8b8bcc
KW
618 return;
619}
620
b5954e7e
KW
621sub find_utf8_turkic_locales (;$) {
622
623 # Return the name of all the locales that core Perl thinks are UTF-8
624 # Turkic LC_CTYPE. Optional parameter is a reference to a list of locales
625 # to try; if omitted, this tries all locales it can find on the platform
626
627 my @return;
628
e41845ea
KW
629 return unless locales_enabled('LC_CTYPE');
630
b5954e7e
KW
631 my $save_locale = setlocale(&POSIX::LC_CTYPE());
632 foreach my $locale (find_utf8_ctype_locales(shift)) {
633 use locale;
634 setlocale(&POSIX::LC_CTYPE(), $locale);
635 push @return, $locale if uc('i') eq "\x{130}";
636 }
637 setlocale(&POSIX::LC_CTYPE(), $save_locale);
638
639 return @return;
640}
641
642sub find_utf8_turkic_locale (;$) {
643 my @turkics = find_utf8_turkic_locales(shift);
644
645 return unless @turkics;
646 return $turkics[0]
647}
648
649
9969000e
TK
650# returns full path to the directory containing the current source
651# file, inspired by mauke's Dir::Self
652sub _source_location {
653 require File::Spec;
654
655 my $caller_filename = (caller)[1];
656
b3c872eb 657 my $loc = File::Spec->rel2abs(
9969000e 658 File::Spec->catpath(
b3c872eb 659 (File::Spec->splitpath($caller_filename))[0, 1], ''
9969000e
TK
660 )
661 );
b3c872eb
TK
662
663 return ($loc =~ /^(.*)$/)[0]; # untaint
9969000e
TK
664}
665
73fc293b
KW
6661
667
668# Format of data is: locale_name, language_codes, country_codes, encodings
669__DATA__
670Afrikaans:af:za:1 15
671Arabic:ar:dz eg sa:6 arabic8
672Brezhoneg Breton:br:fr:1 15
673Bulgarski Bulgarian:bg:bg:5
674Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
675Hrvatski Croatian:hr:hr:2
676Cymraeg Welsh:cy:cy:1 14 15
677Czech:cs:cz:2
678Dansk Danish:da:dk:1 15
679Nederlands Dutch:nl:be nl:1 15
680English American British:en:au ca gb ie nz us uk zw:1 15 cp850
681Esperanto:eo:eo:3
682Eesti Estonian:et:ee:4 6 13
683Suomi Finnish:fi:fi:1 15
684Flamish::fl:1 15
685Deutsch German:de:at be ch de lu:1 15
686Euskaraz Basque:eu:es fr:1 15
687Galego Galician:gl:es:1 15
688Ellada Greek:el:gr:7 g8
689Frysk:fy:nl:1 15
690Greenlandic:kl:gl:4 6
691Hebrew:iw:il:8 hebrew8
692Hungarian:hu:hu:2
693Indonesian:id:id:1 15
694Gaeilge Irish:ga:IE:1 14 15
695Italiano Italian:it:ch it:1 15
696Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
697Korean:ko:kr:
698Latine Latin:la:va:1 15
699Latvian:lv:lv:4 6 13
700Lithuanian:lt:lt:4 6 13
701Macedonian:mk:mk:1 15
702Maltese:mt:mt:3
703Moldovan:mo:mo:2
704Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
705Occitan:oc:es:1 15
706Polski Polish:pl:pl:2
707Rumanian:ro:ro:2
708Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
709Serbski Serbian:sr:yu:5
710Slovak:sk:sk:2
711Slovene Slovenian:sl:si:2
712Sqhip Albanian:sq:sq:1 15
713Svenska Swedish:sv:fi se:1 15
714Thai:th:th:11 tis620
715Turkish:tr:tr:9 turkish8
716Yiddish:yi::1 15