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 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 them
71 my $categories = shift;
74 my $have_setlocale = $Config{d_setlocale};
76 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
77 # and mingw32 uses said silly CRT
78 # This doesn't seem to be an issue any more, at least on Windows XP,
79 # so re-enable the tests for Windows XP onwards.
80 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
81 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
82 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
83 $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
85 # UWIN seems to loop after taint tests, just skip for now
86 $have_setlocale = 0 if ($^O =~ /^uwin/);
88 return unless $have_setlocale;
90 # Done this way in case this is 'required' in the caller before seeing if
92 require POSIX; import POSIX 'locale_h';
94 _trylocale("C", $categories, \@Locale);
95 _trylocale("POSIX", $categories, \@Locale);
97 _trylocale("ISO8859-$_", $categories, \@Locale);
98 _trylocale("iso8859$_", $categories, \@Locale);
99 _trylocale("iso8859-$_", $categories, \@Locale);
100 _trylocale("iso_8859_$_", $categories, \@Locale);
101 _trylocale("isolatin$_", $categories, \@Locale);
102 _trylocale("isolatin-$_", $categories, \@Locale);
103 _trylocale("iso_latin_$_", $categories, \@Locale);
106 # Sanitize the environment so that we can run the external 'locale'
107 # program without the taint mode getting grumpy.
109 # $ENV{PATH} is special in VMS.
110 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
112 # Other subversive stuff.
113 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
115 if (-x "/usr/bin/locale"
116 && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|"))
119 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
120 # ain't great when we're running this testPERL_UNICODE= so that utf8
121 # locales will cause all IO hadles to default to (assume) utf8
122 next unless utf8::valid($_);
124 _trylocale($_, $categories, \@Locale);
127 } elsif ($^O eq 'VMS'
128 && defined($ENV{'SYS$I18N_LOCALE'})
129 && -d 'SYS$I18N_LOCALE')
131 # The SYS$I18N_LOCALE logical name search list was not present on
132 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
133 opendir(LOCALES, "SYS\$I18N_LOCALE:");
134 while ($_ = readdir(LOCALES)) {
136 _trylocale($_, $categories, \@Locale);
139 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
141 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
142 # is much easier and faster than the last resort method.
144 opendir(LOCALES, '/usr/share/locale');
145 while ($_ = readdir(LOCALES)) {
147 _trylocale($_, $categories, \@Locale);
150 } else { # Final fallback. Try our list of locales hard-coded here
152 # This is going to be slow.
156 # Locales whose name differs if the utf8 bit is on are stored in these two
157 # files with appropriate encodings.
158 if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) {
159 @Data = do "lib/locale/utf8";
161 @Data = do "lib/locale/latin1";
164 # The rest of the locales are in this file.
167 foreach my $line (@Data) {
168 my ($locale_name, $language_codes, $country_codes, $encodings) =
170 my @enc = _decode_encodings($encodings);
171 foreach my $loc (split(/ /, $locale_name)) {
172 _trylocale($loc, $categories, \@Locale);
173 foreach my $enc (@enc) {
174 _trylocale("$loc.$enc", $categories, \@Locale);
177 foreach my $enc (@enc) {
178 _trylocale("$loc.$enc", $categories, \@Locale);
181 foreach my $lang (split(/ /, $language_codes)) {
182 _trylocale($lang, $categories, \@Locale);
183 foreach my $country (split(/ /, $country_codes)) {
184 my $lc = "${lang}_${country}";
185 _trylocale($lc, $categories, \@Locale);
186 foreach my $enc (@enc) {
187 _trylocale("$lc.$enc", $categories, \@Locale);
189 my $lC = "${lang}_\U${country}";
190 _trylocale($lC, $categories, \@Locale);
191 foreach my $enc (@enc) {
192 _trylocale("$lC.$enc", $categories, \@Locale);
199 @Locale = sort @Locale;
206 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
212 my $save_locale = setlocale(&POSIX::LC_CTYPE());
213 if (! $save_locale) {
214 ok(0, "Verify could save previous locale");
218 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
219 ok(0, "Verify could setlocale to $locale");
225 # Use an op that gives different results for UTF-8 than any other locale.
226 # If a platform has UTF-8 locales, there should be at least one locale on
227 # most platforms with UTF-8 in its name, so if there is a bug in the op
228 # giving a false negative, we should get a failure for those locales as we
229 # go through testing all the locales on the platform.
230 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
231 if ($locale =~ /UTF-?8/i) {
232 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
239 die "Couldn't restore locale '$save_locale'"
240 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
245 sub find_utf8_ctype_locale (;$) { # Return the name of locale that core Perl
246 # thinks is a UTF-8 LC_CTYPE locale.
247 # Optional parameter is a reference to a
248 # list of locales to try; if omitted, this
249 # tries all locales it can find on the
251 my $locales_ref = shift;
252 return if !defined &POSIX::LC_CTYPE;
253 if (! defined $locales_ref) {
254 my @locales = find_locales(&POSIX::LC_CTYPE());
255 $locales_ref = \@locales;
258 foreach my $locale (@$locales_ref) {
259 return $locale if is_locale_utf8($locale);
267 # Format of data is: locale_name, language_codes, country_codes, encodings
270 Arabic:ar:dz eg sa:6 arabic8
271 Brezhoneg Breton:br:fr:1 15
272 Bulgarski Bulgarian:bg:bg:5
273 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
274 Hrvatski Croatian:hr:hr:2
275 Cymraeg Welsh:cy:cy:1 14 15
277 Dansk Danish:da:dk:1 15
278 Nederlands Dutch:nl:be nl:1 15
279 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
281 Eesti Estonian:et:ee:4 6 13
282 Suomi Finnish:fi:fi:1 15
284 Deutsch German:de:at be ch de lu:1 15
285 Euskaraz Basque:eu:es fr:1 15
286 Galego Galician:gl:es:1 15
287 Ellada Greek:el:gr:7 g8
289 Greenlandic:kl:gl:4 6
290 Hebrew:iw:il:8 hebrew8
292 Indonesian:id:id:1 15
293 Gaeilge Irish:ga:IE:1 14 15
294 Italiano Italian:it:ch it:1 15
295 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
297 Latine Latin:la:va:1 15
299 Lithuanian:lt:lt:4 6 13
300 Macedonian:mk:mk:1 15
303 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
305 Polski Polish:pl:pl:2
307 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
308 Serbski Serbian:sr:yu:5
310 Slovene Slovenian:sl:si:2
311 Sqhip Albanian:sq:sq:1 15
312 Svenska Swedish:sv:fi se:1 15
314 Turkish:tr:tr:9 turkish8