1 /* Set the current locale. -*- coding: utf-8 -*-
2 Copyright (C) 2009, 2011-2019 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
21 /* Override setlocale() so that when the default locale is requested
22 (locale = ""), the environment variables LC_ALL, LC_*, and LANG are
24 Also include all the functionality from libintl's setlocale() override. */
26 /* Please keep this file in sync with
27 gettext/gettext-runtime/intl/setlocale.c ! */
36 #include "localename.h"
38 #if HAVE_CFLOCALECOPYPREFERREDLANGUAGES || HAVE_CFPREFERENCESCOPYAPPVALUE
39 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES
40 # include <CoreFoundation/CFLocale.h>
41 # elif HAVE_CFPREFERENCESCOPYAPPVALUE
42 # include <CoreFoundation/CFPreferences.h>
44 # include <CoreFoundation/CFPropertyList.h>
45 # include <CoreFoundation/CFArray.h>
46 # include <CoreFoundation/CFString.h>
47 extern void gl_locale_name_canonicalize (char *name
);
54 /* Return string representation of locale category CATEGORY. */
56 category_to_name (int category
)
63 retval
= "LC_COLLATE";
69 retval
= "LC_MONETARY";
72 retval
= "LC_NUMERIC";
78 retval
= "LC_MESSAGES";
81 /* If you have a better idea for a default value let me know. */
88 # if defined _WIN32 && ! defined __CYGWIN__
90 /* The native Windows setlocale() function expects locale names of the form
91 "German" or "German_Germany" or "DEU", but not "de" or "de_DE". We need
92 to convert the names from the form with ISO 639 language code and ISO 3166
93 country code to the form with English names or with three-letter identifier.
94 The three-letter identifiers known by a Windows XP SP2 or SP3 are:
95 AFK Afrikaans_South Africa.1252
96 ARA Arabic_Saudi Arabia.1256
97 ARB Arabic_Lebanon.1256
99 ARG Arabic_Algeria.1256
100 ARH Arabic_Bahrain.1256
102 ARJ Arabic_Jordan.1256
103 ARK Arabic_Kuwait.1256
104 ARL Arabic_Libya.1256
105 ARM Arabic_Morocco.1256
107 ARQ Arabic_Qatar.1256
108 ARS Arabic_Syria.1256
109 ART Arabic_Tunisia.1256
110 ARU Arabic_U.A.E..1256
111 ARY Arabic_Yemen.1256
112 AZE Azeri (Latin)_Azerbaijan.1254
113 BEL Belarusian_Belarus.1251
114 BGR Bulgarian_Bulgaria.1251
115 BSB Bosnian_Bosnia and Herzegovina.1250
116 BSC Bosnian (Cyrillic)_Bosnia and Herzegovina.1250 (wrong encoding!)
117 CAT Catalan_Spain.1252
118 CHH Chinese_Hong Kong S.A.R..950
119 CHI Chinese_Singapore.936
120 CHS Chinese_People's Republic of China.936
121 CHT Chinese_Taiwan.950
122 CSY Czech_Czech Republic.1250
123 CYM Welsh_United Kingdom.1252
124 DAN Danish_Denmark.1252
125 DEA German_Austria.1252
126 DEC German_Liechtenstein.1252
127 DEL German_Luxembourg.1252
128 DES German_Switzerland.1252
129 DEU German_Germany.1252
130 ELL Greek_Greece.1253
131 ENA English_Australia.1252
132 ENB English_Caribbean.1252
133 ENC English_Canada.1252
134 ENG English_United Kingdom.1252
135 ENI English_Ireland.1252
136 ENJ English_Jamaica.1252
137 ENL English_Belize.1252
138 ENP English_Republic of the Philippines.1252
139 ENS English_South Africa.1252
140 ENT English_Trinidad and Tobago.1252
141 ENU English_United States.1252
142 ENW English_Zimbabwe.1252
143 ENZ English_New Zealand.1252
144 ESA Spanish_Panama.1252
145 ESB Spanish_Bolivia.1252
146 ESC Spanish_Costa Rica.1252
147 ESD Spanish_Dominican Republic.1252
148 ESE Spanish_El Salvador.1252
149 ESF Spanish_Ecuador.1252
150 ESG Spanish_Guatemala.1252
151 ESH Spanish_Honduras.1252
152 ESI Spanish_Nicaragua.1252
153 ESL Spanish_Chile.1252
154 ESM Spanish_Mexico.1252
155 ESN Spanish_Spain.1252
156 ESO Spanish_Colombia.1252
157 ESP Spanish_Spain.1252
158 ESR Spanish_Peru.1252
159 ESS Spanish_Argentina.1252
160 ESU Spanish_Puerto Rico.1252
161 ESV Spanish_Venezuela.1252
162 ESY Spanish_Uruguay.1252
163 ESZ Spanish_Paraguay.1252
164 ETI Estonian_Estonia.1257
165 EUQ Basque_Spain.1252
167 FIN Finnish_Finland.1252
168 FOS Faroese_Faroe Islands.1252
169 FPO Filipino_Philippines.1252
170 FRA French_France.1252
171 FRB French_Belgium.1252
172 FRC French_Canada.1252
173 FRL French_Luxembourg.1252
174 FRM French_Principality of Monaco.1252
175 FRS French_Switzerland.1252
176 FYN Frisian_Netherlands.1252
177 GLC Galician_Spain.1252
178 HEB Hebrew_Israel.1255
179 HRB Croatian_Bosnia and Herzegovina.1250
180 HRV Croatian_Croatia.1250
181 HUN Hungarian_Hungary.1250
182 IND Indonesian_Indonesia.1252
183 IRE Irish_Ireland.1252
184 ISL Icelandic_Iceland.1252
185 ITA Italian_Italy.1252
186 ITS Italian_Switzerland.1252
187 IUK Inuktitut (Latin)_Canada.1252
188 JPN Japanese_Japan.932
189 KKZ Kazakh_Kazakhstan.1251
191 KYR Kyrgyz_Kyrgyzstan.1251
192 LBX Luxembourgish_Luxembourg.1252
193 LTH Lithuanian_Lithuania.1257
194 LVI Latvian_Latvia.1257
195 MKI FYRO Macedonian_Former Yugoslav Republic of Macedonia.1251
196 MON Mongolian_Mongolia.1251
197 MPD Mapudungun_Chile.1252
198 MSB Malay_Brunei Darussalam.1252
199 MSL Malay_Malaysia.1252
200 MWK Mohawk_Canada.1252
201 NLB Dutch_Belgium.1252
202 NLD Dutch_Netherlands.1252
203 NON Norwegian-Nynorsk_Norway.1252
204 NOR Norwegian (Bokmål)_Norway.1252
205 NSO Northern Sotho_South Africa.1252
206 PLK Polish_Poland.1250
207 PTB Portuguese_Brazil.1252
208 PTG Portuguese_Portugal.1252
209 QUB Quechua_Bolivia.1252
210 QUE Quechua_Ecuador.1252
211 QUP Quechua_Peru.1252
212 RMC Romansh_Switzerland.1252
213 ROM Romanian_Romania.1250
214 RUS Russian_Russia.1251
215 SKY Slovak_Slovakia.1250
216 SLV Slovenian_Slovenia.1250
217 SMA Sami (Southern)_Norway.1252
218 SMB Sami (Southern)_Sweden.1252
219 SME Sami (Northern)_Norway.1252
220 SMF Sami (Northern)_Sweden.1252
221 SMG Sami (Northern)_Finland.1252
222 SMJ Sami (Lule)_Norway.1252
223 SMK Sami (Lule)_Sweden.1252
224 SMN Sami (Inari)_Finland.1252
225 SMS Sami (Skolt)_Finland.1252
226 SQI Albanian_Albania.1250
227 SRB Serbian (Cyrillic)_Serbia and Montenegro.1251
228 SRL Serbian (Latin)_Serbia and Montenegro.1250
229 SRN Serbian (Cyrillic)_Bosnia and Herzegovina.1251
230 SRS Serbian (Latin)_Bosnia and Herzegovina.1250
231 SVE Swedish_Sweden.1252
232 SVF Swedish_Finland.1252
233 SWK Swahili_Kenya.1252
234 THA Thai_Thailand.874
235 TRK Turkish_Turkey.1254
236 TSN Tswana_South Africa.1252
237 TTT Tatar_Russia.1251
238 UKR Ukrainian_Ukraine.1251
239 URD Urdu_Islamic Republic of Pakistan.1256
240 USA English_United States.1252
241 UZB Uzbek (Latin)_Uzbekistan.1254
242 VIT Vietnamese_Viet Nam.1258
243 XHO Xhosa_South Africa.1252
244 ZHH Chinese_Hong Kong S.A.R..950
245 ZHI Chinese_Singapore.936
246 ZHM Chinese_Macau S.A.R..950
247 ZUL Zulu_South Africa.1252
250 /* Table from ISO 639 language code, optionally with country or script suffix,
252 Keep in sync with the gl_locale_name_from_win32_LANGID function in
259 static const struct table_entry language_table
[] =
261 { "af", "Afrikaans" },
264 { "arn", "Mapudungun" },
265 { "as", "Assamese" },
266 { "az@cyrillic", "Azeri (Cyrillic)" },
267 { "az@latin", "Azeri (Latin)" },
269 { "be", "Belarusian" },
270 { "ber", "Tamazight" },
271 { "ber@arabic", "Tamazight (Arabic)" },
272 { "ber@latin", "Tamazight (Latin)" },
273 { "bg", "Bulgarian" },
276 { "bn_BD", "Bengali (Bangladesh)" },
277 { "bn_IN", "Bengali (India)" },
281 { "bs", "BSB" }, /* "Bosnian (Latin)" */
282 { "bs@cyrillic", "BSC" }, /* Bosnian (Cyrillic) */
284 { "chr", "Cherokee" },
285 { "co", "Corsican" },
286 { "cpe", "Hawaiian" },
291 { "dsb", "Lower Sorbian" },
296 { "et", "Estonian" },
299 { "ff", "Fulfulde" },
301 { "fo", "Faroese" }, /* "Faeroese" does not work */
304 { "ga", "IRE" }, /* Gaelic (Ireland) */
305 { "gd", "Gaelic (Scotland)" },
306 { "gd", "Scottish Gaelic" },
307 { "gl", "Galician" },
309 { "gsw", "Alsatian" },
310 { "gu", "Gujarati" },
314 { "hr", "Croatian" },
315 { "hsb", "Upper Sorbian" },
316 { "hu", "Hungarian" },
317 { "hy", "Armenian" },
318 { "id", "Indonesian" },
321 { "is", "Icelandic" },
323 { "iu", "IUK" }, /* Inuktitut */
324 { "ja", "Japanese" },
325 { "ka", "Georgian" },
327 { "kl", "Greenlandic" },
328 { "km", "Cambodian" },
332 { "kok", "Konkani" },
334 { "ks", "Kashmiri" },
335 { "ks_IN", "Kashmiri_India" },
336 { "ks_PK", "Kashmiri (Arabic)_Pakistan" },
339 { "lb", "Luxembourgish" },
341 { "lt", "Lithuanian" },
344 { "mk", "FYRO Macedonian" },
345 { "mk", "Macedonian" },
346 { "ml", "Malayalam" },
347 { "mn", "Mongolian" },
348 { "mni", "Manipuri" },
354 { "nb", "NOR" }, /* Norwegian Bokmål */
358 { "nn", "NON" }, /* Norwegian Nynorsk */
359 { "no", "Norwegian" },
360 { "nso", "Northern Sotho" },
366 { "pap", "Papiamentu" },
370 { "pt", "Portuguese" },
372 { "qut", "K'iche'" },
374 { "ro", "Romanian" },
376 { "rw", "Kinyarwanda" },
377 { "sa", "Sanskrit" },
380 { "se", "Sami (Northern)" },
381 { "se", "Northern Sami" },
382 { "si", "Sinhalese" },
384 { "sl", "Slovenian" },
385 { "sma", "Sami (Southern)" },
386 { "sma", "Southern Sami" },
387 { "smj", "Sami (Lule)" },
388 { "smj", "Lule Sami" },
389 { "smn", "Sami (Inari)" },
390 { "smn", "Inari Sami" },
391 { "sms", "Sami (Skolt)" },
392 { "sms", "Skolt Sami" },
394 { "sq", "Albanian" },
395 { "sr", "Serbian (Latin)" },
396 { "sr@cyrillic", "SRB" }, /* Serbian (Cyrillic) */
404 { "ti", "Tigrinya" },
406 { "tl", "Filipino" },
412 { "uk", "Ukrainian" },
415 { "uz", "Uzbek (Latin)" },
416 { "uz@cyrillic", "Uzbek (Cyrillic)" },
418 { "vi", "Vietnamese" },
419 { "wen", "Sorbian" },
428 /* Table from ISO 3166 country code to English name.
429 Keep in sync with the gl_locale_name_from_win32_LANGID function in
431 static const struct table_entry country_table
[] =
434 { "AF", "Afghanistan" },
437 { "AN", "Netherlands Antilles" },
438 { "AR", "Argentina" },
440 { "AU", "Australia" },
441 { "AZ", "Azerbaijan" },
442 { "BA", "Bosnia and Herzegovina" },
443 { "BD", "Bangladesh" },
445 { "BG", "Bulgaria" },
447 { "BN", "Brunei Darussalam" },
455 { "CH", "Switzerland" },
456 { "CI", "Cote d'Ivoire" },
458 { "CM", "Cameroon" },
459 { "CN", "People's Republic of China" },
460 { "CO", "Colombia" },
461 { "CR", "Costa Rica" },
462 { "CS", "Serbia and Montenegro" },
463 { "CZ", "Czech Republic" },
466 { "DO", "Dominican Republic" },
473 { "ET", "Ethiopia" },
475 { "FO", "Faroe Islands" },
477 { "GB", "United Kingdom" },
478 { "GD", "Caribbean" },
480 { "GL", "Greenland" },
482 { "GT", "Guatemala" },
483 { "HK", "Hong Kong" },
484 { "HK", "Hong Kong S.A.R." },
485 { "HN", "Honduras" },
489 { "ID", "Indonesia" },
501 { "KG", "Kyrgyzstan" },
502 { "KH", "Cambodia" },
503 { "KR", "South Korea" },
505 { "KZ", "Kazakhstan" },
508 { "LI", "Liechtenstein" },
509 { "LK", "Sri Lanka" },
510 { "LT", "Lithuania" },
511 { "LU", "Luxembourg" },
515 { "MC", "Principality of Monaco" },
518 { "ME", "Montenegro" },
519 { "MK", "Former Yugoslav Republic of Macedonia" },
522 { "MN", "Mongolia" },
523 { "MO", "Macau S.A.R." },
525 { "MV", "Maldives" },
527 { "MY", "Malaysia" },
529 { "NI", "Nicaragua" },
530 { "NL", "Netherlands" },
533 { "NZ", "New Zealand" },
537 { "PH", "Philippines" },
538 { "PK", "Islamic Republic of Pakistan" },
540 { "PR", "Puerto Rico" },
541 { "PT", "Portugal" },
542 { "PY", "Paraguay" },
549 { "SA", "Saudi Arabia" },
551 { "SG", "Singapore" },
552 { "SI", "Slovenia" },
556 { "SR", "Suriname" },
557 { "SV", "El Salvador" },
559 { "TH", "Thailand" },
560 { "TJ", "Tajikistan" },
561 { "TM", "Turkmenistan" },
564 { "TT", "Trinidad and Tobago" },
566 { "TZ", "Tanzania" },
568 { "US", "United States" },
571 { "VE", "Venezuela" },
572 { "VN", "Viet Nam" },
574 { "ZA", "South Africa" },
578 /* Given a string STRING, find the set of indices i such that TABLE[i].code is
579 the given STRING. It is a range [lo,hi-1]. */
580 typedef struct { size_t lo
; size_t hi
; } range_t
;
582 search (const struct table_entry
*table
, size_t table_size
, const char *string
,
585 /* The table is sorted. Perform a binary search. */
586 size_t hi
= table_size
;
591 for i < lo, strcmp (table[i].code, string) < 0,
592 for i >= hi, strcmp (table[i].code, string) > 0. */
593 size_t mid
= (hi
+ lo
) >> 1; /* >= lo, < hi */
594 int cmp
= strcmp (table
[mid
].code
, string
);
602 strcmp (language_table[i].code, string) == 0.
603 Find the entire interval of such i. */
607 for (i
= mid
; i
> lo
; )
610 if (strcmp (table
[i
].code
, string
) < 0)
620 for (i
= mid
; i
< hi
; i
++)
622 if (strcmp (table
[i
].code
, string
) > 0)
630 strcmp (language_table[i].code, string) == 0
631 is the interval [lo, hi-1]. */
639 /* Like setlocale, but accept also locale names in the form ll or ll_CC,
640 where ll is an ISO 639 language code and CC is an ISO 3166 country code. */
642 setlocale_unixlike (int category
, const char *locale
)
649 /* The native Windows implementation of setlocale understands the special
650 locale name "C", but not "POSIX". Therefore map "POSIX" to "C". */
651 if (locale
!= NULL
&& strcmp (locale
, "POSIX") == 0)
654 /* First, try setlocale with the original argument unchanged. */
655 result
= setlocale (category
, locale
);
659 /* Otherwise, assume the argument is in the form
660 language[_territory][.codeset][@modifier]
661 and try to map it using the tables. */
662 if (strlen (locale
) < sizeof (llCC_buf
))
664 /* Second try: Remove the codeset part. */
666 const char *p
= locale
;
669 /* Copy the part before the dot. */
670 for (; *p
!= '\0' && *p
!= '.'; p
++, q
++)
673 /* Skip the part up to the '@', if any. */
674 for (; *p
!= '\0' && *p
!= '@'; p
++)
676 /* Copy the part starting with '@', if any. */
677 for (; *p
!= '\0'; p
++, q
++)
681 /* llCC_buf now contains
682 language[_territory][@modifier]
684 if (strcmp (llCC_buf
, locale
) != 0)
686 result
= setlocale (category
, llCC_buf
);
690 /* Look it up in language_table. */
695 search (language_table
,
696 sizeof (language_table
) / sizeof (language_table
[0]),
700 for (i
= range
.lo
; i
< range
.hi
; i
++)
702 /* Try the replacement in language_table[i]. */
703 result
= setlocale (category
, language_table
[i
].english
);
708 /* Split language[_territory][@modifier]
709 into ll_buf = language[@modifier]
710 and CC_buf = territory
713 const char *underscore
= strchr (llCC_buf
, '_');
714 if (underscore
!= NULL
)
716 const char *territory_start
= underscore
+ 1;
717 const char *territory_end
= strchr (territory_start
, '@');
718 if (territory_end
== NULL
)
719 territory_end
= territory_start
+ strlen (territory_start
);
721 memcpy (ll_buf
, llCC_buf
, underscore
- llCC_buf
);
722 strcpy (ll_buf
+ (underscore
- llCC_buf
), territory_end
);
724 memcpy (CC_buf
, territory_start
, territory_end
- territory_start
);
725 CC_buf
[territory_end
- territory_start
] = '\0';
728 /* Look up ll_buf in language_table
729 and CC_buf in country_table. */
730 range_t language_range
;
732 search (language_table
,
733 sizeof (language_table
) / sizeof (language_table
[0]),
736 if (language_range
.lo
< language_range
.hi
)
738 range_t country_range
;
740 search (country_table
,
741 sizeof (country_table
) / sizeof (country_table
[0]),
744 if (country_range
.lo
< country_range
.hi
)
749 for (i
= language_range
.lo
; i
< language_range
.hi
; i
++)
750 for (j
= country_range
.lo
; j
< country_range
.hi
; j
++)
752 /* Concatenate the replacements. */
753 const char *part1
= language_table
[i
].english
;
754 size_t part1_len
= strlen (part1
);
755 const char *part2
= country_table
[j
].english
;
756 size_t part2_len
= strlen (part2
) + 1;
759 if (!(part1_len
+ 1 + part2_len
<= sizeof (buf
)))
761 memcpy (buf
, part1
, part1_len
);
762 buf
[part1_len
] = '_';
763 memcpy (buf
+ part1_len
+ 1, part2
, part2_len
);
765 /* Try the concatenated replacements. */
766 result
= setlocale (category
, buf
);
772 /* Try omitting the country entirely. This may set a locale
773 corresponding to the wrong country, but is better than
778 for (i
= language_range
.lo
; i
< language_range
.hi
; i
++)
780 /* Try only the language replacement. */
782 setlocale (category
, language_table
[i
].english
);
797 # elif defined __ANDROID__
799 /* Like setlocale, but accept also the locale names "C" and "POSIX". */
801 setlocale_unixlike (int category
, const char *locale
)
803 char *result
= setlocale (category
, locale
);
820 || strcmp (locale
, "C") == 0 || strcmp (locale
, "POSIX") == 0)
821 result
= (char *) "C";
828 # define setlocale setlocale_unixlike
831 # define setlocale_unixlike setlocale
834 # if LC_MESSAGES == 1729
836 /* The system does not store an LC_MESSAGES locale category. Do it here. */
837 static char lc_messages_name
[64] = "C";
839 /* Like setlocale, but support also LC_MESSAGES. */
841 setlocale_single (int category
, const char *locale
)
843 if (category
== LC_MESSAGES
)
847 lc_messages_name
[sizeof (lc_messages_name
) - 1] = '\0';
848 strncpy (lc_messages_name
, locale
, sizeof (lc_messages_name
) - 1);
850 return lc_messages_name
;
853 return setlocale_unixlike (category
, locale
);
857 # define setlocale_single setlocale_unixlike
861 rpl_setlocale (int category
, const char *locale
)
863 if (locale
!= NULL
&& locale
[0] == '\0')
865 /* A request to the set the current locale to the default locale. */
866 if (category
== LC_ALL
)
868 /* Set LC_CTYPE first. Then the other categories. */
869 static int const categories
[] =
879 const char *base_name
;
882 /* Back up the old locale, in case one of the steps fails. */
883 saved_locale
= setlocale (LC_ALL
, NULL
);
884 if (saved_locale
== NULL
)
886 saved_locale
= strdup (saved_locale
);
887 if (saved_locale
== NULL
)
890 /* Set LC_CTYPE category. Set all other categories (except possibly
891 LC_MESSAGES) to the same value in the same call; this is likely to
894 gl_locale_name_environ (LC_CTYPE
, category_to_name (LC_CTYPE
));
895 if (base_name
== NULL
)
896 base_name
= gl_locale_name_default ();
898 if (setlocale_unixlike (LC_ALL
, base_name
) != NULL
)
900 /* LC_CTYPE category already set. */
905 /* On Mac OS X, "UTF-8" is a valid locale name for LC_CTYPE but
906 not for LC_ALL. Therefore this call may fail. So, try
907 another base_name. */
909 if (setlocale_unixlike (LC_ALL
, base_name
) == NULL
)
913 # if defined _WIN32 && ! defined __CYGWIN__
914 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
915 LC_CTYPE category to an invalid value ("C") when it does not
916 support the specified encoding. Report a failure instead. */
917 if (strchr (base_name
, '.') != NULL
918 && strcmp (setlocale (LC_CTYPE
, NULL
), "C") == 0)
922 for (; i
< sizeof (categories
) / sizeof (categories
[0]); i
++)
924 int cat
= categories
[i
];
927 name
= gl_locale_name_environ (cat
, category_to_name (cat
));
929 name
= gl_locale_name_default ();
931 /* If name is the same as base_name, it has already been set
932 through the setlocale call before the loop. */
933 if (strcmp (name
, base_name
) != 0
934 # if LC_MESSAGES == 1729
935 || cat
== LC_MESSAGES
938 if (setlocale_single (cat
, name
) == NULL
)
939 # if defined __APPLE__ && defined __MACH__
941 /* On Mac OS X 10.13, some locales can be set through
942 System Preferences > Language & Region, that are not
943 supported by libc. The system's setlocale() falls
944 back to "C" for these locale categories. We can possibly
945 do better. If we can't, print a warning, to limit user
950 warn
= (setlocale_single (cat
, "UTF-8") == NULL
);
951 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES || HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
952 else if (cat
== LC_MESSAGES
)
954 /* Take the primary language preference. */
955 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES /* MacOS X 10.5 or newer */
956 CFArrayRef prefArray
= CFLocaleCopyPreferredLanguages ();
957 # elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
958 CFTypeRef preferences
=
959 CFPreferencesCopyAppValue (CFSTR ("AppleLanguages"),
960 kCFPreferencesCurrentApplication
);
961 if (preferences
!= NULL
962 && CFGetTypeID (preferences
) == CFArrayGetTypeID ())
964 CFArrayRef prefArray
= (CFArrayRef
)preferences
;
966 int n
= CFArrayGetCount (prefArray
);
970 CFTypeRef element
= CFArrayGetValueAtIndex (prefArray
, 0);
972 && CFGetTypeID (element
) == CFStringGetTypeID ()
973 && CFStringGetCString ((CFStringRef
)element
,
975 kCFStringEncodingASCII
))
977 /* Remove the country.
978 E.g. "zh-Hans-DE" -> "zh-Hans". */
979 char *last_minus
= strrchr (buf
, '-');
980 if (last_minus
!= NULL
)
983 /* Convert to Unix locale name.
984 E.g. "zh-Hans" -> "zh_CN". */
985 gl_locale_name_canonicalize (buf
);
987 /* Try setlocale with this value. */
988 warn
= (setlocale_single (cat
, buf
) == NULL
);
991 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES /* MacOS X 10.5 or newer */
992 CFRelease (prefArray
);
993 # elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
998 /* No fallback possible for LC_NUMERIC. The application
999 should use the locale properties
1000 kCFLocaleDecimalSeparator, kCFLocaleGroupingSeparator.
1001 No fallback possible for LC_TIME. The application should
1002 use the locale property kCFLocaleCalendarIdentifier.
1003 No fallback possible for LC_COLLATE. The application
1004 should use the locale properties
1005 kCFLocaleCollationIdentifier, kCFLocaleCollatorIdentifier.
1006 No fallback possible for LC_MONETARY. The application
1007 should use the locale properties
1008 kCFLocaleCurrencySymbol, kCFLocaleCurrencyCode. */
1012 "Warning: Failed to set locale category %s to %s.\n",
1013 category_to_name (cat
), name
);
1020 /* All steps were successful. */
1021 free (saved_locale
);
1022 return setlocale (LC_ALL
, NULL
);
1025 if (saved_locale
[0] != '\0') /* don't risk an endless recursion */
1026 setlocale (LC_ALL
, saved_locale
);
1027 free (saved_locale
);
1033 gl_locale_name_environ (category
, category_to_name (category
));
1035 name
= gl_locale_name_default ();
1037 return setlocale_single (category
, name
);
1042 # if defined _WIN32 && ! defined __CYGWIN__
1043 if (category
== LC_ALL
&& locale
!= NULL
&& strchr (locale
, '.') != NULL
)
1047 /* Back up the old locale. */
1048 saved_locale
= setlocale (LC_ALL
, NULL
);
1049 if (saved_locale
== NULL
)
1051 saved_locale
= strdup (saved_locale
);
1052 if (saved_locale
== NULL
)
1055 if (setlocale_unixlike (LC_ALL
, locale
) == NULL
)
1057 free (saved_locale
);
1061 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
1062 LC_CTYPE category to an invalid value ("C") when it does not
1063 support the specified encoding. Report a failure instead. */
1064 if (strcmp (setlocale (LC_CTYPE
, NULL
), "C") == 0)
1066 if (saved_locale
[0] != '\0') /* don't risk an endless recursion */
1067 setlocale (LC_ALL
, saved_locale
);
1068 free (saved_locale
);
1072 /* It was really successful. */
1073 free (saved_locale
);
1074 return setlocale (LC_ALL
, NULL
);
1078 return setlocale_single (category
, locale
);