Use findlocale by Adam D. Moss (http://icculus.org/~aspirin/findlocale/)
[supertux.git] / src / tinygettext / findlocale.cpp
1 /*
2  findlocale-0.46.tar.gz from http://icculus.org/~aspirin/findlocale/
3  
4 Copyright (C) 2004 Adam D. Moss (the "Author").  All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is fur-
11 nished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
18 NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
21 NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of the Author of the
24 Software shall not be used in advertising or otherwise to promote the sale,
25 use or other dealings in this Software without prior written authorization
26 from the Author.
27
28 */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #ifdef WIN32
35 #include <windows.h>
36 #include <winnt.h>
37 #endif
38
39 #include "findlocale.hpp"
40
41 static int
42 is_lcchar(const int c) {
43   return isalnum(c);
44 }
45
46 static void
47 lang_country_variant_from_envstring(const char *str,
48                                     char **lang,
49                                     char **country,
50                                     char **variant) {
51   int end = 0;
52   int start;
53
54   /* get lang, if any */
55   start = end;
56   while (is_lcchar(str[end])) {
57     ++end;
58   }
59   if (start != end) {
60     int i;
61     int len = end - start;
62     char *s = (char*) malloc(len + 1);
63     for (i=0; i<len; ++i) {
64       s[i] = tolower(str[start + i]);
65     }
66     s[i] = '\0';
67     *lang = s;
68   } else {
69     *lang = NULL;
70   }
71
72   if (str[end] && str[end]!=':') { /* not at end of str */
73     ++end;
74   }
75
76   /* get country, if any */
77   start = end;
78   while (is_lcchar(str[end])) {
79     ++end;
80   }
81   if (start != end) {
82     int i;
83     int len = end - start;
84     char *s = (char*) malloc(len + 1);
85     for (i=0; i<len; ++i) {
86       s[i] = toupper(str[start + i]);
87     }
88     s[i] = '\0';
89     *country = s;
90   } else {
91     *country = NULL;
92   }
93
94   if (str[end] && str[end]!=':') { /* not at end of str */
95     ++end;
96   }
97
98   /* get variant, if any */
99   start = end;
100   while (str[end] && str[end]!=':') {
101     ++end;
102   }
103   if (start != end) {
104     int i;
105     int len = end - start;
106     char *s = (char*) malloc(len + 1);
107     for (i=0; i<len; ++i) {
108       s[i] = str[start + i];
109     }
110     s[i] = '\0';
111     *variant = s;
112   } else {
113     *variant = NULL;
114   }
115 }
116
117
118 static int
119 accumulate_locstring(const char *str, FL_Locale *l) {
120   char *lang = NULL;
121   char *country = NULL;
122   char *variant = NULL;
123   if (str) {
124     lang_country_variant_from_envstring(str, &lang, &country, &variant);
125     if (lang) {
126       l->lang = lang;
127       l->country = country;
128       l->variant = variant;
129       return 1;
130     }
131   }
132   free(lang); free(country); free(variant);
133   return 0;
134 }
135
136
137 static int
138 accumulate_env(const char *name, FL_Locale *l) {
139   char *env;
140   char *lang = NULL;
141   char *country = NULL;
142   char *variant = NULL;
143   env = getenv(name);
144   if (env) {
145     return accumulate_locstring(env, l);
146   }
147   free(lang); free(country); free(variant);
148   return 0;
149 }
150
151
152 static void
153 canonise_fl(FL_Locale *l) {
154   /* this function fixes some common locale-specifying mistakes */
155   /* en_UK -> en_GB */
156   if (l->lang && 0 == strcmp(l->lang, "en")) {
157     if (l->country && 0 == strcmp(l->country, "UK")) {
158       free((void*)l->country);
159       l->country = strdup("GB");
160     }
161   }
162   /* ja_JA -> ja_JP */
163   if (l->lang && 0 == strcmp(l->lang, "ja")) {
164     if (l->country && 0 == strcmp(l->country, "JA")) {
165       free((void*)l->country);
166       l->country = strdup("JP");
167     }
168   }
169 }
170
171
172 #ifdef WIN32
173 #include <stdio.h>
174 #define ML(pn,sn) MAKELANGID(LANG_##pn, SUBLANG_##pn##_##sn)
175 #define MLN(pn) MAKELANGID(LANG_##pn, SUBLANG_DEFAULT)
176 #define RML(pn,sn) MAKELANGID(LANG_##pn, SUBLANG_##sn)
177 typedef struct {
178   LANGID id;
179   char*  code;
180 } IDToCode;
181 static const IDToCode both_to_code[] = {
182   {ML(ENGLISH,US),           "en_US.ISO_8859-1"},
183   {ML(ENGLISH,CAN),          "en_CA"}, /* english / canadian */
184   {ML(ENGLISH,UK),           "en_GB"},
185   {ML(ENGLISH,EIRE),         "en_IE"},
186   {ML(ENGLISH,AUS),          "en_AU"},
187   {MLN(GERMAN),              "de_DE"},
188   {MLN(SPANISH),             "es_ES"},
189   {ML(SPANISH,MEXICAN),      "es_MX"},
190   {MLN(FRENCH),              "fr_FR"},
191   {ML(FRENCH,CANADIAN),      "fr_CA"},
192   {ML(FRENCH,BELGIAN),       "fr_BE"}, /* ? */
193   {ML(DUTCH,BELGIAN),        "nl_BE"}, /* ? */
194   {ML(PORTUGUESE,BRAZILIAN), "pt_BR"},
195   {MLN(PORTUGUESE),          "pt_PT"},
196   {MLN(SWEDISH),             "sv_SE"},
197   {ML(CHINESE,HONGKONG),     "zh_HK"},
198   /* these are machine-generated and not yet verified */
199   {RML(AFRIKAANS,DEFAULT), "af_ZA"},
200   {RML(ALBANIAN,DEFAULT), "sq_AL"},
201   {RML(ARABIC,ARABIC_ALGERIA), "ar_DZ"},
202   {RML(ARABIC,ARABIC_BAHRAIN), "ar_BH"},
203   {RML(ARABIC,ARABIC_EGYPT), "ar_EG"},
204   {RML(ARABIC,ARABIC_IRAQ), "ar_IQ"},
205   {RML(ARABIC,ARABIC_JORDAN), "ar_JO"},
206   {RML(ARABIC,ARABIC_KUWAIT), "ar_KW"},
207   {RML(ARABIC,ARABIC_LEBANON), "ar_LB"},
208   {RML(ARABIC,ARABIC_LIBYA), "ar_LY"},
209   {RML(ARABIC,ARABIC_MOROCCO), "ar_MA"},
210   {RML(ARABIC,ARABIC_OMAN), "ar_OM"},
211   {RML(ARABIC,ARABIC_QATAR), "ar_QA"},
212   {RML(ARABIC,ARABIC_SAUDI_ARABIA), "ar_SA"},
213   {RML(ARABIC,ARABIC_SYRIA), "ar_SY"},
214   {RML(ARABIC,ARABIC_TUNISIA), "ar_TN"},
215   {RML(ARABIC,ARABIC_UAE), "ar_AE"},
216   {RML(ARABIC,ARABIC_YEMEN), "ar_YE"},
217   {RML(ARMENIAN,DEFAULT), "hy_AM"},
218   {RML(AZERI,AZERI_CYRILLIC), "az_AZ"},
219   {RML(AZERI,AZERI_LATIN), "az_AZ"},
220   {RML(BASQUE,DEFAULT), "eu_ES"},
221   {RML(BELARUSIAN,DEFAULT), "be_BY"},
222 /*{RML(BRETON,DEFAULT), "br_FR"},*/
223   {RML(BULGARIAN,DEFAULT), "bg_BG"},
224   {RML(CATALAN,DEFAULT), "ca_ES"},
225   {RML(CHINESE,CHINESE_HONGKONG), "zh_HK"},
226   {RML(CHINESE,CHINESE_MACAU), "zh_MO"},
227   {RML(CHINESE,CHINESE_SIMPLIFIED), "zh_CN"},
228   {RML(CHINESE,CHINESE_SINGAPORE), "zh_SG"},
229   {RML(CHINESE,CHINESE_TRADITIONAL), "zh_TW"},
230 /*{RML(CORNISH,DEFAULT), "kw_GB"},*/
231   {RML(CZECH,DEFAULT), "cs_CZ"},
232   {RML(DANISH,DEFAULT), "da_DK"},
233   {RML(DUTCH,DUTCH), "nl_NL"},
234   {RML(DUTCH,DUTCH_BELGIAN), "nl_BE"},
235 /*{RML(DUTCH,DUTCH_SURINAM), "nl_SR"},*/
236   {RML(ENGLISH,ENGLISH_AUS), "en_AU"},
237   {RML(ENGLISH,ENGLISH_BELIZE), "en_BZ"},
238   {RML(ENGLISH,ENGLISH_CAN), "en_CA"},
239   {RML(ENGLISH,ENGLISH_CARIBBEAN), "en_CB"},
240   {RML(ENGLISH,ENGLISH_EIRE), "en_IE"},
241   {RML(ENGLISH,ENGLISH_JAMAICA), "en_JM"},
242   {RML(ENGLISH,ENGLISH_NZ), "en_NZ"},
243   {RML(ENGLISH,ENGLISH_PHILIPPINES), "en_PH"},
244   {RML(ENGLISH,ENGLISH_SOUTH_AFRICA), "en_ZA"},
245   {RML(ENGLISH,ENGLISH_TRINIDAD), "en_TT"},
246   {RML(ENGLISH,ENGLISH_UK), "en_GB"},
247   {RML(ENGLISH,ENGLISH_US), "en_US"},
248   {RML(ENGLISH,ENGLISH_ZIMBABWE), "en_ZW"},
249 /*{RML(ESPERANTO,DEFAULT), "eo_"},*/
250   {RML(ESTONIAN,DEFAULT), "et_EE"},
251   {RML(FAEROESE,DEFAULT), "fo_FO"},
252   {RML(FARSI,DEFAULT), "fa_IR"},
253   {RML(FINNISH,DEFAULT), "fi_FI"},
254   {RML(FRENCH,FRENCH), "fr_FR"},
255   {RML(FRENCH,FRENCH_BELGIAN), "fr_BE"},
256   {RML(FRENCH,FRENCH_CANADIAN), "fr_CA"},
257   {RML(FRENCH,FRENCH_LUXEMBOURG), "fr_LU"},
258   {RML(FRENCH,FRENCH_MONACO), "fr_MC"},
259   {RML(FRENCH,FRENCH_SWISS), "fr_CH"},
260 /*{RML(GAELIC,GAELIC), "ga_IE"},*/
261 /*{RML(GAELIC,GAELIC_MANX), "gv_GB"},*/
262 /*{RML(GAELIC,GAELIC_SCOTTISH), "gd_GB"},*/
263 /*{RML(GALICIAN,DEFAULT), "gl_ES"},*/
264   {RML(GEORGIAN,DEFAULT), "ka_GE"},
265   {RML(GERMAN,GERMAN), "de_DE"},
266   {RML(GERMAN,GERMAN_AUSTRIAN), "de_AT"},
267   {RML(GERMAN,GERMAN_LIECHTENSTEIN), "de_LI"},
268   {RML(GERMAN,GERMAN_LUXEMBOURG), "de_LU"},
269   {RML(GERMAN,GERMAN_SWISS), "de_CH"},
270   {RML(GREEK,DEFAULT), "el_GR"},
271   {RML(GUJARATI,DEFAULT), "gu_IN"},
272   {RML(HEBREW,DEFAULT), "he_IL"},
273   {RML(HINDI,DEFAULT), "hi_IN"},
274   {RML(HUNGARIAN,DEFAULT), "hu_HU"},
275   {RML(ICELANDIC,DEFAULT), "is_IS"},
276   {RML(INDONESIAN,DEFAULT), "id_ID"},
277   {RML(ITALIAN,ITALIAN), "it_IT"},
278   {RML(ITALIAN,ITALIAN_SWISS), "it_CH"},
279   {RML(JAPANESE,DEFAULT), "ja_JP"},
280   {RML(KANNADA,DEFAULT), "kn_IN"},
281   {RML(KAZAK,DEFAULT), "kk_KZ"},
282   {RML(KONKANI,DEFAULT), "kok_IN"},
283   {RML(KOREAN,KOREAN), "ko_KR"},
284 /*{RML(KYRGYZ,DEFAULT), "ky_KG"},*/
285   {RML(LATVIAN,DEFAULT), "lv_LV"},
286   {RML(LITHUANIAN,LITHUANIAN), "lt_LT"},
287   {RML(MACEDONIAN,DEFAULT), "mk_MK"},
288   {RML(MALAY,MALAY_BRUNEI_DARUSSALAM), "ms_BN"},
289   {RML(MALAY,MALAY_MALAYSIA), "ms_MY"},
290   {RML(MARATHI,DEFAULT), "mr_IN"},
291 /*{RML(MONGOLIAN,DEFAULT), "mn_MN"},*/
292   {RML(NORWEGIAN,NORWEGIAN_BOKMAL), "nb_NO"},
293   {RML(NORWEGIAN,NORWEGIAN_NYNORSK), "nn_NO"},
294   {RML(POLISH,DEFAULT), "pl_PL"},
295   {RML(PORTUGUESE,PORTUGUESE), "pt_PT"},
296   {RML(PORTUGUESE,PORTUGUESE_BRAZILIAN), "pt_BR"},
297   {RML(PUNJABI,DEFAULT), "pa_IN"},
298   {RML(ROMANIAN,DEFAULT), "ro_RO"},
299   {RML(RUSSIAN,DEFAULT), "ru_RU"},
300   {RML(SANSKRIT,DEFAULT), "sa_IN"},
301   {RML(SERBIAN,DEFAULT), "hr_HR"},
302   {RML(SERBIAN,SERBIAN_CYRILLIC), "sr_SP"},
303   {RML(SERBIAN,SERBIAN_LATIN), "sr_SP"},
304   {RML(SLOVAK,DEFAULT), "sk_SK"},
305   {RML(SLOVENIAN,DEFAULT), "sl_SI"},
306   {RML(SPANISH,SPANISH), "es_ES"},
307   {RML(SPANISH,SPANISH_ARGENTINA), "es_AR"},
308   {RML(SPANISH,SPANISH_BOLIVIA), "es_BO"},
309   {RML(SPANISH,SPANISH_CHILE), "es_CL"},
310   {RML(SPANISH,SPANISH_COLOMBIA), "es_CO"},
311   {RML(SPANISH,SPANISH_COSTA_RICA), "es_CR"},
312   {RML(SPANISH,SPANISH_DOMINICAN_REPUBLIC), "es_DO"},
313   {RML(SPANISH,SPANISH_ECUADOR), "es_EC"},
314   {RML(SPANISH,SPANISH_EL_SALVADOR), "es_SV"},
315   {RML(SPANISH,SPANISH_GUATEMALA), "es_GT"},
316   {RML(SPANISH,SPANISH_HONDURAS), "es_HN"},
317   {RML(SPANISH,SPANISH_MEXICAN), "es_MX"},
318   {RML(SPANISH,SPANISH_MODERN), "es_ES"},
319   {RML(SPANISH,SPANISH_NICARAGUA), "es_NI"},
320   {RML(SPANISH,SPANISH_PANAMA), "es_PA"},
321   {RML(SPANISH,SPANISH_PARAGUAY), "es_PY"},
322   {RML(SPANISH,SPANISH_PERU), "es_PE"},
323   {RML(SPANISH,SPANISH_PUERTO_RICO), "es_PR"},
324   {RML(SPANISH,SPANISH_URUGUAY), "es_UY"},
325   {RML(SPANISH,SPANISH_VENEZUELA), "es_VE"},
326   {RML(SWAHILI,DEFAULT), "sw_KE"},
327   {RML(SWEDISH,SWEDISH), "sv_SE"},
328   {RML(SWEDISH,SWEDISH_FINLAND), "sv_FI"},
329 /*{RML(SYRIAC,DEFAULT), "syr_SY"},*/
330   {RML(TAMIL,DEFAULT), "ta_IN"},
331   {RML(TATAR,DEFAULT), "tt_TA"},
332   {RML(TELUGU,DEFAULT), "te_IN"},
333   {RML(THAI,DEFAULT), "th_TH"},
334   {RML(TURKISH,DEFAULT), "tr_TR"},
335   {RML(UKRAINIAN,DEFAULT), "uk_UA"},
336   {RML(URDU,URDU_PAKISTAN), "ur_PK"},
337   {RML(UZBEK,UZBEK_CYRILLIC), "uz_UZ"},
338   {RML(UZBEK,UZBEK_LATIN), "uz_UZ"},
339   {RML(VIETNAMESE,DEFAULT), "vi_VN"},
340 /*{RML(WALON,DEFAULT), "wa_BE"},*/
341 /*{RML(WELSH,DEFAULT), "cy_GB"},*/
342 };
343 static const IDToCode primary_to_code[] = {
344   {LANG_AFRIKAANS,  "af"},
345   {LANG_ARABIC,     "ar"},
346   {LANG_AZERI,      "az"},
347   {LANG_BULGARIAN,  "bg"},
348 /*{LANG_BRETON,     "br"},*/
349   {LANG_BELARUSIAN, "by"},
350   {LANG_CATALAN,    "ca"},
351   {LANG_CZECH,      "cs"},
352 /*{LANG_WELSH,      "cy"},*/
353   {LANG_DANISH,     "da"},
354   {LANG_GERMAN,     "de"},
355   {LANG_GREEK,      "el"},
356   {LANG_ENGLISH,    "en"},
357 /*{LANG_ESPERANTO,  "eo"},*/
358   {LANG_SPANISH,    "es"},
359   {LANG_ESTONIAN,   "et"},
360   {LANG_BASQUE,     "eu"},
361   {LANG_FARSI,      "fa"},
362   {LANG_FINNISH,    "fi"},
363   {LANG_FAEROESE,   "fo"},
364   {LANG_FRENCH,     "fr"},
365 /*{LANG_GAELIC,     "ga"},*/
366 /*{LANG_GALICIAN,   "gl"},*/
367   {LANG_GUJARATI,   "gu"},
368   {LANG_HEBREW,     "he"},
369   {LANG_HINDI,      "hi"},
370   {LANG_SERBIAN,    "hr"},
371   {LANG_HUNGARIAN,  "hu"},
372   {LANG_ARMENIAN,   "hy"},
373   {LANG_INDONESIAN, "id"},
374   {LANG_ITALIAN,    "it"},
375   {LANG_JAPANESE,   "ja"},
376   {LANG_GEORGIAN,   "ka"},
377   {LANG_KAZAK,      "kk"},
378   {LANG_KANNADA,    "kn"},
379   {LANG_KOREAN,     "ko"},
380 /*{LANG_KYRGYZ,     "ky"},*/
381   {LANG_LITHUANIAN, "lt"},
382   {LANG_LATVIAN,    "lv"},
383   {LANG_MACEDONIAN, "mk"},
384 /*{LANG_MONGOLIAN,  "mn"},*/
385   {LANG_MARATHI,    "mr"},
386   {LANG_MALAY,      "ms"},
387   {LANG_NORWEGIAN,  "nb"},
388   {LANG_DUTCH,      "nl"},
389   {LANG_NORWEGIAN,  "nn"},
390   {LANG_NORWEGIAN,  "no"},/* unofficial? */
391   {LANG_PUNJABI,    "pa"},
392   {LANG_POLISH,     "pl"},
393   {LANG_PORTUGUESE, "pt"},
394   {LANG_ROMANIAN,   "ro"},
395   {LANG_RUSSIAN,    "ru"},
396   {LANG_SLOVAK,     "sk"},
397   {LANG_SLOVENIAN,  "sl"},
398   {LANG_ALBANIAN,   "sq"},
399   {LANG_SERBIAN,    "sr"},
400   {LANG_SWEDISH,    "sv"},
401   {LANG_SWAHILI,    "sw"},
402   {LANG_TAMIL,      "ta"},
403   {LANG_THAI,       "th"},
404   {LANG_TURKISH,    "tr"},
405   {LANG_TATAR,      "tt"},
406   {LANG_UKRAINIAN,  "uk"},
407   {LANG_URDU,       "ur"},
408   {LANG_UZBEK,      "uz"},
409   {LANG_VIETNAMESE, "vi"},
410 /*{LANG_WALON,      "wa"},*/
411   {LANG_CHINESE,    "zh"},
412 };
413 static int num_primary_to_code =
414   sizeof(primary_to_code) / sizeof(*primary_to_code);
415 static int num_both_to_code =
416   sizeof(both_to_code) / sizeof(*both_to_code);
417
418 static const int
419 lcid_to_fl(LCID lcid,
420            FL_Locale *rtn) {
421   LANGID langid       = LANGIDFROMLCID(lcid);
422   LANGID primary_lang = PRIMARYLANGID(langid);
423   LANGID sub_lang     = SUBLANGID(langid);
424   int i;
425   /* try to find an exact primary/sublanguage combo that we know about */
426   for (i=0; i<num_both_to_code; ++i) {
427     if (both_to_code[i].id == langid) {
428       accumulate_locstring(both_to_code[i].code, rtn);
429       return 1;
430     }
431   }
432   /* fallback to just checking the primary language id */
433   for (i=0; i<num_primary_to_code; ++i) {
434     if (primary_to_code[i].id == primary_lang) {
435       accumulate_locstring(primary_to_code[i].code, rtn);
436       return 1;
437     }
438   }
439   return 0;
440 }
441 #endif
442
443
444 FL_Success
445 FL_FindLocale(FL_Locale **locale, FL_Domain /*domain*/) {
446   FL_Success success = FL_FAILED;
447   FL_Locale *rtn = (FL_Locale*) malloc(sizeof(FL_Locale));
448   rtn->lang = NULL;
449   rtn->country = NULL;
450   rtn->variant = NULL;
451
452 #ifdef WIN32
453   /* win32 >= mswindows95 */
454   {
455     LCID lcid = GetThreadLocale();
456     if (lcid_to_fl(lcid, rtn)) {
457       success = FL_CONFIDENT;
458     }
459     if (success == FL_FAILED) {
460       /* assume US English on mswindows systems unless we know otherwise */
461       if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
462         success = FL_DEFAULT_GUESS;
463       }
464     }
465   }
466 #else
467   /* assume unixoid */
468   {
469     /* examples: */
470     /* sv_SE.ISO_8859-1 */
471     /* fr_FR.ISO8859-1 */
472     /* no_NO_NB */
473     /* no_NO_NY */
474     /* no_NO */
475     /* de_DE */
476     /* try the various vars in decreasing order of authority */
477     if (accumulate_env("LC_ALL", rtn) ||
478         accumulate_env("LC_MESSAGES", rtn) ||
479         accumulate_env("LANG", rtn) ||
480         accumulate_env("LANGUAGE", rtn)) {
481       success = FL_CONFIDENT;
482     }
483     if (success == FL_FAILED) {
484       /* assume US English on unixoid systems unless we know otherwise */
485       if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
486         success = FL_DEFAULT_GUESS;
487       }
488     }
489   }
490 #endif
491
492   if (success != FL_FAILED) {
493     canonise_fl(rtn);
494   }
495
496   *locale = rtn;
497   return success;
498 }
499
500
501 void
502 FL_FreeLocale(FL_Locale **locale) {
503   if (locale) {
504     FL_Locale *l = *locale;
505     if (l) {
506       if (l->lang) {
507         free((void*)l->lang);
508       }
509       if (l->country) {
510         free((void*)l->country);
511       }
512       if (l->variant) {
513         free((void*)l->variant);
514       }
515       free(l);
516       *locale = NULL;
517     }
518   }
519 }