2 findlocale-0.46.tar.gz from http://icculus.org/~aspirin/findlocale/
4 Copyright (C) 2004 Adam D. Moss (the "Author"). All Rights Reserved.
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
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
40 #include "findlocale.hpp"
43 is_lcchar(const int c) {
48 lang_country_variant_from_envstring(const char *str,
55 /* get lang, if any */
57 while (is_lcchar(str[end])) {
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]);
73 if (str[end] && str[end]!=':') { /* not at end of str */
77 /* get country, if any */
79 while (is_lcchar(str[end])) {
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]);
95 if (str[end] && str[end]!=':') { /* not at end of str */
99 /* get variant, if any */
101 while (str[end] && str[end]!=':') {
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];
120 accumulate_locstring(const char *str, FL_Locale *l) {
122 char *country = NULL;
123 char *variant = NULL;
125 lang_country_variant_from_envstring(str, &lang, &country, &variant);
128 l->country = country;
129 l->variant = variant;
133 free(lang); free(country); free(variant);
140 accumulate_env(const char *name, FL_Locale *l) {
143 char *country = NULL;
144 char *variant = NULL;
147 return accumulate_locstring(env, l);
149 free(lang); free(country); free(variant);
155 canonise_fl(FL_Locale *l) {
156 /* this function fixes some common locale-specifying mistakes */
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");
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");
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)
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"},*/
345 static const IDToCode primary_to_code[] = {
346 {LANG_AFRIKAANS, "af"},
349 {LANG_BULGARIAN, "bg"},
350 /*{LANG_BRETON, "br"},*/
351 {LANG_BELARUSIAN, "by"},
352 {LANG_CATALAN, "ca"},
354 /*{LANG_WELSH, "cy"},*/
358 {LANG_ENGLISH, "en"},
359 /*{LANG_ESPERANTO, "eo"},*/
360 {LANG_SPANISH, "es"},
361 {LANG_ESTONIAN, "et"},
364 {LANG_FINNISH, "fi"},
365 {LANG_FAEROESE, "fo"},
367 /*{LANG_GAELIC, "ga"},*/
368 /*{LANG_GALICIAN, "gl"},*/
369 {LANG_GUJARATI, "gu"},
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"},
380 {LANG_KANNADA, "kn"},
382 /*{LANG_KYRGYZ, "ky"},*/
383 {LANG_LITHUANIAN, "lt"},
384 {LANG_LATVIAN, "lv"},
385 {LANG_MACEDONIAN, "mk"},
386 /*{LANG_MONGOLIAN, "mn"},*/
387 {LANG_MARATHI, "mr"},
389 {LANG_NORWEGIAN, "nb"},
391 {LANG_NORWEGIAN, "nn"},
392 {LANG_NORWEGIAN, "no"},/* unofficial? */
393 {LANG_PUNJABI, "pa"},
395 {LANG_PORTUGUESE, "pt"},
396 {LANG_ROMANIAN, "ro"},
397 {LANG_RUSSIAN, "ru"},
399 {LANG_SLOVENIAN, "sl"},
400 {LANG_ALBANIAN, "sq"},
401 {LANG_SERBIAN, "sr"},
402 {LANG_SWEDISH, "sv"},
403 {LANG_SWAHILI, "sw"},
406 {LANG_TURKISH, "tr"},
408 {LANG_UKRAINIAN, "uk"},
411 {LANG_VIETNAMESE, "vi"},
412 /*{LANG_WALON, "wa"},*/
413 {LANG_CHINESE, "zh"},
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);
421 lcid_to_fl(LCID lcid,
423 LANGID langid = LANGIDFROMLCID(lcid);
424 LANGID primary_lang = PRIMARYLANGID(langid);
426 LANGID sub_lang = SUBLANGID(langid);
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);
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);
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));
457 /* win32 >= mswindows95 */
459 LCID lcid = GetThreadLocale();
460 if (lcid_to_fl(lcid, rtn)) {
461 success = FL_CONFIDENT;
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;
474 /* sv_SE.ISO_8859-1 */
475 /* fr_FR.ISO8859-1 */
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;
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;
496 if (success != FL_FAILED) {
506 FL_FreeLocale(FL_Locale **locale) {
508 FL_Locale *l = *locale;
511 free((void*)l->lang);
514 free((void*)l->country);
517 free((void*)l->variant);