Debian Codegraph @

lib/setlocale.c

raw
1 /* Set the current locale. -*- coding: utf-8 -*-
2 Copyright (C) 2009, 2011-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2009. */
18
19 #include <config.h>
20
21 /* Override setlocale() so that when the default locale is requested
22 (locale = ""), the environment variables LC_ALL, LC_*, and LANG are
23 considered.
24 Also include all the functionality from libintl's setlocale() override. */
25
26 /* Please keep this file in sync with
27 gettext/gettext-runtime/intl/setlocale.c ! */
28
29 /* Specification. */
30 #include <locale.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "setlocale-fixes.h"
37 #include "localename.h"
38
39 #if HAVE_CFLOCALECOPYPREFERREDLANGUAGES || HAVE_CFPREFERENCESCOPYAPPVALUE
40 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES
41 # include <CoreFoundation/CFLocale.h>
42 # elif HAVE_CFPREFERENCESCOPYAPPVALUE
43 # include <CoreFoundation/CFPreferences.h>
44 # endif
45 # include <CoreFoundation/CFPropertyList.h>
46 # include <CoreFoundation/CFArray.h>
47 # include <CoreFoundation/CFString.h>
48 extern void gl_locale_name_canonicalize (char *name);
49 #endif
50
51 #if defined _WIN32 && !defined __CYGWIN__
52 # define WIN32_LEAN_AND_MEAN
53 # include <windows.h>
54 #endif
55
56 #if 1
57
58 # undef setlocale
59
60 /* Which of the replacements to activate? */
61 # if NEED_SETLOCALE_IMPROVED
62 # define setlocale_improved rpl_setlocale
63 # elif NEED_SETLOCALE_MTSAFE
64 # define setlocale_mtsafe rpl_setlocale
65 # else
66 # error "This file should only be compiled if NEED_SETLOCALE_IMPROVED || NEED_SETLOCALE_MTSAFE."
67 # endif
68
69 /* Like setlocale, but guaranteed to be multithread-safe if LOCALE == NULL. */
70 # if !SETLOCALE_NULL_ALL_MTSAFE || !SETLOCALE_NULL_ONE_MTSAFE /* i.e. if NEED_SETLOCALE_MTSAFE */
71
72 # if NEED_SETLOCALE_IMPROVED
73 static
74 # endif
75 char *
76 setlocale_mtsafe (int category, const char *locale)
77 {
78 if (locale == NULL)
79 return (char *) setlocale_null (category);
80 else
81 return setlocale (category, locale);
82 }
83 # else /* !NEED_SETLOCALE_MTSAFE */
84
85 # define setlocale_mtsafe setlocale
86
87 # endif /* NEED_SETLOCALE_MTSAFE */
88
89 # if NEED_SETLOCALE_IMPROVED
90
91 /* Return string representation of locale category CATEGORY. */
92 static const char *
93 category_to_name (int category)
94 {
95 const char *retval;
96
97 switch (category)
98 {
99 case LC_COLLATE:
100 retval = "LC_COLLATE";
101 break;
102 case LC_CTYPE:
103 retval = "LC_CTYPE";
104 break;
105 case LC_MONETARY:
106 retval = "LC_MONETARY";
107 break;
108 case LC_NUMERIC:
109 retval = "LC_NUMERIC";
110 break;
111 case LC_TIME:
112 retval = "LC_TIME";
113 break;
114 case LC_MESSAGES:
115 retval = "LC_MESSAGES";
116 break;
117 default:
118 /* If you have a better idea for a default value let me know. */
119 retval = "LC_XXX";
120 }
121
122 return retval;
123 }
124
125 # if defined _WIN32 && ! defined __CYGWIN__
126
127 /* The native Windows setlocale() function expects locale names of the form
128 "German" or "German_Germany" or "DEU", but not "de" or "de_DE". We need
129 to convert the names from the form with ISO 639 language code and ISO 3166
130 country code to the form with English names or with three-letter identifier.
131 The three-letter identifiers known by a Windows XP SP2 or SP3 are:
132 AFK Afrikaans_South Africa.1252
133 ARA Arabic_Saudi Arabia.1256
134 ARB Arabic_Lebanon.1256
135 ARE Arabic_Egypt.1256
136 ARG Arabic_Algeria.1256
137 ARH Arabic_Bahrain.1256
138 ARI Arabic_Iraq.1256
139 ARJ Arabic_Jordan.1256
140 ARK Arabic_Kuwait.1256
141 ARL Arabic_Libya.1256
142 ARM Arabic_Morocco.1256
143 ARO Arabic_Oman.1256
144 ARQ Arabic_Qatar.1256
145 ARS Arabic_Syria.1256
146 ART Arabic_Tunisia.1256
147 ARU Arabic_U.A.E..1256
148 ARY Arabic_Yemen.1256
149 AZE Azeri (Latin)_Azerbaijan.1254
150 BEL Belarusian_Belarus.1251
151 BGR Bulgarian_Bulgaria.1251
152 BSB Bosnian_Bosnia and Herzegovina.1250
153 BSC Bosnian (Cyrillic)_Bosnia and Herzegovina.1250 (wrong encoding!)
154 CAT Catalan_Spain.1252
155 CHH Chinese_Hong Kong S.A.R..950
156 CHI Chinese_Singapore.936
157 CHS Chinese_People's Republic of China.936
158 CHT Chinese_Taiwan.950
159 CSY Czech_Czech Republic.1250
160 CYM Welsh_United Kingdom.1252
161 DAN Danish_Denmark.1252
162 DEA German_Austria.1252
163 DEC German_Liechtenstein.1252
164 DEL German_Luxembourg.1252
165 DES German_Switzerland.1252
166 DEU German_Germany.1252
167 ELL Greek_Greece.1253
168 ENA English_Australia.1252
169 ENB English_Caribbean.1252
170 ENC English_Canada.1252
171 ENG English_United Kingdom.1252
172 ENI English_Ireland.1252
173 ENJ English_Jamaica.1252
174 ENL English_Belize.1252
175 ENP English_Republic of the Philippines.1252
176 ENS English_South Africa.1252
177 ENT English_Trinidad and Tobago.1252
178 ENU English_United States.1252
179 ENW English_Zimbabwe.1252
180 ENZ English_New Zealand.1252
181 ESA Spanish_Panama.1252
182 ESB Spanish_Bolivia.1252
183 ESC Spanish_Costa Rica.1252
184 ESD Spanish_Dominican Republic.1252
185 ESE Spanish_El Salvador.1252
186 ESF Spanish_Ecuador.1252
187 ESG Spanish_Guatemala.1252
188 ESH Spanish_Honduras.1252
189 ESI Spanish_Nicaragua.1252
190 ESL Spanish_Chile.1252
191 ESM Spanish_Mexico.1252
192 ESN Spanish_Spain.1252
193 ESO Spanish_Colombia.1252
194 ESP Spanish_Spain.1252
195 ESR Spanish_Peru.1252
196 ESS Spanish_Argentina.1252
197 ESU Spanish_Puerto Rico.1252
198 ESV Spanish_Venezuela.1252
199 ESY Spanish_Uruguay.1252
200 ESZ Spanish_Paraguay.1252
201 ETI Estonian_Estonia.1257
202 EUQ Basque_Spain.1252
203 FAR Farsi_Iran.1256
204 FIN Finnish_Finland.1252
205 FOS Faroese_Faroe Islands.1252
206 FPO Filipino_Philippines.1252
207 FRA French_France.1252
208 FRB French_Belgium.1252
209 FRC French_Canada.1252
210 FRL French_Luxembourg.1252
211 FRM French_Principality of Monaco.1252
212 FRS French_Switzerland.1252
213 FYN Frisian_Netherlands.1252
214 GLC Galician_Spain.1252
215 HEB Hebrew_Israel.1255
216 HRB Croatian_Bosnia and Herzegovina.1250
217 HRV Croatian_Croatia.1250
218 HUN Hungarian_Hungary.1250
219 IND Indonesian_Indonesia.1252
220 IRE Irish_Ireland.1252
221 ISL Icelandic_Iceland.1252
222 ITA Italian_Italy.1252
223 ITS Italian_Switzerland.1252
224 IUK Inuktitut (Latin)_Canada.1252
225 JPN Japanese_Japan.932
226 KKZ Kazakh_Kazakhstan.1251
227 KOR Korean_Korea.949
228 KYR Kyrgyz_Kyrgyzstan.1251
229 LBX Luxembourgish_Luxembourg.1252
230 LTH Lithuanian_Lithuania.1257
231 LVI Latvian_Latvia.1257
232 MKI FYRO Macedonian_Former Yugoslav Republic of Macedonia.1251
233 MON Mongolian_Mongolia.1251
234 MPD Mapudungun_Chile.1252
235 MSB Malay_Brunei Darussalam.1252
236 MSL Malay_Malaysia.1252
237 MWK Mohawk_Canada.1252
238 NLB Dutch_Belgium.1252
239 NLD Dutch_Netherlands.1252
240 NON Norwegian-Nynorsk_Norway.1252
241 NOR Norwegian (Bokmål)_Norway.1252
242 NSO Northern Sotho_South Africa.1252
243 PLK Polish_Poland.1250
244 PTB Portuguese_Brazil.1252
245 PTG Portuguese_Portugal.1252
246 QUB Quechua_Bolivia.1252
247 QUE Quechua_Ecuador.1252
248 QUP Quechua_Peru.1252
249 RMC Romansh_Switzerland.1252
250 ROM Romanian_Romania.1250
251 RUS Russian_Russia.1251
252 SKY Slovak_Slovakia.1250
253 SLV Slovenian_Slovenia.1250
254 SMA Sami (Southern)_Norway.1252
255 SMB Sami (Southern)_Sweden.1252
256 SME Sami (Northern)_Norway.1252
257 SMF Sami (Northern)_Sweden.1252
258 SMG Sami (Northern)_Finland.1252
259 SMJ Sami (Lule)_Norway.1252
260 SMK Sami (Lule)_Sweden.1252
261 SMN Sami (Inari)_Finland.1252
262 SMS Sami (Skolt)_Finland.1252
263 SQI Albanian_Albania.1250
264 SRB Serbian (Cyrillic)_Serbia and Montenegro.1251
265 SRL Serbian (Latin)_Serbia and Montenegro.1250
266 SRN Serbian (Cyrillic)_Bosnia and Herzegovina.1251
267 SRS Serbian (Latin)_Bosnia and Herzegovina.1250
268 SVE Swedish_Sweden.1252
269 SVF Swedish_Finland.1252
270 SWK Swahili_Kenya.1252
271 THA Thai_Thailand.874
272 TRK Turkish_Turkey.1254
273 TSN Tswana_South Africa.1252
274 TTT Tatar_Russia.1251
275 UKR Ukrainian_Ukraine.1251
276 URD Urdu_Islamic Republic of Pakistan.1256
277 USA English_United States.1252
278 UZB Uzbek (Latin)_Uzbekistan.1254
279 VIT Vietnamese_Viet Nam.1258
280 XHO Xhosa_South Africa.1252
281 ZHH Chinese_Hong Kong S.A.R..950
282 ZHI Chinese_Singapore.936
283 ZHM Chinese_Macau S.A.R..950
284 ZUL Zulu_South Africa.1252
285 */
286
287 /* Table from ISO 639 language code, optionally with country or script suffix,
288 to English name.
289 Keep in sync with the gl_locale_name_from_win32_LANGID function in
290 localename.c! */
291 struct table_entry
292 {
293 const char *code;
294 const char *english;
295 };
296 static const struct table_entry language_table[] =
297 {
298 { "af", "Afrikaans" },
299 { "am", "Amharic" },
300 { "ar", "Arabic" },
301 { "arn", "Mapudungun" },
302 { "as", "Assamese" },
303 { "az@cyrillic", "Azeri (Cyrillic)" },
304 { "az@latin", "Azeri (Latin)" },
305 { "ba", "Bashkir" },
306 { "be", "Belarusian" },
307 { "ber", "Tamazight" },
308 { "ber@arabic", "Tamazight (Arabic)" },
309 { "ber@latin", "Tamazight (Latin)" },
310 { "bg", "Bulgarian" },
311 { "bin", "Edo" },
312 { "bn", "Bengali" },
313 { "bn_BD", "Bengali (Bangladesh)" },
314 { "bn_IN", "Bengali (India)" },
315 { "bnt", "Sutu" },
316 { "bo", "Tibetan" },
317 { "br", "Breton" },
318 { "bs", "BSB" }, /* "Bosnian (Latin)" */
319 { "bs@cyrillic", "BSC" }, /* Bosnian (Cyrillic) */
320 { "ca", "Catalan" },
321 { "chr", "Cherokee" },
322 { "co", "Corsican" },
323 { "cpe", "Hawaiian" },
324 { "cs", "Czech" },
325 { "cy", "Welsh" },
326 { "da", "Danish" },
327 { "de", "German" },
328 { "dsb", "Lower Sorbian" },
329 { "dv", "Divehi" },
330 { "el", "Greek" },
331 { "en", "English" },
332 { "es", "Spanish" },
333 { "et", "Estonian" },
334 { "eu", "Basque" },
335 { "fa", "Farsi" },
336 { "ff", "Fulfulde" },
337 { "fi", "Finnish" },
338 { "fo", "Faroese" }, /* "Faeroese" does not work */
339 { "fr", "French" },
340 { "fy", "Frisian" },
341 { "ga", "IRE" }, /* Gaelic (Ireland) */
342 { "gd", "Gaelic (Scotland)" },
343 { "gd", "Scottish Gaelic" },
344 { "gl", "Galician" },
345 { "gn", "Guarani" },
346 { "gsw", "Alsatian" },
347 { "gu", "Gujarati" },
348 { "ha", "Hausa" },
349 { "he", "Hebrew" },
350 { "hi", "Hindi" },
351 { "hr", "Croatian" },
352 { "hsb", "Upper Sorbian" },
353 { "hu", "Hungarian" },
354 { "hy", "Armenian" },
355 { "id", "Indonesian" },
356 { "ig", "Igbo" },
357 { "ii", "Yi" },
358 { "is", "Icelandic" },
359 { "it", "Italian" },
360 { "iu", "IUK" }, /* Inuktitut */
361 { "ja", "Japanese" },
362 { "ka", "Georgian" },
363 { "kk", "Kazakh" },
364 { "kl", "Greenlandic" },
365 { "km", "Cambodian" },
366 { "km", "Khmer" },
367 { "kn", "Kannada" },
368 { "ko", "Korean" },
369 { "kok", "Konkani" },
370 { "kr", "Kanuri" },
371 { "ks", "Kashmiri" },
372 { "ks_IN", "Kashmiri_India" },
373 { "ks_PK", "Kashmiri (Arabic)_Pakistan" },
374 { "ky", "Kyrgyz" },
375 { "la", "Latin" },
376 { "lb", "Luxembourgish" },
377 { "lo", "Lao" },
378 { "lt", "Lithuanian" },
379 { "lv", "Latvian" },
380 { "mi", "Maori" },
381 { "mk", "FYRO Macedonian" },
382 { "mk", "Macedonian" },
383 { "ml", "Malayalam" },
384 { "mn", "Mongolian" },
385 { "mni", "Manipuri" },
386 { "moh", "Mohawk" },
387 { "mr", "Marathi" },
388 { "ms", "Malay" },
389 { "mt", "Maltese" },
390 { "my", "Burmese" },
391 { "nb", "NOR" }, /* Norwegian Bokmål */
392 { "ne", "Nepali" },
393 { "nic", "Ibibio" },
394 { "nl", "Dutch" },
395 { "nn", "NON" }, /* Norwegian Nynorsk */
396 { "no", "Norwegian" },
397 { "nso", "Northern Sotho" },
398 { "nso", "Sepedi" },
399 { "oc", "Occitan" },
400 { "om", "Oromo" },
401 { "or", "Oriya" },
402 { "pa", "Punjabi" },
403 { "pap", "Papiamentu" },
404 { "pl", "Polish" },
405 { "prs", "Dari" },
406 { "ps", "Pashto" },
407 { "pt", "Portuguese" },
408 { "qu", "Quechua" },
409 { "qut", "K'iche'" },
410 { "rm", "Romansh" },
411 { "ro", "Romanian" },
412 { "ru", "Russian" },
413 { "rw", "Kinyarwanda" },
414 { "sa", "Sanskrit" },
415 { "sah", "Yakut" },
416 { "sd", "Sindhi" },
417 { "se", "Sami (Northern)" },
418 { "se", "Northern Sami" },
419 { "si", "Sinhalese" },
420 { "sk", "Slovak" },
421 { "sl", "Slovenian" },
422 { "sma", "Sami (Southern)" },
423 { "sma", "Southern Sami" },
424 { "smj", "Sami (Lule)" },
425 { "smj", "Lule Sami" },
426 { "smn", "Sami (Inari)" },
427 { "smn", "Inari Sami" },
428 { "sms", "Sami (Skolt)" },
429 { "sms", "Skolt Sami" },
430 { "so", "Somali" },
431 { "sq", "Albanian" },
432 { "sr", "Serbian (Latin)" },
433 { "sr@cyrillic", "SRB" }, /* Serbian (Cyrillic) */
434 { "sv", "Swedish" },
435 { "sw", "Swahili" },
436 { "syr", "Syriac" },
437 { "ta", "Tamil" },
438 { "te", "Telugu" },
439 { "tg", "Tajik" },
440 { "th", "Thai" },
441 { "ti", "Tigrinya" },
442 { "tk", "Turkmen" },
443 { "tl", "Filipino" },
444 { "tn", "Tswana" },
445 { "tr", "Turkish" },
446 { "ts", "Tsonga" },
447 { "tt", "Tatar" },
448 { "ug", "Uighur" },
449 { "uk", "Ukrainian" },
450 { "ur", "Urdu" },
451 { "uz", "Uzbek" },
452 { "uz", "Uzbek (Latin)" },
453 { "uz@cyrillic", "Uzbek (Cyrillic)" },
454 { "ve", "Venda" },
455 { "vi", "Vietnamese" },
456 { "wen", "Sorbian" },
457 { "wo", "Wolof" },
458 { "xh", "Xhosa" },
459 { "yi", "Yiddish" },
460 { "yo", "Yoruba" },
461 { "zh", "Chinese" },
462 { "zu", "Zulu" }
463 };
464
465 /* Table from ISO 3166 country code to English name.
466 Keep in sync with the gl_locale_name_from_win32_LANGID function in
467 localename.c! */
468 static const struct table_entry country_table[] =
469 {
470 { "AE", "U.A.E." },
471 { "AF", "Afghanistan" },
472 { "AL", "Albania" },
473 { "AM", "Armenia" },
474 { "AN", "Netherlands Antilles" },
475 { "AR", "Argentina" },
476 { "AT", "Austria" },
477 { "AU", "Australia" },
478 { "AZ", "Azerbaijan" },
479 { "BA", "Bosnia and Herzegovina" },
480 { "BD", "Bangladesh" },
481 { "BE", "Belgium" },
482 { "BG", "Bulgaria" },
483 { "BH", "Bahrain" },
484 { "BN", "Brunei Darussalam" },
485 { "BO", "Bolivia" },
486 { "BR", "Brazil" },
487 { "BT", "Bhutan" },
488 { "BY", "Belarus" },
489 { "BZ", "Belize" },
490 { "CA", "Canada" },
491 { "CG", "Congo" },
492 { "CH", "Switzerland" },
493 { "CI", "Cote d'Ivoire" },
494 { "CL", "Chile" },
495 { "CM", "Cameroon" },
496 { "CN", "People's Republic of China" },
497 { "CO", "Colombia" },
498 { "CR", "Costa Rica" },
499 { "CS", "Serbia and Montenegro" },
500 { "CZ", "Czech Republic" },
501 { "DE", "Germany" },
502 { "DK", "Denmark" },
503 { "DO", "Dominican Republic" },
504 { "DZ", "Algeria" },
505 { "EC", "Ecuador" },
506 { "EE", "Estonia" },
507 { "EG", "Egypt" },
508 { "ER", "Eritrea" },
509 { "ES", "Spain" },
510 { "ET", "Ethiopia" },
511 { "FI", "Finland" },
512 { "FO", "Faroe Islands" },
513 { "FR", "France" },
514 { "GB", "United Kingdom" },
515 { "GD", "Caribbean" },
516 { "GE", "Georgia" },
517 { "GL", "Greenland" },
518 { "GR", "Greece" },
519 { "GT", "Guatemala" },
520 { "HK", "Hong Kong" },
521 { "HK", "Hong Kong S.A.R." },
522 { "HN", "Honduras" },
523 { "HR", "Croatia" },
524 { "HT", "Haiti" },
525 { "HU", "Hungary" },
526 { "ID", "Indonesia" },
527 { "IE", "Ireland" },
528 { "IL", "Israel" },
529 { "IN", "India" },
530 { "IQ", "Iraq" },
531 { "IR", "Iran" },
532 { "IS", "Iceland" },
533 { "IT", "Italy" },
534 { "JM", "Jamaica" },
535 { "JO", "Jordan" },
536 { "JP", "Japan" },
537 { "KE", "Kenya" },
538 { "KG", "Kyrgyzstan" },
539 { "KH", "Cambodia" },
540 { "KR", "South Korea" },
541 { "KW", "Kuwait" },
542 { "KZ", "Kazakhstan" },
543 { "LA", "Laos" },
544 { "LB", "Lebanon" },
545 { "LI", "Liechtenstein" },
546 { "LK", "Sri Lanka" },
547 { "LT", "Lithuania" },
548 { "LU", "Luxembourg" },
549 { "LV", "Latvia" },
550 { "LY", "Libya" },
551 { "MA", "Morocco" },
552 { "MC", "Principality of Monaco" },
553 { "MD", "Moldava" },
554 { "MD", "Moldova" },
555 { "ME", "Montenegro" },
556 { "MK", "Former Yugoslav Republic of Macedonia" },
557 { "ML", "Mali" },
558 { "MM", "Myanmar" },
559 { "MN", "Mongolia" },
560 { "MO", "Macau S.A.R." },
561 { "MT", "Malta" },
562 { "MV", "Maldives" },
563 { "MX", "Mexico" },
564 { "MY", "Malaysia" },
565 { "NG", "Nigeria" },
566 { "NI", "Nicaragua" },
567 { "NL", "Netherlands" },
568 { "NO", "Norway" },
569 { "NP", "Nepal" },
570 { "NZ", "New Zealand" },
571 { "OM", "Oman" },
572 { "PA", "Panama" },
573 { "PE", "Peru" },
574 { "PH", "Philippines" },
575 { "PK", "Islamic Republic of Pakistan" },
576 { "PL", "Poland" },
577 { "PR", "Puerto Rico" },
578 { "PT", "Portugal" },
579 { "PY", "Paraguay" },
580 { "QA", "Qatar" },
581 { "RE", "Reunion" },
582 { "RO", "Romania" },
583 { "RS", "Serbia" },
584 { "RU", "Russia" },
585 { "RW", "Rwanda" },
586 { "SA", "Saudi Arabia" },
587 { "SE", "Sweden" },
588 { "SG", "Singapore" },
589 { "SI", "Slovenia" },
590 { "SK", "Slovak" },
591 { "SN", "Senegal" },
592 { "SO", "Somalia" },
593 { "SR", "Suriname" },
594 { "SV", "El Salvador" },
595 { "SY", "Syria" },
596 { "TH", "Thailand" },
597 { "TJ", "Tajikistan" },
598 { "TM", "Turkmenistan" },
599 { "TN", "Tunisia" },
600 { "TR", "Turkey" },
601 { "TT", "Trinidad and Tobago" },
602 { "TW", "Taiwan" },
603 { "TZ", "Tanzania" },
604 { "UA", "Ukraine" },
605 { "US", "United States" },
606 { "UY", "Uruguay" },
607 { "VA", "Vatican" },
608 { "VE", "Venezuela" },
609 { "VN", "Viet Nam" },
610 { "YE", "Yemen" },
611 { "ZA", "South Africa" },
612 { "ZW", "Zimbabwe" }
613 };
614
615 /* Given a string STRING, find the set of indices i such that TABLE[i].code is
616 the given STRING. It is a range [lo,hi-1]. */
617 typedef struct { size_t lo; size_t hi; } range_t;
618 static void
619 search (const struct table_entry *table, size_t table_size, const char *string,
620 range_t *result)
621 {
622 /* The table is sorted. Perform a binary search. */
623 size_t hi = table_size;
624 size_t lo = 0;
625 while (lo < hi)
626 {
627 /* Invariant:
628 for i < lo, strcmp (table[i].code, string) < 0,
629 for i >= hi, strcmp (table[i].code, string) > 0. */
630 size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
631 int cmp = strcmp (table[mid].code, string);
632 if (cmp < 0)
633 lo = mid + 1;
634 else if (cmp > 0)
635 hi = mid;
636 else
637 {
638 /* Found an i with
639 strcmp (language_table[i].code, string) == 0.
640 Find the entire interval of such i. */
641 {
642 size_t i;
643
644 for (i = mid; i > lo; )
645 {
646 i--;
647 if (strcmp (table[i].code, string) < 0)
648 {
649 lo = i + 1;
650 break;
651 }
652 }
653 }
654 {
655 size_t i;
656
657 for (i = mid + 1; i < hi; i++)
658 {
659 if (strcmp (table[i].code, string) > 0)
660 {
661 hi = i;
662 break;
663 }
664 }
665 }
666 /* The set of i with
667 strcmp (language_table[i].code, string) == 0
668 is the interval [lo, hi-1]. */
669 break;
670 }
671 }
672 result->lo = lo;
673 result->hi = hi;
674 }
675
676 /* Like setlocale, but accept also locale names in the form ll or ll_CC,
677 where ll is an ISO 639 language code and CC is an ISO 3166 country code. */
678 static char *
679 setlocale_unixlike (int category, const char *locale)
680 {
681 int is_utf8 = (GetACP () == 65001);
682 char *result;
683 char llCC_buf[64];
684 char ll_buf[64];
685 char CC_buf[64];
686
687 /* The native Windows implementation of setlocale understands the special
688 locale name "C", but not "POSIX". Therefore map "POSIX" to "C". */
689 if (locale != NULL && strcmp (locale, "POSIX") == 0)
690 locale = "C";
691
692 /* The native Windows implementation of setlocale, in the UTF-8 environment,
693 does not understand the locale names "C.UTF-8" or "C.utf8" or "C.65001",
694 but it understands "English_United States.65001", which is functionally
695 equivalent. */
696 if (locale != NULL
697 && ((is_utf8 && strcmp (locale, "C") == 0)
698 || strcmp (locale, "C.UTF-8") == 0))
699 locale = "English_United States.65001";
700
701 /* First, try setlocale with the original argument unchanged. */
702 result = setlocale_mtsafe (category, locale);
703 if (result != NULL)
704 return result;
705
706 /* Otherwise, assume the argument is in the form
707 language[_territory][.codeset][@modifier]
708 and try to map it using the tables. */
709 if (strlen (locale) < sizeof (llCC_buf))
710 {
711 /* Second try: Remove the codeset part. */
712 {
713 const char *p = locale;
714 char *q = llCC_buf;
715
716 /* Copy the part before the dot. */
717 for (; *p != '\0' && *p != '.'; p++, q++)
718 *q = *p;
719 if (*p == '.')
720 /* Skip the part up to the '@', if any. */
721 for (; *p != '\0' && *p != '@'; p++)
722 ;
723 /* Copy the part starting with '@', if any. */
724 for (; *p != '\0'; p++, q++)
725 *q = *p;
726 *q = '\0';
727 }
728 /* llCC_buf now contains
729 language[_territory][@modifier]
730 */
731 if (strcmp (llCC_buf, locale) != 0)
732 {
733 if (is_utf8)
734 {
735 char buf[64+6];
736 strcpy (buf, llCC_buf);
737 strcat (buf, ".65001");
738 result = setlocale (category, buf);
739 }
740 else
741 result = setlocale (category, llCC_buf);
742 if (result != NULL)
743 return result;
744 }
745 /* Look it up in language_table. */
746 {
747 range_t range;
748 size_t i;
749
750 search (language_table,
751 sizeof (language_table) / sizeof (language_table[0]),
752 llCC_buf,
753 &range);
754
755 for (i = range.lo; i < range.hi; i++)
756 {
757 /* Try the replacement in language_table[i]. */
758 if (is_utf8)
759 {
760 char buf[64+6];
761 strcpy (buf, language_table[i].english);
762 strcat (buf, ".65001");
763 result = setlocale (category, buf);
764 }
765 else
766 result = setlocale (category, language_table[i].english);
767 if (result != NULL)
768 return result;
769 }
770 }
771 /* Split language[_territory][@modifier]
772 into ll_buf = language[@modifier]
773 and CC_buf = territory
774 */
775 {
776 const char *underscore = strchr (llCC_buf, '_');
777 if (underscore != NULL)
778 {
779 const char *territory_start = underscore + 1;
780 const char *territory_end = strchr (territory_start, '@');
781 if (territory_end == NULL)
782 territory_end = territory_start + strlen (territory_start);
783
784 memcpy (ll_buf, llCC_buf, underscore - llCC_buf);
785 strcpy (ll_buf + (underscore - llCC_buf), territory_end);
786
787 memcpy (CC_buf, territory_start, territory_end - territory_start);
788 CC_buf[territory_end - territory_start] = '\0';
789
790 {
791 /* Look up ll_buf in language_table
792 and CC_buf in country_table. */
793 range_t language_range;
794
795 search (language_table,
796 sizeof (language_table) / sizeof (language_table[0]),
797 ll_buf,
798 &language_range);
799 if (language_range.lo < language_range.hi)
800 {
801 range_t country_range;
802
803 search (country_table,
804 sizeof (country_table) / sizeof (country_table[0]),
805 CC_buf,
806 &country_range);
807 if (country_range.lo < country_range.hi)
808 {
809 size_t i;
810 size_t j;
811
812 for (i = language_range.lo; i < language_range.hi; i++)
813 for (j = country_range.lo; j < country_range.hi; j++)
814 {
815 /* Concatenate the replacements. */
816 const char *part1 = language_table[i].english;
817 size_t part1_len = strlen (part1);
818 const char *part2 = country_table[j].english;
819 size_t part2_len = strlen (part2) + 1;
820 char buf[64+64+6];
821
822 if (!(part1_len + 1 + part2_len + 6 <= sizeof (buf)))
823 abort ();
824 memcpy (buf, part1, part1_len);
825 buf[part1_len] = '_';
826 memcpy (buf + part1_len + 1, part2, part2_len);
827 if (is_utf8)
828 strcat (buf, ".65001");
829
830 /* Try the concatenated replacements. */
831 result = setlocale (category, buf);
832 if (result != NULL)
833 return result;
834 }
835 }
836
837 /* Try omitting the country entirely. This may set a locale
838 corresponding to the wrong country, but is better than
839 failing entirely. */
840 {
841 size_t i;
842
843 for (i = language_range.lo; i < language_range.hi; i++)
844 {
845 /* Try only the language replacement. */
846 if (is_utf8)
847 {
848 char buf[64+6];
849 strcpy (buf, language_table[i].english);
850 strcat (buf, ".65001");
851 result = setlocale (category, buf);
852 }
853 else
854 result =
855 setlocale (category, language_table[i].english);
856 if (result != NULL)
857 return result;
858 }
859 }
860 }
861 }
862 }
863 }
864 }
865
866 /* Failed. */
867 return NULL;
868 }
869
870 # elif defined __ANDROID__
871
872 /* Like setlocale, but accept also the locale names "C" and "POSIX". */
873 static char *
874 setlocale_unixlike (int category, const char *locale)
875 {
876 char *result = setlocale_fixed (category, locale);
877 if (result == NULL)
878 switch (category)
879 {
880 case LC_CTYPE:
881 case LC_NUMERIC:
882 case LC_TIME:
883 case LC_COLLATE:
884 case LC_MONETARY:
885 case LC_MESSAGES:
886 case LC_ALL:
887 case LC_PAPER:
888 case LC_NAME:
889 case LC_ADDRESS:
890 case LC_TELEPHONE:
891 case LC_MEASUREMENT:
892 if (locale == NULL
893 || strcmp (locale, "C") == 0 || strcmp (locale, "POSIX") == 0)
894 result = (char *) "C";
895 break;
896 default:
897 break;
898 }
899 return result;
900 }
901 # define setlocale setlocale_unixlike
902
903 # else
904 # define setlocale_unixlike setlocale_mtsafe
905 # endif
906
907 # if LC_MESSAGES == 1729
908
909 /* Like setlocale, but support also LC_MESSAGES. */
910 static char *
911 setlocale_single (int category, const char *locale)
912 {
913 if (category == LC_MESSAGES)
914 return setlocale_messages (locale);
915 else
916 return setlocale_unixlike (category, locale);
917 }
918
919 # else
920 # define setlocale_single setlocale_unixlike
921 # endif
922
923 # if defined __APPLE__ && defined __MACH__
924
925 /* Mapping from language to main territory where that language is spoken. */
926 static char const locales_with_principal_territory[][6 + 1] =
927 {
928 /* Language Main territory */
929 "ace_ID", /* Achinese Indonesia */
930 "af_ZA", /* Afrikaans South Africa */
931 "ak_GH", /* Akan Ghana */
932 "am_ET", /* Amharic Ethiopia */
933 "an_ES", /* Aragonese Spain */
934 "ang_GB", /* Old English Britain */
935 "arn_CL", /* Mapudungun Chile */
936 "as_IN", /* Assamese India */
937 "ast_ES", /* Asturian Spain */
938 "av_RU", /* Avaric Russia */
939 "awa_IN", /* Awadhi India */
940 "az_AZ", /* Azerbaijani Azerbaijan */
941 "ban_ID", /* Balinese Indonesia */
942 "be_BY", /* Belarusian Belarus */
943 "bej_SD", /* Beja Sudan */
944 "bem_ZM", /* Bemba Zambia */
945 "bg_BG", /* Bulgarian Bulgaria */
946 "bho_IN", /* Bhojpuri India */
947 "bi_VU", /* Bislama Vanuatu */
948 "bik_PH", /* Bikol Philippines */
949 "bin_NG", /* Bini Nigeria */
950 "bm_ML", /* Bambara Mali */
951 "bn_IN", /* Bengali India */
952 "bo_CN", /* Tibetan China */
953 "br_FR", /* Breton France */
954 "bs_BA", /* Bosnian Bosnia */
955 "bug_ID", /* Buginese Indonesia */
956 "ca_ES", /* Catalan Spain */
957 "ce_RU", /* Chechen Russia */
958 "ceb_PH", /* Cebuano Philippines */
959 "co_FR", /* Corsican France */
960 "cr_CA", /* Cree Canada */
961 /* Don't put "crh_UZ" or "crh_UA" here. That would be asking for fruitless
962 political discussion. */
963 "cs_CZ", /* Czech Czech Republic */
964 "csb_PL", /* Kashubian Poland */
965 "cy_GB", /* Welsh Britain */
966 "da_DK", /* Danish Denmark */
967 "de_DE", /* German Germany */
968 "din_SD", /* Dinka Sudan */
969 "doi_IN", /* Dogri India */
970 "dsb_DE", /* Lower Sorbian Germany */
971 "dv_MV", /* Divehi Maldives */
972 "dz_BT", /* Dzongkha Bhutan */
973 "ee_GH", /* Éwé Ghana */
974 "el_GR", /* Greek Greece */
975 /* Don't put "en_GB" or "en_US" here. That would be asking for fruitless
976 political discussion. */
977 "es_ES", /* Spanish Spain */
978 "et_EE", /* Estonian Estonia */
979 "fa_IR", /* Persian Iran */
980 "fi_FI", /* Finnish Finland */
981 "fil_PH", /* Filipino Philippines */
982 "fj_FJ", /* Fijian Fiji */
983 "fo_FO", /* Faroese Faeroe Islands */
984 "fon_BJ", /* Fon Benin */
985 "fr_FR", /* French France */
986 "fur_IT", /* Friulian Italy */
987 "fy_NL", /* Western Frisian Netherlands */
988 "ga_IE", /* Irish Ireland */
989 "gd_GB", /* Scottish Gaelic Britain */
990 "gl_ES", /* Galician Spain */
991 "gon_IN", /* Gondi India */
992 "gsw_CH", /* Swiss German Switzerland */
993 "gu_IN", /* Gujarati India */
994 "he_IL", /* Hebrew Israel */
995 "hi_IN", /* Hindi India */
996 "hil_PH", /* Hiligaynon Philippines */
997 "hr_HR", /* Croatian Croatia */
998 "hsb_DE", /* Upper Sorbian Germany */
999 "ht_HT", /* Haitian Haiti */
1000 "hu_HU", /* Hungarian Hungary */
1001 "hy_AM", /* Armenian Armenia */
1002 "id_ID", /* Indonesian Indonesia */
1003 "ig_NG", /* Igbo Nigeria */
1004 "ii_CN", /* Sichuan Yi China */
1005 "ilo_PH", /* Iloko Philippines */
1006 "is_IS", /* Icelandic Iceland */
1007 "it_IT", /* Italian Italy */
1008 "ja_JP", /* Japanese Japan */
1009 "jab_NG", /* Hyam Nigeria */
1010 "jv_ID", /* Javanese Indonesia */
1011 "ka_GE", /* Georgian Georgia */
1012 "kab_DZ", /* Kabyle Algeria */
1013 "kaj_NG", /* Jju Nigeria */
1014 "kam_KE", /* Kamba Kenya */
1015 "kmb_AO", /* Kimbundu Angola */
1016 "kcg_NG", /* Tyap Nigeria */
1017 "kdm_NG", /* Kagoma Nigeria */
1018 "kg_CD", /* Kongo Democratic Republic of Congo */
1019 "kk_KZ", /* Kazakh Kazakhstan */
1020 "kl_GL", /* Kalaallisut Greenland */
1021 "km_KH", /* Central Khmer Cambodia */
1022 "kn_IN", /* Kannada India */
1023 "ko_KR", /* Korean Korea (South) */
1024 "kok_IN", /* Konkani India */
1025 "kr_NG", /* Kanuri Nigeria */
1026 "kru_IN", /* Kurukh India */
1027 "ky_KG", /* Kyrgyz Kyrgyzstan */
1028 "lg_UG", /* Ganda Uganda */
1029 "li_BE", /* Limburgish Belgium */
1030 "lo_LA", /* Laotian Laos */
1031 "lt_LT", /* Lithuanian Lithuania */
1032 "lu_CD", /* Luba-Katanga Democratic Republic of Congo */
1033 "lua_CD", /* Luba-Lulua Democratic Republic of Congo */
1034 "luo_KE", /* Luo Kenya */
1035 "lv_LV", /* Latvian Latvia */
1036 "mad_ID", /* Madurese Indonesia */
1037 "mag_IN", /* Magahi India */
1038 "mai_IN", /* Maithili India */
1039 "mak_ID", /* Makasar Indonesia */
1040 "man_ML", /* Mandingo Mali */
1041 "men_SL", /* Mende Sierra Leone */
1042 "mfe_MU", /* Mauritian Creole Mauritius */
1043 "mg_MG", /* Malagasy Madagascar */
1044 "mi_NZ", /* Maori New Zealand */
1045 "min_ID", /* Minangkabau Indonesia */
1046 "mk_MK", /* Macedonian North Macedonia */
1047 "ml_IN", /* Malayalam India */
1048 "mn_MN", /* Mongolian Mongolia */
1049 "mni_IN", /* Manipuri India */
1050 "mos_BF", /* Mossi Burkina Faso */
1051 "mr_IN", /* Marathi India */
1052 "ms_MY", /* Malay Malaysia */
1053 "mt_MT", /* Maltese Malta */
1054 "mwr_IN", /* Marwari India */
1055 "my_MM", /* Burmese Myanmar */
1056 "na_NR", /* Nauru Nauru */
1057 "nah_MX", /* Nahuatl Mexico */
1058 "nap_IT", /* Neapolitan Italy */
1059 "nb_NO", /* Norwegian Bokmål Norway */
1060 "nds_DE", /* Low Saxon Germany */
1061 "ne_NP", /* Nepali Nepal */
1062 "nl_NL", /* Dutch Netherlands */
1063 "nn_NO", /* Norwegian Nynorsk Norway */
1064 "no_NO", /* Norwegian Norway */
1065 "nr_ZA", /* South Ndebele South Africa */
1066 "nso_ZA", /* Northern Sotho South Africa */
1067 "ny_MW", /* Chichewa Malawi */
1068 "nym_TZ", /* Nyamwezi Tanzania */
1069 "nyn_UG", /* Nyankole Uganda */
1070 "oc_FR", /* Occitan France */
1071 "oj_CA", /* Ojibwa Canada */
1072 "or_IN", /* Oriya India */
1073 "pa_IN", /* Punjabi India */
1074 "pag_PH", /* Pangasinan Philippines */
1075 "pam_PH", /* Pampanga Philippines */
1076 "pap_AN", /* Papiamento Netherlands Antilles - this line can be removed in 2018 */
1077 "pbb_CO", /* Páez Colombia */
1078 "pl_PL", /* Polish Poland */
1079 "ps_AF", /* Pashto Afghanistan */
1080 "pt_PT", /* Portuguese Portugal */
1081 "raj_IN", /* Rajasthani India */
1082 "rm_CH", /* Romansh Switzerland */
1083 "rn_BI", /* Kirundi Burundi */
1084 "ro_RO", /* Romanian Romania */
1085 "ru_RU", /* Russian Russia */
1086 "rw_RW", /* Kinyarwanda Rwanda */
1087 "sa_IN", /* Sanskrit India */
1088 "sah_RU", /* Yakut Russia */
1089 "sas_ID", /* Sasak Indonesia */
1090 "sat_IN", /* Santali India */
1091 "sc_IT", /* Sardinian Italy */
1092 "scn_IT", /* Sicilian Italy */
1093 "sg_CF", /* Sango Central African Republic */
1094 "shn_MM", /* Shan Myanmar */
1095 "si_LK", /* Sinhala Sri Lanka */
1096 "sid_ET", /* Sidamo Ethiopia */
1097 "sk_SK", /* Slovak Slovakia */
1098 "sl_SI", /* Slovenian Slovenia */
1099 "sm_WS", /* Samoan Samoa */
1100 "smn_FI", /* Inari Sami Finland */
1101 "sms_FI", /* Skolt Sami Finland */
1102 "so_SO", /* Somali Somalia */
1103 "sq_AL", /* Albanian Albania */
1104 "sr_RS", /* Serbian Serbia */
1105 "srr_SN", /* Serer Senegal */
1106 "suk_TZ", /* Sukuma Tanzania */
1107 "sus_GN", /* Susu Guinea */
1108 "sv_SE", /* Swedish Sweden */
1109 "ta_IN", /* Tamil India */
1110 "te_IN", /* Telugu India */
1111 "tem_SL", /* Timne Sierra Leone */
1112 "tet_ID", /* Tetum Indonesia */
1113 "tg_TJ", /* Tajik Tajikistan */
1114 "th_TH", /* Thai Thailand */
1115 "ti_ER", /* Tigrinya Eritrea */
1116 "tiv_NG", /* Tiv Nigeria */
1117 "tk_TM", /* Turkmen Turkmenistan */
1118 "tl_PH", /* Tagalog Philippines */
1119 "to_TO", /* Tonga Tonga */
1120 "tpi_PG", /* Tok Pisin Papua New Guinea */
1121 "tr_TR", /* Turkish Türkiye */
1122 "tum_MW", /* Tumbuka Malawi */
1123 "ug_CN", /* Uighur China */
1124 "uk_UA", /* Ukrainian Ukraine */
1125 "umb_AO", /* Umbundu Angola */
1126 "ur_PK", /* Urdu Pakistan */
1127 "uz_UZ", /* Uzbek Uzbekistan */
1128 "ve_ZA", /* Venda South Africa */
1129 "vi_VN", /* Vietnamese Vietnam */
1130 "wa_BE", /* Walloon Belgium */
1131 "wal_ET", /* Walamo Ethiopia */
1132 "war_PH", /* Waray Philippines */
1133 "wen_DE", /* Sorbian Germany */
1134 "yao_MW", /* Yao Malawi */
1135 "zap_MX" /* Zapotec Mexico */
1136 };
1137
1138 /* Compare just the language part of two locale names. */
1139 static int
1140 langcmp (const char *locale1, const char *locale2)
1141 {
1142 size_t locale1_len;
1143 size_t locale2_len;
1144 int cmp;
1145
1146 {
1147 const char *locale1_end = strchr (locale1, '_');
1148 if (locale1_end != NULL)
1149 locale1_len = locale1_end - locale1;
1150 else
1151 locale1_len = strlen (locale1);
1152 }
1153 {
1154 const char *locale2_end = strchr (locale2, '_');
1155 if (locale2_end != NULL)
1156 locale2_len = locale2_end - locale2;
1157 else
1158 locale2_len = strlen (locale2);
1159 }
1160
1161 if (locale1_len < locale2_len)
1162 {
1163 cmp = memcmp (locale1, locale2, locale1_len);
1164 if (cmp == 0)
1165 cmp = -1;
1166 }
1167 else
1168 {
1169 cmp = memcmp (locale1, locale2, locale2_len);
1170 if (locale1_len > locale2_len && cmp == 0)
1171 cmp = 1;
1172 }
1173
1174 return cmp;
1175 }
1176
1177 /* Given a locale name, return the main locale with the same language,
1178 or NULL if not found.
1179 For example: "fr_DE" -> "fr_FR". */
1180 static const char *
1181 get_main_locale_with_same_language (const char *locale)
1182 {
1183 # define table locales_with_principal_territory
1184 /* The table is sorted. Perform a binary search. */
1185 size_t hi = sizeof (table) / sizeof (table[0]);
1186 size_t lo = 0;
1187 while (lo < hi)
1188 {
1189 /* Invariant:
1190 for i < lo, langcmp (table[i], locale) < 0,
1191 for i >= hi, langcmp (table[i], locale) > 0. */
1192 size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
1193 int cmp = langcmp (table[mid], locale);
1194 if (cmp < 0)
1195 lo = mid + 1;
1196 else if (cmp > 0)
1197 hi = mid;
1198 else
1199 {
1200 /* Found an i with
1201 langcmp (language_table[i], locale) == 0.
1202 Verify that it is the only such i. */
1203 if (mid > lo && langcmp (table[mid - 1], locale) >= 0)
1204 abort ();
1205 if (mid + 1 < hi && langcmp (table[mid + 1], locale) <= 0)
1206 abort ();
1207 return table[mid];
1208 }
1209 }
1210 # undef table
1211 return NULL;
1212 }
1213
1214 /* Mapping from territory to main language that is spoken in that territory. */
1215 static char const locales_with_principal_language[][6 + 1] =
1216 {
1217 /* This is based on the set of existing locales in glibc, with duplicates
1218 removed, and on the Wikipedia pages named "Languages of <territory>".
1219 If in doubt, use the locale that exists in macOS. For example, the only
1220 "*_IN" locale in macOS 10.13 is "hi_IN", so use that. */
1221 /* A useful shell function for producing a line of this table is:
1222 func_line ()
1223 {
1224 # Usage: func_line ll_CC
1225 ll=`echo "$1" | sed -e 's|_.*||'`
1226 cc=`echo "$1" | sed -e 's|^.*_||'`
1227 llx=`sed -n -e "s|^${ll} ||p" < gettext-tools/doc/ISO_639`
1228 ccx=`expand gettext-tools/doc/ISO_3166 | sed -n -e "s|^${cc} *||p"`
1229 echo " \"$1\", /$X* ${llx} ${ccx} *$X/"
1230 }
1231 */
1232 /* Main language Territory */
1233 "ca_AD", /* Catalan Andorra */
1234 "ar_AE", /* Arabic United Arab Emirates */
1235 "ps_AF", /* Pashto Afghanistan */
1236 "en_AG", /* English Antigua and Barbuda */
1237 "sq_AL", /* Albanian Albania */
1238 "hy_AM", /* Armenian Armenia */
1239 "pap_AN", /* Papiamento Netherlands Antilles - this line can be removed in 2018 */
1240 "pt_AO", /* Portuguese Angola */
1241 "es_AR", /* Spanish Argentina */
1242 "de_AT", /* German Austria */
1243 "en_AU", /* English Australia */
1244 /* Aruba has two official languages: "nl_AW", "pap_AW". */
1245 "az_AZ", /* Azerbaijani Azerbaijan */
1246 "bs_BA", /* Bosnian Bosnia */
1247 "bn_BD", /* Bengali Bangladesh */
1248 "nl_BE", /* Dutch Belgium */
1249 "fr_BF", /* French Burkina Faso */
1250 "bg_BG", /* Bulgarian Bulgaria */
1251 "ar_BH", /* Arabic Bahrain */
1252 "rn_BI", /* Kirundi Burundi */
1253 "fr_BJ", /* French Benin */
1254 "es_BO", /* Spanish Bolivia */
1255 "pt_BR", /* Portuguese Brazil */
1256 "dz_BT", /* Dzongkha Bhutan */
1257 "en_BW", /* English Botswana */
1258 "be_BY", /* Belarusian Belarus */
1259 "en_CA", /* English Canada */
1260 "fr_CD", /* French Democratic Republic of Congo */
1261 "sg_CF", /* Sango Central African Republic */
1262 "de_CH", /* German Switzerland */
1263 "es_CL", /* Spanish Chile */
1264 "zh_CN", /* Chinese China */
1265 "es_CO", /* Spanish Colombia */
1266 "es_CR", /* Spanish Costa Rica */
1267 "es_CU", /* Spanish Cuba */
1268 /* Curaçao has three official languages: "nl_CW", "pap_CW", "en_CW". */
1269 "el_CY", /* Greek Cyprus */
1270 "cs_CZ", /* Czech Czech Republic */
1271 "de_DE", /* German Germany */
1272 /* Djibouti has two official languages: "ar_DJ" and "fr_DJ". */
1273 "da_DK", /* Danish Denmark */
1274 "es_DO", /* Spanish Dominican Republic */
1275 "ar_DZ", /* Arabic Algeria */
1276 "es_EC", /* Spanish Ecuador */
1277 "et_EE", /* Estonian Estonia */
1278 "ar_EG", /* Arabic Egypt */
1279 "ti_ER", /* Tigrinya Eritrea */
1280 "es_ES", /* Spanish Spain */
1281 "am_ET", /* Amharic Ethiopia */
1282 "fi_FI", /* Finnish Finland */
1283 /* Fiji has three official languages: "en_FJ", "fj_FJ", "hif_FJ". */
1284 "fo_FO", /* Faroese Faeroe Islands */
1285 "fr_FR", /* French France */
1286 "en_GB", /* English Britain */
1287 "ka_GE", /* Georgian Georgia */
1288 "en_GH", /* English Ghana */
1289 "kl_GL", /* Kalaallisut Greenland */
1290 "fr_GN", /* French Guinea */
1291 "el_GR", /* Greek Greece */
1292 "es_GT", /* Spanish Guatemala */
1293 "zh_HK", /* Chinese Hong Kong */
1294 "es_HN", /* Spanish Honduras */
1295 "hr_HR", /* Croatian Croatia */
1296 "ht_HT", /* Haitian Haiti */
1297 "hu_HU", /* Hungarian Hungary */
1298 "id_ID", /* Indonesian Indonesia */
1299 "en_IE", /* English Ireland */
1300 "he_IL", /* Hebrew Israel */
1301 "hi_IN", /* Hindi India */
1302 "ar_IQ", /* Arabic Iraq */
1303 "fa_IR", /* Persian Iran */
1304 "is_IS", /* Icelandic Iceland */
1305 "it_IT", /* Italian Italy */
1306 "ar_JO", /* Arabic Jordan */
1307 "ja_JP", /* Japanese Japan */
1308 "sw_KE", /* Swahili Kenya */
1309 "ky_KG", /* Kyrgyz Kyrgyzstan */
1310 "km_KH", /* Central Khmer Cambodia */
1311 "ko_KR", /* Korean Korea (South) */
1312 "ar_KW", /* Arabic Kuwait */
1313 "kk_KZ", /* Kazakh Kazakhstan */
1314 "lo_LA", /* Laotian Laos */
1315 "ar_LB", /* Arabic Lebanon */
1316 "de_LI", /* German Liechtenstein */
1317 "si_LK", /* Sinhala Sri Lanka */
1318 "lt_LT", /* Lithuanian Lithuania */
1319 /* Luxembourg has three official languages: "lb_LU", "fr_LU", "de_LU". */
1320 "lv_LV", /* Latvian Latvia */
1321 "ar_LY", /* Arabic Libya */
1322 "ar_MA", /* Arabic Morocco */
1323 "sr_ME", /* Serbian Montenegro */
1324 "mg_MG", /* Malagasy Madagascar */
1325 "mk_MK", /* Macedonian North Macedonia */
1326 "fr_ML", /* French Mali */
1327 "my_MM", /* Burmese Myanmar */
1328 "mn_MN", /* Mongolian Mongolia */
1329 "mt_MT", /* Maltese Malta */
1330 "mfe_MU", /* Mauritian Creole Mauritius */
1331 "dv_MV", /* Divehi Maldives */
1332 "ny_MW", /* Chichewa Malawi */
1333 "es_MX", /* Spanish Mexico */
1334 "ms_MY", /* Malay Malaysia */
1335 "en_NG", /* English Nigeria */
1336 "es_NI", /* Spanish Nicaragua */
1337 "nl_NL", /* Dutch Netherlands */
1338 "no_NO", /* Norwegian Norway */
1339 "ne_NP", /* Nepali Nepal */
1340 "na_NR", /* Nauru Nauru */
1341 "niu_NU", /* Niuean Niue */
1342 "en_NZ", /* English New Zealand */
1343 "ar_OM", /* Arabic Oman */
1344 "es_PA", /* Spanish Panama */
1345 "es_PE", /* Spanish Peru */
1346 "tpi_PG", /* Tok Pisin Papua New Guinea */
1347 "fil_PH", /* Filipino Philippines */
1348 "pa_PK", /* Punjabi Pakistan */
1349 "pl_PL", /* Polish Poland */
1350 "es_PR", /* Spanish Puerto Rico */
1351 "pt_PT", /* Portuguese Portugal */
1352 "es_PY", /* Spanish Paraguay */
1353 "ar_QA", /* Arabic Qatar */
1354 "ro_RO", /* Romanian Romania */
1355 "sr_RS", /* Serbian Serbia */
1356 "ru_RU", /* Russian Russia */
1357 "rw_RW", /* Kinyarwanda Rwanda */
1358 "ar_SA", /* Arabic Saudi Arabia */
1359 "en_SC", /* English Seychelles */
1360 "ar_SD", /* Arabic Sudan */
1361 "sv_SE", /* Swedish Sweden */
1362 "en_SG", /* English Singapore */
1363 "sl_SI", /* Slovenian Slovenia */
1364 "sk_SK", /* Slovak Slovakia */
1365 "en_SL", /* English Sierra Leone */
1366 "fr_SN", /* French Senegal */
1367 "so_SO", /* Somali Somalia */
1368 "ar_SS", /* Arabic South Sudan */
1369 "es_SV", /* Spanish El Salvador */
1370 "ar_SY", /* Arabic Syria */
1371 "th_TH", /* Thai Thailand */
1372 "tg_TJ", /* Tajik Tajikistan */
1373 "tk_TM", /* Turkmen Turkmenistan */
1374 "ar_TN", /* Arabic Tunisia */
1375 "to_TO", /* Tonga Tonga */
1376 "tr_TR", /* Turkish Türkiye */
1377 "zh_TW", /* Chinese Taiwan */
1378 "sw_TZ", /* Swahili Tanzania */
1379 "uk_UA", /* Ukrainian Ukraine */
1380 "lg_UG", /* Ganda Uganda */
1381 "en_US", /* English United States of America */
1382 "es_UY", /* Spanish Uruguay */
1383 "uz_UZ", /* Uzbek Uzbekistan */
1384 "es_VE", /* Spanish Venezuela */
1385 "vi_VN", /* Vietnamese Vietnam */
1386 "bi_VU", /* Bislama Vanuatu */
1387 "sm_WS", /* Samoan Samoa */
1388 "ar_YE", /* Arabic Yemen */
1389 "en_ZA", /* English South Africa */
1390 "en_ZM", /* English Zambia */
1391 "en_ZW" /* English Zimbabwe */
1392 };
1393
1394 /* Compare just the territory part of two locale names. */
1395 static int
1396 terrcmp (const char *locale1, const char *locale2)
1397 {
1398 const char *territory1 = strrchr (locale1, '_') + 1;
1399 const char *territory2 = strrchr (locale2, '_') + 1;
1400
1401 return strcmp (territory1, territory2);
1402 }
1403
1404 /* Given a locale name, return the locale corresponding to the main language
1405 with the same territory, or NULL if not found.
1406 For example: "fr_DE" -> "de_DE". */
1407 static const char *
1408 get_main_locale_with_same_territory (const char *locale)
1409 {
1410 if (strrchr (locale, '_') != NULL)
1411 {
1412 # define table locales_with_principal_language
1413 /* The table is sorted. Perform a binary search. */
1414 size_t hi = sizeof (table) / sizeof (table[0]);
1415 size_t lo = 0;
1416 while (lo < hi)
1417 {
1418 /* Invariant:
1419 for i < lo, terrcmp (table[i], locale) < 0,
1420 for i >= hi, terrcmp (table[i], locale) > 0. */
1421 size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
1422 int cmp = terrcmp (table[mid], locale);
1423 if (cmp < 0)
1424 lo = mid + 1;
1425 else if (cmp > 0)
1426 hi = mid;
1427 else
1428 {
1429 /* Found an i with
1430 terrcmp (language_table[i], locale) == 0.
1431 Verify that it is the only such i. */
1432 if (mid > lo && terrcmp (table[mid - 1], locale) >= 0)
1433 abort ();
1434 if (mid + 1 < hi && terrcmp (table[mid + 1], locale) <= 0)
1435 abort ();
1436 return table[mid];
1437 }
1438 }
1439 # undef table
1440 }
1441 return NULL;
1442 }
1443
1444 # endif
1445
1446 char *
1447 setlocale_improved (int category, const char *locale)
1448 {
1449 if (locale != NULL && locale[0] == '\0')
1450 {
1451 /* A request to the set the current locale to the default locale. */
1452 if (category == LC_ALL)
1453 {
1454 /* Set LC_CTYPE first. Then the other categories. */
1455 static int const categories[] =
1456 {
1457 LC_CTYPE,
1458 LC_NUMERIC,
1459 LC_TIME,
1460 LC_COLLATE,
1461 LC_MONETARY,
1462 LC_MESSAGES
1463 };
1464 char *saved_locale;
1465 const char *base_name;
1466 unsigned int i;
1467
1468 /* Back up the old locale, in case one of the steps fails. */
1469 saved_locale = setlocale (LC_ALL, NULL);
1470 if (saved_locale == NULL)
1471 return NULL;
1472 saved_locale = strdup (saved_locale);
1473 if (saved_locale == NULL)
1474 return NULL;
1475
1476 /* Set LC_CTYPE category. Set all other categories (except possibly
1477 LC_MESSAGES) to the same value in the same call; this is likely to
1478 save calls. */
1479 base_name =
1480 gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
1481 if (base_name == NULL)
1482 base_name = gl_locale_name_default ();
1483
1484 if (setlocale_unixlike (LC_ALL, base_name) != NULL)
1485 {
1486 /* LC_CTYPE category already set. */
1487 i = 1;
1488 }
1489 else
1490 {
1491 /* On Mac OS X, "UTF-8" is a valid locale name for LC_CTYPE but
1492 not for LC_ALL. Therefore this call may fail. So, try
1493 another base_name. */
1494 base_name = "C";
1495 if (setlocale_unixlike (LC_ALL, base_name) == NULL)
1496 goto fail;
1497 i = 0;
1498 }
1499 # if defined _WIN32 && ! defined __CYGWIN__
1500 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
1501 LC_CTYPE category to an invalid value ("C") when it does not
1502 support the specified encoding. Report a failure instead. */
1503 if (strchr (base_name, '.') != NULL
1504 && strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
1505 goto fail;
1506 # endif
1507
1508 for (; i < sizeof (categories) / sizeof (categories[0]); i++)
1509 {
1510 int cat = categories[i];
1511 const char *name;
1512
1513 name = gl_locale_name_environ (cat, category_to_name (cat));
1514 if (name == NULL)
1515 name = gl_locale_name_default ();
1516
1517 /* If name is the same as base_name, it has already been set
1518 through the setlocale call before the loop. */
1519 if (strcmp (name, base_name) != 0
1520 # if LC_MESSAGES == 1729
1521 || cat == LC_MESSAGES
1522 # endif
1523 )
1524 if (setlocale_single (cat, name) == NULL)
1525 # if defined __APPLE__ && defined __MACH__
1526 {
1527 /* On Mac OS X 10.13, some locales can be set through
1528 System Preferences > Language & Region, that are not
1529 supported by libc. The system's setlocale() falls
1530 back to "C" for these locale categories. We can do
1531 better, by trying an existing locale with the same
1532 language or an existing locale with the same territory.
1533 If we can't, print a warning, to limit user
1534 expectations. */
1535 int warn = 0;
1536
1537 if (cat == LC_CTYPE)
1538 warn = (setlocale_single (cat, "UTF-8") == NULL);
1539 else if (cat == LC_MESSAGES)
1540 {
1541 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES || HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
1542 /* Take the primary language preference. */
1543 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES /* MacOS X 10.5 or newer */
1544 CFArrayRef prefArray = CFLocaleCopyPreferredLanguages ();
1545 # elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
1546 CFTypeRef preferences =
1547 CFPreferencesCopyAppValue (CFSTR ("AppleLanguages"),
1548 kCFPreferencesCurrentApplication);
1549 if (preferences != NULL
1550 && CFGetTypeID (preferences) == CFArrayGetTypeID ())
1551 {
1552 CFArrayRef prefArray = (CFArrayRef)preferences;
1553 # endif
1554 int n = CFArrayGetCount (prefArray);
1555 if (n > 0)
1556 {
1557 char buf[256];
1558 CFTypeRef element = CFArrayGetValueAtIndex (prefArray, 0);
1559 if (element != NULL
1560 && CFGetTypeID (element) == CFStringGetTypeID ()
1561 && CFStringGetCString ((CFStringRef)element,
1562 buf, sizeof (buf),
1563 kCFStringEncodingASCII))
1564 {
1565 /* Remove the country.
1566 E.g. "zh-Hans-DE" -> "zh-Hans". */
1567 char *last_minus = strrchr (buf, '-');
1568 if (last_minus != NULL)
1569 *last_minus = '\0';
1570
1571 /* Convert to Unix locale name.
1572 E.g. "zh-Hans" -> "zh_CN". */
1573 gl_locale_name_canonicalize (buf);
1574
1575 /* Try setlocale with this value. */
1576 if (setlocale_single (cat, buf) == NULL)
1577 {
1578 const char *last_try =
1579 get_main_locale_with_same_language (buf);
1580
1581 if (last_try == NULL
1582 || setlocale_single (cat, last_try) == NULL)
1583 warn = 1;
1584 }
1585 }
1586 }
1587 # if HAVE_CFLOCALECOPYPREFERREDLANGUAGES /* MacOS X 10.5 or newer */
1588 CFRelease (prefArray);
1589 # elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.4 or newer */
1590 }
1591 # endif
1592 # else
1593 const char *last_try =
1594 get_main_locale_with_same_language (name);
1595
1596 if (last_try == NULL
1597 || setlocale_single (cat, last_try) == NULL)
1598 warn = 1;
1599 # endif
1600 }
1601 else
1602 {
1603 /* For LC_NUMERIC, the application should use the locale
1604 properties kCFLocaleDecimalSeparator,
1605 kCFLocaleGroupingSeparator.
1606 For LC_TIME, the application should use the locale
1607 property kCFLocaleCalendarIdentifier.
1608 For LC_COLLATE, the application should use the locale
1609 properties kCFLocaleCollationIdentifier,
1610 kCFLocaleCollatorIdentifier.
1611 For LC_MONETARY, the application should use the locale
1612 properties kCFLocaleCurrencySymbol,
1613 kCFLocaleCurrencyCode.
1614 But since most applications don't have macOS specific
1615 code like this, try an existing locale with the same
1616 territory. */
1617 const char *last_try =
1618 get_main_locale_with_same_territory (name);
1619
1620 if (last_try == NULL
1621 || setlocale_single (cat, last_try) == NULL)
1622 warn = 1;
1623 }
1624
1625 if (warn)
1626 {
1627 /* Warn only if the environment variable
1628 SETLOCALE_VERBOSE is set. Otherwise these warnings
1629 are just annoyances, since normal users won't invoke
1630 'localedef'. */
1631 const char *verbose = getenv ("SETLOCALE_VERBOSE");
1632 if (verbose != NULL && verbose[0] != '\0')
1633 fprintf (stderr,
1634 "Warning: Failed to set locale category %s to %s.\n",
1635 category_to_name (cat), name);
1636 }
1637 }
1638 # else
1639 goto fail;
1640 # endif
1641 }
1642
1643 /* All steps were successful. */
1644 free (saved_locale);
1645 goto ret_all;
1646
1647 fail:
1648 if (saved_locale[0] != '\0') /* don't risk an endless recursion */
1649 setlocale (LC_ALL, saved_locale);
1650 free (saved_locale);
1651 return NULL;
1652 }
1653 else
1654 {
1655 const char *name =
1656 gl_locale_name_environ (category, category_to_name (category));
1657 if (name == NULL)
1658 name = gl_locale_name_default ();
1659
1660 return setlocale_single (category, name);
1661 }
1662 }
1663 else
1664 {
1665 # if LC_MESSAGES == 1729
1666 if (locale != NULL)
1667 {
1668 char truncated_locale[SETLOCALE_NULL_ALL_MAX];
1669 const char *native_locale;
1670 const char *messages_locale;
1671
1672 if (strncmp (locale, "LC_COLLATE=", 11) == 0)
1673 {
1674 /* The locale argument indicates a mixed locale. It must be of
1675 the form
1676 "LC_COLLATE=...;LC_CTYPE=...;LC_MONETARY=...;LC_NUMERIC=...;LC_TIME=...;LC_MESSAGES=..."
1677 since that is what this function returns (see ret_all below).
1678 Decompose it. */
1679 const char *last_semicolon = strrchr (locale, ';');
1680 if (!(last_semicolon != NULL
1681 && strncmp (last_semicolon + 1, "LC_MESSAGES=", 12) == 0))
1682 return NULL;
1683 if (category == LC_MESSAGES)
1684 return setlocale_single (category, last_semicolon + 13);
1685 size_t truncated_locale_len = last_semicolon - locale;
1686 if (truncated_locale_len >= sizeof (truncated_locale))
1687 return NULL;
1688 memcpy (truncated_locale, locale, truncated_locale_len);
1689 truncated_locale[truncated_locale_len] = '\0';
1690 native_locale = truncated_locale;
1691 messages_locale = last_semicolon + 13;
1692 }
1693 else
1694 {
1695 native_locale = locale;
1696 messages_locale = locale;
1697 }
1698
1699 if (category == LC_ALL)
1700 {
1701 /* In the underlying implementation, LC_ALL does not contain
1702 LC_MESSAGES. Therefore we need to handle LC_MESSAGES
1703 separately. */
1704 char *result;
1705
1706 # if defined _WIN32 && ! defined __CYGWIN__
1707 if (strchr (native_locale, '.') != NULL)
1708 {
1709 char *saved_locale;
1710
1711 /* Back up the old locale. */
1712 saved_locale = setlocale (LC_ALL, NULL);
1713 if (saved_locale == NULL)
1714 return NULL;
1715 saved_locale = strdup (saved_locale);
1716 if (saved_locale == NULL)
1717 return NULL;
1718
1719 if (setlocale_unixlike (LC_ALL, native_locale) == NULL)
1720 {
1721 free (saved_locale);
1722 return NULL;
1723 }
1724
1725 /* On native Windows, setlocale(LC_ALL,...) may succeed but
1726 set the LC_CTYPE category to an invalid value ("C") when
1727 it does not support the specified encoding. Report a
1728 failure instead. */
1729 if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
1730 {
1731 /* Don't risk an endless recursion. */
1732 if (saved_locale[0] != '\0')
1733 setlocale (LC_ALL, saved_locale);
1734 free (saved_locale);
1735 return NULL;
1736 }
1737
1738 /* It was really successful. */
1739 free (saved_locale);
1740 result = setlocale (LC_ALL, NULL);
1741 }
1742 else
1743 # endif
1744 result = setlocale_single (LC_ALL, native_locale);
1745 if (result == NULL)
1746 return NULL;
1747
1748 setlocale_single (LC_MESSAGES, messages_locale);
1749
1750 goto ret_all;
1751 }
1752 else
1753 {
1754 if (category == LC_MESSAGES)
1755 return setlocale_single (category, messages_locale);
1756 else
1757 return setlocale_single (category, native_locale);
1758 }
1759 }
1760 else /* locale == NULL */
1761 {
1762 if (category == LC_ALL)
1763 goto ret_all;
1764 else
1765 return setlocale_single (category, NULL);
1766 }
1767 # else
1768 return setlocale_single (category, locale);
1769 # endif
1770 }
1771
1772 ret_all:
1773 /* Return the name of all categories of the current locale. */
1774 # if LC_MESSAGES == 1729 /* native Windows */
1775 /* The locale name for mixed locales looks like this:
1776 "LC_COLLATE=...;LC_CTYPE=...;LC_MONETARY=...;LC_NUMERIC=...;LC_TIME=..."
1777 If necessary, add ";LC_MESSAGES=..." at the end. */
1778 {
1779 char *name1 = setlocale (LC_ALL, NULL);
1780 char *name2 = setlocale_single (LC_MESSAGES, NULL);
1781 if (strcmp (name1, name2) == 0)
1782 /* Not a mixed locale. */
1783 return name1;
1784 else
1785 {
1786 static char resultbuf[SETLOCALE_NULL_ALL_MAX];
1787 /* Prepare the result in a stack-allocated buffer, in order to reduce
1788 race conditions in a multithreaded situation. */
1789 char stackbuf[SETLOCALE_NULL_ALL_MAX];
1790 if (strncmp (name1, "LC_COLLATE=", 11) == 0)
1791 {
1792 if (strlen (name1) + strlen (name2) + 13 >= sizeof (stackbuf))
1793 return NULL;
1794 sprintf (stackbuf, "%s;LC_MESSAGES=%s", name1, name2);
1795 }
1796 else
1797 {
1798 if (5 * strlen (name1) + strlen (name2) + 68 >= sizeof (stackbuf))
1799 return NULL;
1800 sprintf (stackbuf,
1801 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s;LC_MESSAGES=%s",
1802 name1, name1, name1, name1, name1, name2);
1803 }
1804 strcpy (resultbuf, stackbuf);
1805 return resultbuf;
1806 }
1807 }
1808 # elif defined __ANDROID__
1809 return setlocale_fixed_null (LC_ALL);
1810 # else
1811 return setlocale (LC_ALL, NULL);
1812 # endif
1813 }
1814
1815 # endif /* NEED_SETLOCALE_IMPROVED */
1816
1817 #endif