1 # Common tools for test files files to find the locales which exist on the
2 # system. Caller should have defined ok() for the unlikely event that setup
3 # here fails, and should have verified that this isn't miniperl before calling
6 # Note that it's okay that some languages have their native names
7 # capitalized here even though that's not "right". They are lowercased
8 # anyway later during the scanning process (and besides, some clueless
9 # vendor might have them capitalized erroneously anyway).
11 sub _trylocale { # Adds the locale given by the first parameter to the list
12 # given by the 3rd iff the platform supports the locale in
13 # each of the categories given by the 2nd parameter, which
14 # is either a single category or a reference to a list of
17 my $categories = shift;
19 return if ! $locale || grep { $locale eq $_ } @$list;
21 $categories = [ $categories ] unless ref $categories;
23 foreach my $category (@$categories) {
24 return unless setlocale($category, $locale);
29 local $SIG{__WARN__} = sub {
30 $badutf8 = $_[0] =~ /Malformed UTF-8/;
35 ok(0, "Verify locale name doesn't contain malformed utf8");
41 sub _decode_encodings {
44 foreach (split(/ /, shift)) {
46 push @enc, "ISO8859-$1";
47 push @enc, "iso8859$1"; # HP
49 push @enc, "roman8"; # HP
52 push @enc, "$_.UTF-8";
53 push @enc, "$_.65001"; # Windows UTF-8
54 push @enc, "$_.ACP"; # Windows ANSI code page
55 push @enc, "$_.OCP"; # Windows OEM code page
59 push @enc, qw(IBM-037 IBM-819 IBM-1047);
62 push @enc, "65001"; # Windows UTF-8
67 sub find_locales ($) { # Returns an array of all the locales we found on the
68 # system. The parameter is either a single locale
69 # category or a reference to a list of categories to
70 # find valid locales for it (or in the case of
71 # multiple) for all of them
72 my $categories = shift;
75 my $have_setlocale = $Config{d_setlocale};
77 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
78 # and mingw32 uses said silly CRT
79 # This doesn't seem to be an issue any more, at least on Windows XP,
80 # so re-enable the tests for Windows XP onwards.
81 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
82 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
83 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
84 $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
86 # UWIN seems to loop after taint tests, just skip for now
87 $have_setlocale = 0 if ($^O =~ /^uwin/);
89 return unless $have_setlocale;
91 # Done this way in case this is 'required' in the caller before seeing if
93 eval { require POSIX; import POSIX 'locale_h'; };
94 unless (defined &POSIX::LC_CTYPE) {
98 _trylocale("C", $categories, \@Locale);
99 _trylocale("POSIX", $categories, \@Locale);
101 _trylocale("ISO8859-$_", $categories, \@Locale);
102 _trylocale("iso8859$_", $categories, \@Locale);
103 _trylocale("iso8859-$_", $categories, \@Locale);
104 _trylocale("iso_8859_$_", $categories, \@Locale);
105 _trylocale("isolatin$_", $categories, \@Locale);
106 _trylocale("isolatin-$_", $categories, \@Locale);
107 _trylocale("iso_latin_$_", $categories, \@Locale);
110 # Sanitize the environment so that we can run the external 'locale'
111 # program without the taint mode getting grumpy.
113 # $ENV{PATH} is special in VMS.
114 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
116 # Other subversive stuff.
117 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
119 if (-x "/usr/bin/locale"
120 && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|"))
123 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
124 # ain't great when we're running this testPERL_UNICODE= so that utf8
125 # locales will cause all IO hadles to default to (assume) utf8
126 next unless utf8::valid($_);
128 _trylocale($_, $categories, \@Locale);
131 } elsif ($^O eq 'VMS'
132 && defined($ENV{'SYS$I18N_LOCALE'})
133 && -d 'SYS$I18N_LOCALE')
135 # The SYS$I18N_LOCALE logical name search list was not present on
136 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
137 opendir(LOCALES, "SYS\$I18N_LOCALE:");
138 while ($_ = readdir(LOCALES)) {
140 _trylocale($_, $categories, \@Locale);
143 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
145 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
146 # is much easier and faster than the last resort method.
148 opendir(LOCALES, '/usr/share/locale');
149 while ($_ = readdir(LOCALES)) {
151 _trylocale($_, $categories, \@Locale);
154 } else { # Final fallback. Try our list of locales hard-coded here
156 # This is going to be slow.
160 # Locales whose name differs if the utf8 bit is on are stored in these two
161 # files with appropriate encodings.
162 if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) {
163 @Data = do "lib/locale/utf8";
165 @Data = do "lib/locale/latin1";
168 # The rest of the locales are in this file.
171 foreach my $line (@Data) {
172 my ($locale_name, $language_codes, $country_codes, $encodings) =
174 my @enc = _decode_encodings($encodings);
175 foreach my $loc (split(/ /, $locale_name)) {
176 _trylocale($loc, $categories, \@Locale);
177 foreach my $enc (@enc) {
178 _trylocale("$loc.$enc", $categories, \@Locale);
181 foreach my $enc (@enc) {
182 _trylocale("$loc.$enc", $categories, \@Locale);
185 foreach my $lang (split(/ /, $language_codes)) {
186 _trylocale($lang, $categories, \@Locale);
187 foreach my $country (split(/ /, $country_codes)) {
188 my $lc = "${lang}_${country}";
189 _trylocale($lc, $categories, \@Locale);
190 foreach my $enc (@enc) {
191 _trylocale("$lc.$enc", $categories, \@Locale);
193 my $lC = "${lang}_\U${country}";
194 _trylocale($lC, $categories, \@Locale);
195 foreach my $enc (@enc) {
196 _trylocale("$lC.$enc", $categories, \@Locale);
203 @Locale = sort @Locale;
210 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
216 my $save_locale = setlocale(&POSIX::LC_CTYPE());
217 if (! $save_locale) {
218 ok(0, "Verify could save previous locale");
222 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
223 ok(0, "Verify could setlocale to $locale");
229 # Use an op that gives different results for UTF-8 than any other locale.
230 # If a platform has UTF-8 locales, there should be at least one locale on
231 # most platforms with UTF-8 in its name, so if there is a bug in the op
232 # giving a false negative, we should get a failure for those locales as we
233 # go through testing all the locales on the platform.
234 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
235 if ($locale =~ /UTF-?8/i) {
236 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
243 die "Couldn't restore locale '$save_locale'"
244 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
249 sub find_utf8_ctype_locale (;$) { # Return the name of locale that core Perl
250 # thinks is a UTF-8 LC_CTYPE locale.
251 # Optional parameter is a reference to a
252 # list of locales to try; if omitted, this
253 # tries all locales it can find on the
255 my $locales_ref = shift;
256 return if !defined &POSIX::LC_CTYPE;
257 if (! defined $locales_ref) {
258 my @locales = find_locales(&POSIX::LC_CTYPE());
259 $locales_ref = \@locales;
262 foreach my $locale (@$locales_ref) {
263 return $locale if is_locale_utf8($locale);
271 # Format of data is: locale_name, language_codes, country_codes, encodings
274 Arabic:ar:dz eg sa:6 arabic8
275 Brezhoneg Breton:br:fr:1 15
276 Bulgarski Bulgarian:bg:bg:5
277 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
278 Hrvatski Croatian:hr:hr:2
279 Cymraeg Welsh:cy:cy:1 14 15
281 Dansk Danish:da:dk:1 15
282 Nederlands Dutch:nl:be nl:1 15
283 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
285 Eesti Estonian:et:ee:4 6 13
286 Suomi Finnish:fi:fi:1 15
288 Deutsch German:de:at be ch de lu:1 15
289 Euskaraz Basque:eu:es fr:1 15
290 Galego Galician:gl:es:1 15
291 Ellada Greek:el:gr:7 g8
293 Greenlandic:kl:gl:4 6
294 Hebrew:iw:il:8 hebrew8
296 Indonesian:id:id:1 15
297 Gaeilge Irish:ga:IE:1 14 15
298 Italiano Italian:it:ch it:1 15
299 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
301 Latine Latin:la:va:1 15
303 Lithuanian:lt:lt:4 6 13
304 Macedonian:mk:mk:1 15
307 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
309 Polski Polish:pl:pl:2
311 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
312 Serbski Serbian:sr:yu:5
314 Slovene Slovenian:sl:si:2
315 Sqhip Albanian:sq:sq:1 15
316 Svenska Swedish:sv:fi se:1 15
318 Turkish:tr:tr:9 turkish8