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