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