1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 #include <rtl/ustring.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/strbuf.hxx>
34 
35 #include "i18npool/mslangid.hxx"
36 
37 // =======================================================================
38 
39 struct IsoLangEngEntry
40 {
41     LanguageType        mnLang;
42     sal_Char            maCountry[3];
43 };
44 
45 struct IsoLangNoneStdEntry
46 {
47     LanguageType        mnLang;
48     sal_Char            maLangStr[4];
49     sal_Char            maCountry[9];
50 };
51 
52 struct IsoLangOtherEntry
53 {
54     LanguageType        mnLang;
55     const sal_Char*     mpLangStr;
56 };
57 
58 // -----------------------------------------------------------------------
59 
60 // Entries for languages are lower case, for countries upper case, as
61 // recommended by rfc4646 (obsoletes rfc3066 (obsoletes rfc1766)).
62 // convertIsoNamesToLanguage() is case insensitive
63 //
64 // Sort order: Most used first.
65 //
66 // The default entry for a LangID <-> ISO mapping has to be first. For
67 // conversion of legacy mappings one LangID can map to multiple ISO codes, and
68 // one ISO code combination can map to multiple LangIDs. For compatibility with
69 // already existing calls it can also be a sequence as follows:
70 
71 // LANGUAGE_ENGLISH,    "en", ""
72 // LANGUAGE_ENGLISH_US, "en", "US"
73 
74 // Here, in a convertIsoNamesToLanguage() call "en-US" is converted to
75 // LANGUAGE_ENGLISH_US and "en" is converted to LANGUAGE_ENGLISH. A call with
76 // "en-ZZ" (not in table) would result in LANGUAGE_ENGLISH because the first
77 // entry matching the language and not having a country is returned, regardless
78 // of whether being sorted before or after other entries of the same language
79 // with some country. To obtain a _locale_ (not language only) in the order
80 // given, convertLocaleToLanguageWithFallback() must be called.
81 
82 // If the sequence instead was
83 
84 // LANGUAGE_ENGLISH_US, "en", "US"
85 // LANGUAGE_ENGLISH,    "en", ""
86 
87 // in a convertIsoNamesToLanguage() call "en-US" is still converted to
88 // LANGUAGE_ENGLISH_US, but "en" is _also_ converted to LANGUAGE_ENGLISH_US
89 // because no country was passed and it is the first entry to match the
90 // language, see code. A call with "en-ZZ" (not in table) would still result in
91 // LANGUAGE_ENGLISH.
92 
93 /* erAck: 2007-07-05T20:01+0200  TODO: The entire suite's "primary language
94  * only" usage and locale fall back should be cleaned up and made consistent. I
95  * strongly doubt that most callers exactly expect the behavior described.
96  * Currently these primary LangIDs are used literally in OOo code:
97  * LANGUAGE_ENGLISH LANGUAGE_CHINESE LANGUAGE_MALAY
98  * LANGUAGE_AZERI LANGUAGE_URDU LANGUAGE_KASHMIRI
99  */
100 
101 static MsLangId::IsoLangEntry const aImplIsoLangEntries[] =
102 {
103     // MS-LANGID codes               ISO639-1/2/3 ISO3166
104     { LANGUAGE_ENGLISH,                     "en", ""   },
105     { LANGUAGE_ENGLISH_US,                  "en", "US" },
106     { LANGUAGE_ENGLISH_UK,                  "en", "GB" },
107     { LANGUAGE_ENGLISH_AUS,                 "en", "AU" },
108     { LANGUAGE_ENGLISH_CAN,                 "en", "CA" },
109     { LANGUAGE_FRENCH,                      "fr", "FR" },
110     { LANGUAGE_FRENCH,                      "fr", ""   },
111     { LANGUAGE_GERMAN,                      "de", "DE" },
112     { LANGUAGE_ITALIAN,                     "it", "IT" },
113     { LANGUAGE_DUTCH,                       "nl", "NL" },
114     { LANGUAGE_SPANISH_MODERN,              "es", "ES" },
115     { LANGUAGE_SPANISH_DATED,               "es", "ES" },
116     { LANGUAGE_PORTUGUESE,                  "pt", "PT" },
117     { LANGUAGE_PORTUGUESE_BRAZILIAN,        "pt", "BR" },
118     { LANGUAGE_DANISH,                      "da", "DK" },
119     { LANGUAGE_GREEK,                       "el", "GR" },
120     { LANGUAGE_CHINESE,                     "zh", ""   },
121     { LANGUAGE_CHINESE_SIMPLIFIED,          "zh", "CN" },
122     { LANGUAGE_CHINESE_TRADITIONAL,         "zh", "TW" },
123     { LANGUAGE_CHINESE_HONGKONG,            "zh", "HK" },
124     { LANGUAGE_CHINESE_SINGAPORE,           "zh", "SG" },
125     { LANGUAGE_CHINESE_MACAU,               "zh", "MO" },
126     { LANGUAGE_ENGLISH_HONG_KONG_SAR,       "en", "HK" },
127     { LANGUAGE_JAPANESE,                    "ja", "JP" },
128     { LANGUAGE_KOREAN,                      "ko", "KR" },
129     { LANGUAGE_KOREAN_JOHAB,                "ko", "KR" },
130     { LANGUAGE_USER_KOREAN_NORTH,           "ko", "KP" },
131     { LANGUAGE_SWEDISH,                     "sv", "SE" },
132     { LANGUAGE_SWEDISH_FINLAND,             "sv", "FI" },
133     { LANGUAGE_FINNISH,                     "fi", "FI" },
134     { LANGUAGE_RUSSIAN,                     "ru", "RU" },
135     { LANGUAGE_TATAR,                       "tt", "RU" },
136     { LANGUAGE_ENGLISH_NZ,                  "en", "NZ" },
137     { LANGUAGE_ENGLISH_EIRE,                "en", "IE" },
138     { LANGUAGE_DUTCH_BELGIAN,               "nl", "BE" },
139     { LANGUAGE_FRENCH_BELGIAN,              "fr", "BE" },
140     { LANGUAGE_FRENCH_CANADIAN,             "fr", "CA" },
141     { LANGUAGE_FRENCH_SWISS,                "fr", "CH" },
142     { LANGUAGE_GERMAN_SWISS,                "de", "CH" },
143     { LANGUAGE_GERMAN_AUSTRIAN,             "de", "AT" },
144     { LANGUAGE_ITALIAN_SWISS,               "it", "CH" },
145     { LANGUAGE_ALBANIAN,                    "sq", "AL" },
146     { LANGUAGE_ARABIC_SAUDI_ARABIA,         "ar", "SA" },
147     { LANGUAGE_ARABIC_EGYPT,                "ar", "EG" },
148     { LANGUAGE_ARABIC_UAE,                  "ar", "AE" },
149     { LANGUAGE_ARABIC_IRAQ,                 "ar", "IQ" },
150     { LANGUAGE_ARABIC_LIBYA,                "ar", "LY" },
151     { LANGUAGE_ARABIC_ALGERIA,              "ar", "DZ" },
152     { LANGUAGE_ARABIC_MOROCCO,              "ar", "MA" },
153     { LANGUAGE_ARABIC_TUNISIA,              "ar", "TN" },
154     { LANGUAGE_ARABIC_OMAN,                 "ar", "OM" },
155     { LANGUAGE_ARABIC_YEMEN,                "ar", "YE" },
156     { LANGUAGE_ARABIC_SYRIA,                "ar", "SY" },
157     { LANGUAGE_ARABIC_JORDAN,               "ar", "JO" },
158     { LANGUAGE_ARABIC_LEBANON,              "ar", "LB" },
159     { LANGUAGE_ARABIC_KUWAIT,               "ar", "KW" },
160     { LANGUAGE_ARABIC_BAHRAIN,              "ar", "BH" },
161     { LANGUAGE_ARABIC_QATAR,                "ar", "QA" },
162     { LANGUAGE_USER_ARABIC_CHAD,            "ar", "TD" },
163     { LANGUAGE_USER_ARABIC_COMOROS,         "ar", "KM" },
164     { LANGUAGE_USER_ARABIC_DJIBOUTI,        "ar", "DJ" },
165     { LANGUAGE_USER_ARABIC_ERITREA,         "ar", "ER" },
166     { LANGUAGE_USER_ARABIC_ISRAEL,          "ar", "IL" },
167     { LANGUAGE_USER_ARABIC_MAURITANIA,      "ar", "MR" },
168     { LANGUAGE_USER_ARABIC_PALESTINE,       "ar", "PS" },
169     { LANGUAGE_USER_ARABIC_SOMALIA,         "ar", "SO" },
170     { LANGUAGE_USER_ARABIC_SUDAN,           "ar", "SD" },
171     { LANGUAGE_ARABIC_PRIMARY_ONLY,         "ar", ""   },
172     { LANGUAGE_BASQUE,                      "eu", ""   },
173     { LANGUAGE_BULGARIAN,                   "bg", "BG" },
174     { LANGUAGE_CZECH,                       "cs", "CZ" },
175     { LANGUAGE_CZECH,                       "cz", ""   },
176     { LANGUAGE_ENGLISH_JAMAICA,             "en", "JM" },
177     { LANGUAGE_ENGLISH_CARRIBEAN,           "en", "BS" },   // not 100%, because AG is Bahamas
178     { LANGUAGE_ENGLISH_BELIZE,              "en", "BZ" },
179     { LANGUAGE_ENGLISH_TRINIDAD,            "en", "TT" },
180     { LANGUAGE_ENGLISH_ZIMBABWE,            "en", "ZW" },
181     { LANGUAGE_ENGLISH_INDONESIA,           "en", "ID" },
182     { LANGUAGE_ESTONIAN,                    "et", "EE" },
183     { LANGUAGE_FAEROESE,                    "fo", "FO" },
184     { LANGUAGE_FARSI,                       "fa", "IR" },
185     { LANGUAGE_FRENCH_LUXEMBOURG,           "fr", "LU" },
186     { LANGUAGE_FRENCH_MONACO,               "fr", "MC" },
187     { LANGUAGE_GERMAN_LUXEMBOURG,           "de", "LU" },
188     { LANGUAGE_GERMAN_LIECHTENSTEIN,        "de", "LI" },
189     { LANGUAGE_HEBREW,                      "he", "IL" },   // new: old was "iw"
190     { LANGUAGE_HEBREW,                      "iw", "IL" },   // old: new is "he"
191     { LANGUAGE_HUNGARIAN,                   "hu", "HU" },
192     { LANGUAGE_ICELANDIC,                   "is", "IS" },
193     { LANGUAGE_INDONESIAN,                  "id", "ID" },   // new: old was "in"
194     { LANGUAGE_INDONESIAN,                  "in", "ID" },   // old: new is "id"
195     { LANGUAGE_NORWEGIAN,                   "no", "NO" },
196     { LANGUAGE_NORWEGIAN_BOKMAL,            "nb", "NO" },
197     { LANGUAGE_NORWEGIAN_NYNORSK,           "nn", "NO" },
198     { LANGUAGE_POLISH,                      "pl", "PL" },
199     { LANGUAGE_RHAETO_ROMAN,                "rm", "CH" },
200     { LANGUAGE_ROMANIAN,                    "ro", "RO" },
201     { LANGUAGE_ROMANIAN_MOLDOVA,            "ro", "MD" },
202     { LANGUAGE_SLOVAK,                      "sk", "SK" },
203     { LANGUAGE_SLOVENIAN,                   "sl", "SI" },
204     { LANGUAGE_SPANISH_MEXICAN,             "es", "MX" },
205     { LANGUAGE_SPANISH_GUATEMALA,           "es", "GT" },
206     { LANGUAGE_SPANISH_COSTARICA,           "es", "CR" },
207     { LANGUAGE_SPANISH_PANAMA,              "es", "PA" },
208     { LANGUAGE_SPANISH_DOMINICAN_REPUBLIC,  "es", "DO" },
209     { LANGUAGE_SPANISH_VENEZUELA,           "es", "VE" },
210     { LANGUAGE_SPANISH_COLOMBIA,            "es", "CO" },
211     { LANGUAGE_SPANISH_PERU,                "es", "PE" },
212     { LANGUAGE_SPANISH_ARGENTINA,           "es", "AR" },
213     { LANGUAGE_SPANISH_ECUADOR,             "es", "EC" },
214     { LANGUAGE_SPANISH_CHILE,               "es", "CL" },
215     { LANGUAGE_SPANISH_URUGUAY,             "es", "UY" },
216     { LANGUAGE_SPANISH_PARAGUAY,            "es", "PY" },
217     { LANGUAGE_SPANISH_BOLIVIA,             "es", "BO" },
218     { LANGUAGE_SPANISH_EL_SALVADOR,         "es", "SV" },
219     { LANGUAGE_SPANISH_HONDURAS,            "es", "HN" },
220     { LANGUAGE_SPANISH_NICARAGUA,           "es", "NI" },
221     { LANGUAGE_SPANISH_PUERTO_RICO,         "es", "PR" },
222     { LANGUAGE_SPANISH_UNITED_STATES,       "es", "US" },
223     { LANGUAGE_SPANISH_LATIN_AMERICA,       "es", ""   },
224     { LANGUAGE_TURKISH,                     "tr", "TR" },
225     { LANGUAGE_UKRAINIAN,                   "uk", "UA" },
226     { LANGUAGE_VIETNAMESE,                  "vi", "VN" },
227     { LANGUAGE_LATVIAN,                     "lv", "LV" },
228     { LANGUAGE_MACEDONIAN,                  "mk", "MK" },
229     { LANGUAGE_MALAY,                       "ms", ""   },
230     { LANGUAGE_MALAY_MALAYSIA,              "ms", "MY" },
231     { LANGUAGE_MALAY_BRUNEI_DARUSSALAM,     "ms", "BN" },
232     { LANGUAGE_ENGLISH_MALAYSIA,            "en", "MY" },
233     { LANGUAGE_THAI,                        "th", "TH" },
234     { LANGUAGE_LITHUANIAN,                  "lt", "LT" },
235     { LANGUAGE_LITHUANIAN_CLASSIC,          "lt", "LT" },
236     { LANGUAGE_CROATIAN,                    "hr", "HR" },   // Croatian in Croatia
237     { LANGUAGE_CROATIAN_BOSNIA_HERZEGOVINA, "hr", "BA" },
238     { LANGUAGE_BOSNIAN_LATIN_BOSNIA_HERZEGOVINA,        "bs", "BA" },
239 //  { LANGUAGE_BOSNIAN_CYRILLIC_BOSNIA_AND_HERZEGOVINA, "bs", "BA" },   // script codes not supported yet
240     { LANGUAGE_USER_SERBIAN_CYRILLIC_SERBIA,        "sr", "RS" },   // Serbian Cyrillic in Serbia
241     { LANGUAGE_SERBIAN_CYRILLIC,                    "sr", "YU" },   // legacy Serbian Cyrillic in Serbia and Montenegro (former Yugoslavia); kludge, needed to be sr_CS instead, sr_CS not supported by ICU 2.6 (3.4 does)
242     { LANGUAGE_SERBIAN_CYRILLIC,                    "sr", "CS" },   // alias to be able to integrate localizations, rsc needs it
243     { LANGUAGE_USER_SERBIAN_CYRILLIC_MONTENEGRO,    "sr", "ME" },
244     { LANGUAGE_SERBIAN_CYRILLIC_BOSNIA_HERZEGOVINA, "sr", "BA" },
245     { LANGUAGE_SERBIAN,                             "sr", ""   },   // SERBIAN is only LID, MS-LCID not defined (was dupe of CROATIAN)
246     { LANGUAGE_USER_SERBIAN_LATIN_SERBIA,           "sh", "RS" },   // Serbian Latin in Serbia; kludge, needed to be sr_Latn_RS instead, script codes not supported yet
247     { LANGUAGE_SERBIAN_LATIN,                       "sh", "YU" },   // legacy Serbian Latin in Serbia and Montenegro (former Yugoslavia); kludge, needed to be sr_Latn_CS instead, script codes not supported yet
248     { LANGUAGE_SERBIAN_LATIN,                       "sh", "CS" },   // Serbian Latin in Serbia and Montenegro; kludge, needed to be sr_Latn_CS instead, script codes not supported yet
249     { LANGUAGE_USER_SERBIAN_LATIN_MONTENEGRO,       "sh", "ME" },   // Serbian Latin in Montenegro; kludge, needed to be sr_Latn_ME instead, script codes not supported yet
250     { LANGUAGE_SERBIAN_LATIN_BOSNIA_HERZEGOVINA,    "sh", "BA" },
251     { LANGUAGE_SERBIAN_LATIN_NEUTRAL,               "sh", ""   },   // kludge, needed to be sr_Latn instead, script codes not supported yet
252     { LANGUAGE_ARMENIAN,                    "hy", "AM" },
253     { LANGUAGE_AZERI,                       "az", ""   },
254     { LANGUAGE_AZERI_LATIN,                 "az", "AZ" },
255 //  { LANGUAGE_AZERI_CYRILLIC,              "az", "AZ" },   // script codes not supported yet
256     { LANGUAGE_UZBEK_LATIN,                 "uz", "UZ" },
257 //  { LANGUAGE_UZBEK_CYRILLIC,              "uz", "UZ" },   // script codes not supported yet
258     { LANGUAGE_BENGALI_BANGLADESH,          "bn", "BD" },
259     { LANGUAGE_BENGALI,                     "bn", "IN" },
260     { LANGUAGE_BURMESE,                     "my", "MM" },
261     { LANGUAGE_KAZAK,                       "kk", "KZ" },
262     { LANGUAGE_ENGLISH_INDIA,               "en", "IN" },
263     { LANGUAGE_URDU,                        "ur", ""   },
264     { LANGUAGE_URDU_INDIA,                  "ur", "IN" },
265     { LANGUAGE_URDU_PAKISTAN,               "ur", "PK" },
266     { LANGUAGE_HINDI,                       "hi", "IN" },
267     { LANGUAGE_GUJARATI,                    "gu", "IN" },
268     { LANGUAGE_KANNADA,                     "kn", "IN" },
269     { LANGUAGE_ASSAMESE,                    "as", "IN" },
270     { LANGUAGE_KASHMIRI,                    "ks", ""   },
271     { LANGUAGE_KASHMIRI_INDIA,              "ks", "IN" },
272     { LANGUAGE_MALAYALAM,                   "ml", "IN" },
273     { LANGUAGE_MANIPURI,                   "mni", "IN" },
274     { LANGUAGE_MARATHI,                     "mr", "IN" },
275     { LANGUAGE_KONKANI,                    "kok", "IN" },
276     { LANGUAGE_NEPALI,                      "ne", "NP" },
277     { LANGUAGE_NEPALI_INDIA,                "ne", "IN" },
278     { LANGUAGE_ORIYA,                       "or", "IN" },
279     { LANGUAGE_PUNJABI,                     "pa", "IN" },
280     { LANGUAGE_SANSKRIT,                    "sa", "IN" },
281     { LANGUAGE_SINDHI,                      "sd", "IN" },
282     { LANGUAGE_TAMIL,                       "ta", "IN" },
283     { LANGUAGE_TELUGU,                      "te", "IN" },
284     { LANGUAGE_PUNJABI_PAKISTAN,           "lah", "PK" },   // preferring "lah" over "pa" for Western Punjabi, see http://www.ethnologue.com/show_language.asp?code=PNB
285     { LANGUAGE_PUNJABI_PAKISTAN,            "pa", "PK" },
286     { LANGUAGE_SINDHI_PAKISTAN,             "sd", "PK" },
287     { LANGUAGE_BELARUSIAN,                  "be", "BY" },
288     { LANGUAGE_CATALAN,                     "ca", "ES" },   // Spain (default)
289     { LANGUAGE_CATALAN,                     "ca", "AD" },   // Andorra
290     { LANGUAGE_USER_CATALAN_VALENCIAN,                     "ca", "XV" },   // XV: ISO 3166 user-assigned; workaround for UI localization only, do not use in document content!
291     { LANGUAGE_CATALAN,                    "qcv", "ES" },   // qcv: ISO 639-3 reserved-for-local-use; UI localization quirk only, do not use in document content!
292 //    { LANGUAGE_USER_CATALAN_VALENCIAN,      "ca", "ES" },   // In case MS format files escaped into the wild, map them back.
293     { LANGUAGE_FRENCH_CAMEROON,             "fr", "CM" },
294     { LANGUAGE_FRENCH_COTE_D_IVOIRE,        "fr", "CI" },
295     { LANGUAGE_FRENCH_MALI,                 "fr", "ML" },
296     { LANGUAGE_FRENCH_SENEGAL,              "fr", "SN" },
297     { LANGUAGE_FRENCH_ZAIRE,                "fr", "CD" },   // Democratic Republic Of Congo
298     { LANGUAGE_FRENCH_MOROCCO,              "fr", "MA" },
299     { LANGUAGE_FRENCH_REUNION,              "fr", "RE" },
300     { LANGUAGE_FRENCH_NORTH_AFRICA,         "fr", ""   },
301     { LANGUAGE_FRENCH_WEST_INDIES,          "fr", ""   },   // unknown ISO country code
302     { LANGUAGE_FRISIAN_NETHERLANDS,         "fy", "NL" },
303     { LANGUAGE_GAELIC_IRELAND,              "ga", "IE" },
304     { LANGUAGE_GAELIC_SCOTLAND,             "gd", "GB" },
305     { LANGUAGE_GALICIAN,                    "gl", "ES" },
306     { LANGUAGE_GEORGIAN,                    "ka", "GE" },
307     { LANGUAGE_KHMER,                       "km", "KH" },
308     { LANGUAGE_KIRGHIZ,                     "ky", "KG" },
309     { LANGUAGE_LAO,                         "lo", "LA" },
310     { LANGUAGE_MALTESE,                     "mt", "MT" },
311     { LANGUAGE_MONGOLIAN,                   "mn", "MN" },   // Cyrillic script
312     { LANGUAGE_MONGOLIAN_MONGOLIAN,         "mn", "MN" },
313     { LANGUAGE_RUSSIAN_MOLDOVA,             "mo", "MD" },
314     { LANGUAGE_SWAHILI,                     "sw", "KE" },
315     { LANGUAGE_USER_SWAHILI_TANZANIA,       "sw", "TZ" },
316     { LANGUAGE_TAJIK,                       "tg", "TJ" },
317     { LANGUAGE_TIBETAN,                     "bo", "CN" },   // CN politically correct?
318     { LANGUAGE_DZONGKHA,                    "dz", "BT" },
319     { LANGUAGE_TURKMEN,                     "tk", "TM" },
320     { LANGUAGE_WELSH,                       "cy", "GB" },
321     { LANGUAGE_SESOTHO,                     "st", "ZA" },
322     { LANGUAGE_SEPEDI,                     "nso", "ZA" },
323     { LANGUAGE_SEPEDI,                      "ns", "ZA" },   // fake "ns" for compatibility with existing OOo1.1.x localization to be able to read those documents
324     { LANGUAGE_TSONGA,                      "ts", "ZA" },
325     { LANGUAGE_TSWANA,                      "tn", "ZA" },
326     { LANGUAGE_ENGLISH_SAFRICA,             "en", "ZA" },
327     { LANGUAGE_AFRIKAANS,                   "af", "ZA" },
328     { LANGUAGE_VENDA,                       "ve", "ZA" },   // default 639-1
329     { LANGUAGE_VENDA,                      "ven", "ZA" },   // 639-2 may have been used temporarily since 2004-07-23
330     { LANGUAGE_XHOSA,                       "xh", "ZA" },
331     { LANGUAGE_ZULU,                        "zu", "ZA" },
332     { LANGUAGE_QUECHUA_ECUADOR,             "qu", "EC" },
333     { LANGUAGE_QUECHUA_PERU,                "qu", "PE" },
334     { LANGUAGE_QUECHUA_BOLIVIA,             "qu", "BO" },   // macro: quh-BO, qul-BO
335     { LANGUAGE_PASHTO,                      "ps", "AF" },
336     { LANGUAGE_OROMO,                       "om", "ET" },
337     { LANGUAGE_DHIVEHI,                     "dv", "MV" },
338     { LANGUAGE_UIGHUR_CHINA,                "ug", "CN" },
339     { LANGUAGE_TIGRIGNA_ETHIOPIA,           "ti", "ET" },
340     { LANGUAGE_TIGRIGNA_ERITREA,            "ti", "ER" },
341     { LANGUAGE_AMHARIC_ETHIOPIA,            "am", "ET" },
342     { LANGUAGE_GUARANI_PARAGUAY,           "gug", "PY" },
343     { LANGUAGE_HAWAIIAN_UNITED_STATES,     "haw", "US" },
344     { LANGUAGE_EDO,                        "bin", "NG" },
345     { LANGUAGE_FULFULDE_NIGERIA,            "ff", "NG" },
346     { LANGUAGE_HAUSA_NIGERIA,               "ha", "NG" },
347     { LANGUAGE_USER_HAUSA_GHANA,            "ha", "GH" },
348     { LANGUAGE_IGBO_NIGERIA,                "ig", "NG" },
349     { LANGUAGE_KANURI_NIGERIA,              "kr", "NG" },
350     { LANGUAGE_YORUBA,                      "yo", "NG" },
351     { LANGUAGE_SOMALI,                      "so", "SO" },
352     { LANGUAGE_PAPIAMENTU,                 "pap", "AN" },
353     { LANGUAGE_USER_PAPIAMENTU_ARUBA,      "pap", "AW" },
354     { LANGUAGE_ENGLISH_SINGAPORE,           "en", "SG" },
355     { LANGUAGE_USER_YIDDISH_US,             "yi", "US" },
356     { LANGUAGE_YIDDISH,                     "yi", "IL" },   // new: old was "ji"
357     { LANGUAGE_YIDDISH,                     "ji", "IL" },   // old: new is "yi"
358     { LANGUAGE_SYRIAC,                     "syr", "TR" },   // "TR" according to http://www.ethnologue.com/show_language.asp?code=SYC
359     { LANGUAGE_SINHALESE_SRI_LANKA,         "si", "LK" },
360     { LANGUAGE_CHEROKEE_UNITED_STATES,     "chr", "US" },
361     { LANGUAGE_INUKTITUT_LATIN_CANADA,      "iu", "CA" },
362 //  { LANGUAGE_INUKTITUT_SYLLABICS_CANADA,  "iu", "CA" },   // script codes not supported yet
363     { LANGUAGE_SAMI_NORTHERN_NORWAY,        "se", "NO" },
364     { LANGUAGE_SAMI_INARI,                 "smn", "FI" },
365     { LANGUAGE_SAMI_LULE_NORWAY,           "smj", "NO" },
366     { LANGUAGE_SAMI_LULE_SWEDEN,           "smj", "SE" },
367     { LANGUAGE_SAMI_NORTHERN_FINLAND,       "se", "FI" },
368     { LANGUAGE_SAMI_NORTHERN_SWEDEN,        "se", "SE" },
369     { LANGUAGE_SAMI_SKOLT,                 "sms", "FI" },
370     { LANGUAGE_SAMI_SOUTHERN_NORWAY,       "sma", "NO" },
371     { LANGUAGE_SAMI_SOUTHERN_SWEDEN,       "sma", "SE" },
372     { LANGUAGE_USER_SAMI_KILDIN_RUSSIA,    "sjd", "RU" },
373     { LANGUAGE_MAPUDUNGUN_CHILE,           "arn", "CL" },
374     { LANGUAGE_CORSICAN_FRANCE,             "co", "FR" },
375     { LANGUAGE_ALSATIAN_FRANCE,            "gsw", "FR" },   // in fact 'gsw' is Schwyzerduetsch (Swiss German), which is a dialect of Alemannic German, as is Alsatian. They aren't distinct languages and share this code.
376     { LANGUAGE_YAKUT_RUSSIA,               "sah", "RU" },
377     { LANGUAGE_MOHAWK_CANADA,              "moh", "CA" },
378     { LANGUAGE_BASHKIR_RUSSIA,              "ba", "RU" },
379     { LANGUAGE_KICHE_GUATEMALA,            "qut", "GT" },
380     { LANGUAGE_DARI_AFGHANISTAN,           "gbz", "AF" },
381     { LANGUAGE_WOLOF_SENEGAL,               "wo", "SN" },
382     { LANGUAGE_FILIPINO,                   "fil", "PH" },
383     { LANGUAGE_USER_TAGALOG,                "tl", "PH" },
384     { LANGUAGE_ENGLISH_PHILIPPINES,         "en", "PH" },
385 //  { LANGUAGE_IBIBIO_NIGERIA,             "nic", "NG" },   // ISO "nic" is only a collective language code
386     { LANGUAGE_YI,                          "ii", "CN" },
387 //  { LANGUAGE_TAMAZIGHT_LATIN,            "ber", ""   },   // ISO "ber" only collective!
388 //  { LANGUAGE_TAMAZIGHT_ARABIC,           "ber", ""   },   // ISO "ber" only collective!
389     { LANGUAGE_LATIN,                       "la", "VA" },
390     { LANGUAGE_OBSOLETE_USER_LATIN,         "la", "VA" },
391     { LANGUAGE_USER_ESPERANTO,              "eo", ""   },
392     { LANGUAGE_USER_INTERLINGUA,            "ia", ""   },
393     { LANGUAGE_MAORI_NEW_ZEALAND,           "mi", "NZ" },
394     { LANGUAGE_OBSOLETE_USER_MAORI,         "mi", "NZ" },
395     { LANGUAGE_KINYARWANDA_RWANDA,          "rw", "RW" },
396     { LANGUAGE_OBSOLETE_USER_KINYARWANDA,   "rw", "RW" },
397     { LANGUAGE_UPPER_SORBIAN_GERMANY,      "hsb", "DE" },   // MS maps this to 'wen-DE', which is nonsense. 'wen' is a collective language code, 'WEN' is a SIL code, see http://www.ethnologue.com/14/show_iso639.asp?code=wen and http://www.ethnologue.com/14/show_language.asp?code=WEN
398     { LANGUAGE_OBSOLETE_USER_UPPER_SORBIAN,"hsb", "DE" },
399     { LANGUAGE_LOWER_SORBIAN_GERMANY,      "dsb", "DE" },   // MS maps this to 'wee-DE', which is nonsense. 'WEE' is a SIL code, see http://www.ethnologue.com/14/show_language.asp?code=WEE
400     { LANGUAGE_OBSOLETE_USER_LOWER_SORBIAN,"dsb", "DE" },
401     { LANGUAGE_OCCITAN_FRANCE,              "oc", "FR" },
402     { LANGUAGE_OBSOLETE_USER_OCCITAN,       "oc", "FR" },
403     { LANGUAGE_USER_KURDISH_TURKEY,         "ku", "TR" },
404     { LANGUAGE_USER_KURDISH_SYRIA,          "ku", "SY" },
405     { LANGUAGE_USER_KURDISH_IRAQ,           "ku", "IQ" },
406     { LANGUAGE_USER_KURDISH_IRAN,           "ku", "IR" },
407     { LANGUAGE_USER_SARDINIAN,              "sc", "IT" },   // macrolanguage code
408     { LANGUAGE_USER_SARDINIAN_CAMPIDANESE, "sro", "IT" },
409     { LANGUAGE_USER_SARDINIAN_GALLURESE,   "sdn", "IT" },
410     { LANGUAGE_USER_SARDINIAN_LOGUDORESE,  "src", "IT" },
411     { LANGUAGE_USER_SARDINIAN_SASSARESE,   "sdc", "IT" },
412     { LANGUAGE_BRETON_FRANCE,               "br", "FR" },
413     { LANGUAGE_OBSOLETE_USER_BRETON,        "br", "FR" },
414     { LANGUAGE_KALAALLISUT_GREENLAND,       "kl", "GL" },
415     { LANGUAGE_OBSOLETE_USER_KALAALLISUT,   "kl", "GL" },
416     { LANGUAGE_USER_SWAZI,                  "ss", "ZA" },
417     { LANGUAGE_USER_NDEBELE_SOUTH,          "nr", "ZA" },
418     { LANGUAGE_USER_TSWANA_BOTSWANA,        "tn", "BW" },
419     { LANGUAGE_USER_MOORE,                 "mos", "BF" },
420     { LANGUAGE_USER_BAMBARA,                "bm", "ML" },
421     { LANGUAGE_USER_AKAN,                   "ak", "GH" },
422     { LANGUAGE_LUXEMBOURGISH_LUXEMBOURG,    "lb", "LU" },
423     { LANGUAGE_OBSOLETE_USER_LUXEMBOURGISH, "lb", "LU" },
424     { LANGUAGE_USER_FRIULIAN,              "fur", "IT" },
425     { LANGUAGE_USER_FIJIAN,                 "fj", "FJ" },
426     { LANGUAGE_USER_AFRIKAANS_NAMIBIA,      "af", "NA" },
427     { LANGUAGE_USER_ENGLISH_NAMIBIA,        "en", "NA" },
428     { LANGUAGE_USER_WALLOON,                "wa", "BE" },
429     { LANGUAGE_USER_COPTIC,                "cop", "EG" },
430     { LANGUAGE_USER_GASCON,                "gsc", "FR" },
431     { LANGUAGE_USER_GERMAN_BELGIUM,         "de", "BE" },
432     { LANGUAGE_USER_CHUVASH,                "cv", "RU" },
433     { LANGUAGE_USER_EWE_GHANA,              "ee", "GH" },
434     { LANGUAGE_USER_ENGLISH_GHANA,          "en", "GH" },
435     { LANGUAGE_USER_SANGO,                  "sg", "CF" },
436     { LANGUAGE_USER_GANDA,                  "lg", "UG" },
437     { LANGUAGE_USER_LINGALA_DRCONGO,        "ln", "CD" },
438     { LANGUAGE_USER_LOW_GERMAN,            "nds", "DE" },
439     { LANGUAGE_USER_HILIGAYNON,            "hil", "PH" },
440     { LANGUAGE_USER_NYANJA,                 "ny", "MW" },
441     { LANGUAGE_USER_KASHUBIAN,             "csb", "PL" },
442     { LANGUAGE_USER_SPANISH_CUBA,           "es", "CU" },
443     { LANGUAGE_USER_QUECHUA_NORTH_BOLIVIA, "qul", "BO" },
444     { LANGUAGE_USER_QUECHUA_SOUTH_BOLIVIA, "quh", "BO" },
445     { LANGUAGE_USER_BODO_INDIA,            "brx", "IN" },
446     { LANGUAGE_USER_DOGRI_INDIA,           "dgo", "IN" },
447     { LANGUAGE_USER_MAITHILI_INDIA,        "mai", "IN" },
448     { LANGUAGE_USER_SANTALI_INDIA,         "sat", "IN" },
449     { LANGUAGE_USER_TETUN,                 "tet", "ID" },
450     { LANGUAGE_USER_TETUN_TIMOR_LESTE,     "tet", "TL" },
451     { LANGUAGE_USER_TOK_PISIN,             "tpi", "PG" },
452     { LANGUAGE_USER_SHUSWAP,               "shs", "CA" },
453     { LANGUAGE_USER_ANCIENT_GREEK,         "grc", "GR" },
454     { LANGUAGE_USER_ASTURIAN,              "ast", "ES" },
455     { LANGUAGE_USER_LATGALIAN,             "ltg", "LV" },
456     { LANGUAGE_USER_MAORE,                 "swb", "YT" },
457     { LANGUAGE_USER_BUSHI,                 "buc", "YT" },
458     { LANGUAGE_USER_TAHITIAN,               "ty", "PF" },
459     { LANGUAGE_USER_MALAGASY_PLATEAU,      "plt", "MG" },
460     { LANGUAGE_USER_BAFIA,                 "ksf", "CM" },
461     { LANGUAGE_USER_GIKUYU,                 "ki", "KE" },
462     { LANGUAGE_USER_RUSYN_UKRAINE,         "rue", "UA" },
463     { LANGUAGE_USER_RUSYN_SLOVAKIA,        "rue", "SK" },
464     { LANGUAGE_USER_LIMBU,                 "lif", "NP" },
465     { LANGUAGE_USER_LOJBAN,                "jbo", ""   },
466     { LANGUAGE_USER_KABYLE,                "kab", "DZ" },
467     { LANGUAGE_USER_HAITIAN,                "ht", "HT" },
468     { LANGUAGE_FRENCH_HAITI,                "fr", "HT" },
469     { LANGUAGE_NONE,                       "zxx", ""   },   // added to ISO 639-2 on 2006-01-11: Used to declare the absence of linguistic information
470     { LANGUAGE_DONTKNOW,                    "",   ""   }    // marks end of table
471 };
472 
473 static MsLangId::IsoLangEntry aLastResortFallbackEntry =
474 { LANGUAGE_ENGLISH_US, "en", "US" };
475 
476 // -----------------------------------------------------------------------
477 
478 // In this table are the countries which should mapped to a specific
479 // english language
480 static IsoLangEngEntry const aImplIsoLangEngEntries[] =
481 {
482     { LANGUAGE_ENGLISH_UK,                  "AO" },         // Angola
483     { LANGUAGE_ENGLISH_UK,                  "BJ" },         // Benin
484     { LANGUAGE_ENGLISH_UK,                  "BW" },         // Botswana
485     { LANGUAGE_ENGLISH_UK,                  "BI" },         // Burundi
486     { LANGUAGE_ENGLISH_UK,                  "CM" },         // Cameroon
487     { LANGUAGE_ENGLISH_UK,                  "GA" },         // Gabon
488     { LANGUAGE_ENGLISH_UK,                  "GM" },         // Gambia
489     { LANGUAGE_ENGLISH_UK,                  "GH" },         // Ghana
490     { LANGUAGE_ENGLISH_UK,                  "GN" },         // Guinea
491     { LANGUAGE_ENGLISH_UK,                  "LS" },         // Lesotho
492     { LANGUAGE_ENGLISH_UK,                  "MW" },         // Malawi
493     { LANGUAGE_ENGLISH_UK,                  "MT" },         // Malta
494     { LANGUAGE_ENGLISH_UK,                  "NA" },         // Namibia
495     { LANGUAGE_ENGLISH_UK,                  "NG" },         // Nigeria
496     { LANGUAGE_ENGLISH_UK,                  "UG" },         // Uganda
497     { LANGUAGE_ENGLISH_UK,                  "ZM" },         // Zambia
498     { LANGUAGE_ENGLISH_UK,                  "ZW" },         // Zimbabwe
499     { LANGUAGE_ENGLISH_UK,                  "SZ" },         // Swaziland
500     { LANGUAGE_ENGLISH_UK,                  "NG" },         // Sierra Leone
501     { LANGUAGE_ENGLISH_UK,                  "KN" },         // Saint Kitts and Nevis
502     { LANGUAGE_ENGLISH_UK,                  "SH" },         // St. Helena
503     { LANGUAGE_ENGLISH_UK,                  "IO" },         // British Indian Oceanic Territory
504     { LANGUAGE_ENGLISH_UK,                  "FK" },         // Falkland Islands
505     { LANGUAGE_ENGLISH_UK,                  "GI" },         // Gibraltar
506     { LANGUAGE_ENGLISH_UK,                  "KI" },         // Kiribati
507     { LANGUAGE_ENGLISH_UK,                  "VG" },         // Virgin Islands
508     { LANGUAGE_ENGLISH_UK,                  "MU" },         // Mauritius
509     { LANGUAGE_ENGLISH_UK,                  "FJ" },         // Fiji
510     { LANGUAGE_ENGLISH_US,                  "KI" },         // Kiribati
511     { LANGUAGE_ENGLISH_US,                  "LR" },         // Liberia
512     { LANGUAGE_ENGLISH_US,                  "GU" },         // Guam
513     { LANGUAGE_ENGLISH_US,                  "MH" },         // Marshall Islands
514     { LANGUAGE_ENGLISH_US,                  "PW" },         // Palau
515     { LANGUAGE_ENGLISH_CARRIBEAN,           "AI" },         // Anguilla
516     { LANGUAGE_ENGLISH_CARRIBEAN,           "AG" },         // Antigua and Barbuda
517     { LANGUAGE_ENGLISH_CARRIBEAN,           "BS" },         // Bahamas
518     { LANGUAGE_ENGLISH_CARRIBEAN,           "BB" },         // Barbedos
519     { LANGUAGE_ENGLISH_CARRIBEAN,           "BM" },         // Bermuda
520     { LANGUAGE_ENGLISH_CARRIBEAN,           "KY" },         // Cayman Islands
521     { LANGUAGE_ENGLISH_CARRIBEAN,           "GD" },         // Grenada
522     { LANGUAGE_ENGLISH_CARRIBEAN,           "DM" },         // Dominica
523     { LANGUAGE_ENGLISH_CARRIBEAN,           "HT" },         // Haiti
524     { LANGUAGE_ENGLISH_CARRIBEAN,           "MS" },         // Montserrat
525     { LANGUAGE_ENGLISH_CARRIBEAN,           "FM" },         // Micronesia
526     { LANGUAGE_ENGLISH_CARRIBEAN,           "VC" },         // St. Vincent / Grenadines
527     { LANGUAGE_ENGLISH_CARRIBEAN,           "LC" },         // Saint Lucia
528     { LANGUAGE_ENGLISH_CARRIBEAN,           "TC" },         // Turks & Caicos Islands
529     { LANGUAGE_ENGLISH_CARRIBEAN,           "GY" },         // Guyana
530     { LANGUAGE_ENGLISH_CARRIBEAN,           "TT" },         // Trinidad and Tobago
531     { LANGUAGE_ENGLISH_AUS,                 "CX" },         // Christmas Islands
532     { LANGUAGE_ENGLISH_AUS,                 "CC" },         // Cocos (Keeling) Islands
533     { LANGUAGE_ENGLISH_AUS,                 "NF" },         // Norfolk Island
534     { LANGUAGE_ENGLISH_AUS,                 "PG" },         // Papua New Guinea
535     { LANGUAGE_ENGLISH_AUS,                 "SB" },         // Solomon Islands
536     { LANGUAGE_ENGLISH_AUS,                 "TV" },         // Tuvalu
537     { LANGUAGE_ENGLISH_AUS,                 "NR" },         // Nauru
538     { LANGUAGE_ENGLISH_NZ,                  "CK" },         // Cook Islands
539     { LANGUAGE_ENGLISH_NZ,                  "NU" },         // Niue
540     { LANGUAGE_ENGLISH_NZ,                  "TK" },         // Tokelau
541     { LANGUAGE_ENGLISH_NZ,                  "TO" },         // Tonga
542     { LANGUAGE_DONTKNOW,                    ""   }          // marks end of table
543 };
544 
545 // -----------------------------------------------------------------------
546 
547 static IsoLangNoneStdEntry const aImplIsoNoneStdLangEntries[] =
548 {
549     { LANGUAGE_NORWEGIAN_BOKMAL,            "no", "BOK"      }, // registered subtags for "no" in rfc1766
550     { LANGUAGE_NORWEGIAN_NYNORSK,           "no", "NYN"      }, // registered subtags for "no" in rfc1766
551     { LANGUAGE_SERBIAN_LATIN,               "sr", "latin"    },
552     { LANGUAGE_SERBIAN_CYRILLIC,            "sr", "cyrillic" },
553     { LANGUAGE_AZERI_LATIN,                 "az", "latin"    },
554     { LANGUAGE_AZERI_CYRILLIC,              "az", "cyrillic" },
555     { LANGUAGE_DONTKNOW,                    "",   ""         }  // marks end of table
556 };
557 
558 // -----------------------------------------------------------------------
559 
560 // in this table are only names to find the best language
561 static IsoLangNoneStdEntry const aImplIsoNoneStdLangEntries2[] =
562 {
563     { LANGUAGE_NORWEGIAN_BOKMAL,            "no", "bokmaal"  },
564     { LANGUAGE_NORWEGIAN_BOKMAL,            "no", "bokmal"   },
565     { LANGUAGE_NORWEGIAN_NYNORSK,           "no", "nynorsk"  },
566     { LANGUAGE_DONTKNOW,                    "",   ""         }  // marks end of table
567 };
568 
569 // -----------------------------------------------------------------------
570 
571 // in this table are only names to find the best language
572 static IsoLangOtherEntry const aImplOtherEntries[] =
573 {
574     { LANGUAGE_ENGLISH_US,                  "c"              },
575     { LANGUAGE_CHINESE,                     "chinese"        },
576     { LANGUAGE_GERMAN,                      "german"         },
577     { LANGUAGE_JAPANESE,                    "japanese"       },
578     { LANGUAGE_KOREAN,                      "korean"         },
579     { LANGUAGE_ENGLISH_US,                  "posix"          },
580     { LANGUAGE_CHINESE_TRADITIONAL,         "tchinese"       },
581     { LANGUAGE_DONTKNOW,                    NULL             }  // marks end of table
582 };
583 
584 // =======================================================================
585 
586 // static
587 void MsLangId::convertLanguageToIsoNames( LanguageType nLang,
588         rtl::OUString& rLangStr, rtl::OUString& rCountry )
589 {
590     if ( nLang == LANGUAGE_SYSTEM )
591         nLang = MsLangId::getSystemLanguage();
592 
593     // Search for LangID (in this table we find only defined ISO combinations)
594     const IsoLangEntry* pEntry = aImplIsoLangEntries;
595     do
596     {
597         if ( pEntry->mnLang == nLang )
598         {
599             rLangStr = rtl::OUString::createFromAscii( pEntry->maLangStr );
600             rCountry = rtl::OUString::createFromAscii( pEntry->maCountry );
601             return;
602         }
603         ++pEntry;
604     }
605     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
606 
607     // Search for LangID if we didn't find a specific ISO combination.
608     // All entries in this table are allowed for mime specifications,
609     // but not defined ISO combinations.
610     const IsoLangNoneStdEntry* pNoneStdEntry = aImplIsoNoneStdLangEntries;
611     do
612     {
613         if ( pNoneStdEntry->mnLang == nLang )
614         {
615             rLangStr = rtl::OUString::createFromAscii( pNoneStdEntry->maLangStr );
616             rCountry = rtl::OUString::createFromAscii( pNoneStdEntry->maCountry );
617             return;
618         }
619         ++pNoneStdEntry;
620     }
621     while ( pNoneStdEntry->mnLang != LANGUAGE_DONTKNOW );
622 
623     // not found
624     rLangStr = rtl::OUString();
625     rCountry = rtl::OUString();
626 }
627 
628 // -----------------------------------------------------------------------
629 
630 // static
631 void MsLangId::convertLanguageToIsoNames( LanguageType nLang,
632         rtl::OString& rLangStr, rtl::OString& rCountry )
633 {
634     if ( nLang == LANGUAGE_SYSTEM )
635         nLang = MsLangId::getSystemLanguage();
636 
637     // Search for LangID (in this table we find only defined ISO combinations)
638     const IsoLangEntry* pEntry = aImplIsoLangEntries;
639     do
640     {
641         if ( pEntry->mnLang == nLang )
642         {
643             rLangStr = rtl::OString( pEntry->maLangStr );
644             rCountry = rtl::OString( pEntry->maCountry );
645             return;
646         }
647         ++pEntry;
648     }
649     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
650 
651     // Search for LangID if we didn't find a specific ISO combination.
652     // All entries in this table are allowed for mime specifications,
653     // but not defined ISO combinations.
654     const IsoLangNoneStdEntry* pNoneStdEntry = aImplIsoNoneStdLangEntries;
655     do
656     {
657         if ( pNoneStdEntry->mnLang == nLang )
658         {
659             rLangStr = rtl::OString( pNoneStdEntry->maLangStr );
660             rCountry = rtl::OString( pNoneStdEntry->maCountry );
661             return;
662         }
663         ++pNoneStdEntry;
664     }
665     while ( pNoneStdEntry->mnLang != LANGUAGE_DONTKNOW );
666 
667     // not found
668     rLangStr = rtl::OString();
669     rCountry = rtl::OString();
670 }
671 
672 // -----------------------------------------------------------------------
673 
674 static const MsLangId::IsoLangEntry & lcl_lookupFallbackEntry( LanguageType nLang )
675 {
676     LanguageType nPrimary = MsLangId::getPrimaryLanguage( nLang);
677 
678     // Search for LangID and remember first lang-only.
679     const MsLangId::IsoLangEntry* pFirstPrimary = NULL;
680     const MsLangId::IsoLangEntry* pEntry = aImplIsoLangEntries;
681     do
682     {
683         if (pEntry->mnLang == nLang)
684         {
685             if (*pEntry->maCountry)
686                 return *pEntry;
687             switch (nLang)
688             {
689                 // These are known to have no country assigned.
690                 case LANGUAGE_BASQUE:
691                 case LANGUAGE_USER_ESPERANTO:
692                 case LANGUAGE_USER_INTERLINGUA:
693                 case LANGUAGE_USER_LOJBAN:
694                     return *pEntry;
695                 default:
696                     ;   // nothing
697             }
698         }
699         if (!pFirstPrimary &&
700                 MsLangId::getPrimaryLanguage( pEntry->mnLang) == nPrimary)
701             pFirstPrimary = pEntry;
702         ++pEntry;
703     }
704     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
705 
706     // Language not found at all => use default.
707     if (!pFirstPrimary)
708         return aLastResortFallbackEntry;
709 
710     // Search for first entry of primary language with any country.
711     pEntry = pFirstPrimary;
712     do
713     {
714         if (MsLangId::getPrimaryLanguage( pEntry->mnLang) == nLang)
715         {
716             if (*pEntry->maCountry)
717                 return *pEntry;
718         }
719         ++pEntry;
720     }
721     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
722 
723     return aLastResortFallbackEntry;
724 }
725 
726 // static
727 LanguageType MsLangId::lookupFallbackLanguage( LanguageType nLang )
728 {
729     return lcl_lookupFallbackEntry( nLang).mnLang;
730 }
731 
732 
733 // static
734 ::com::sun::star::lang::Locale MsLangId::lookupFallbackLocale( LanguageType nLang )
735 {
736     const MsLangId::IsoLangEntry& rEntry = lcl_lookupFallbackEntry( nLang);
737     return ::com::sun::star::lang::Locale(
738             rtl::OUString::createFromAscii( rEntry.maLangStr),
739             rtl::OUString::createFromAscii( rEntry.maCountry),
740             rtl::OUString());
741 }
742 
743 // -----------------------------------------------------------------------
744 
745 static const MsLangId::IsoLangEntry & lcl_lookupFallbackEntry(
746         const ::com::sun::star::lang::Locale & rLocale )
747 {
748     // language is lower case in table
749     rtl::OUString aLowerLang = rLocale.Language.toAsciiLowerCase();
750     // country is upper case in table
751     rtl::OUString aUpperCountry = rLocale.Country.toAsciiUpperCase();
752     sal_Int32 nCountryLen = aUpperCountry.getLength();
753 
754     // Search for locale and remember first lang-only.
755     const MsLangId::IsoLangEntry* pFirstLang = NULL;
756     const MsLangId::IsoLangEntry* pEntry = aImplIsoLangEntries;
757     do
758     {
759         if (aLowerLang.equalsAscii( pEntry->maLangStr))
760         {
761             if (*pEntry->maCountry)
762             {
763                 if (nCountryLen && aUpperCountry.equalsAscii( pEntry->maCountry))
764                     return *pEntry;
765             }
766             else
767             {
768                 switch (pEntry->mnLang)
769                 {
770                     // These are known to have no country assigned.
771                     case LANGUAGE_BASQUE:
772                     case LANGUAGE_USER_ESPERANTO:
773                     case LANGUAGE_USER_INTERLINGUA:
774                     case LANGUAGE_USER_LOJBAN:
775                         return *pEntry;
776                     default:
777                         ;   // nothing
778                 }
779             }
780             if (!pFirstLang)
781                 pFirstLang = pEntry;
782         }
783         ++pEntry;
784     }
785     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
786 
787     // Language not found at all => use default.
788     if (!pFirstLang)
789         return aLastResortFallbackEntry;
790 
791     // Search for first entry of language with any country.
792     pEntry = pFirstLang;
793     do
794     {
795         if (aLowerLang.equalsAscii( pEntry->maLangStr))
796         {
797             if (*pEntry->maCountry)
798                 return *pEntry;
799         }
800         ++pEntry;
801     }
802     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
803 
804     return aLastResortFallbackEntry;
805 }
806 
807 // static
808 LanguageType MsLangId::lookupFallbackLanguage(
809         const ::com::sun::star::lang::Locale & rLocale )
810 {
811     return lcl_lookupFallbackEntry( rLocale).mnLang;
812 }
813 
814 
815 // static
816 ::com::sun::star::lang::Locale MsLangId::lookupFallbackLocale(
817         const ::com::sun::star::lang::Locale & rLocale )
818 {
819     const MsLangId::IsoLangEntry& rEntry = lcl_lookupFallbackEntry( rLocale);
820     return ::com::sun::star::lang::Locale(
821             rtl::OUString::createFromAscii( rEntry.maLangStr),
822             rtl::OUString::createFromAscii( rEntry.maCountry),
823             rtl::OUString());
824 }
825 
826 // -----------------------------------------------------------------------
827 
828 // static
829 rtl::OUString MsLangId::convertLanguageToIsoString( LanguageType nLang,
830         sal_Unicode cSep )
831 {
832     rtl::OUString   aLangStr;
833     rtl::OUString   aCountry;
834     convertLanguageToIsoNames( nLang, aLangStr, aCountry );
835     if ( aCountry.getLength() )
836     {
837         rtl::OUStringBuffer aBuf( aLangStr);
838         aBuf.append( cSep );
839         aBuf.append( aCountry );
840         return aBuf.makeStringAndClear();
841     }
842     else
843         return aLangStr;
844 }
845 
846 // -----------------------------------------------------------------------
847 
848 // static
849 rtl::OString MsLangId::convertLanguageToIsoByteString( LanguageType nLang,
850         sal_Char cSep )
851 {
852     rtl::OString  aLangStr;
853     rtl::OString  aCountry;
854     convertLanguageToIsoNames( nLang, aLangStr, aCountry );
855     if ( aCountry.getLength() )
856     {
857         rtl::OStringBuffer aBuf( aLangStr);
858         aBuf.append( cSep );
859         aBuf.append( aCountry );
860         return aBuf.makeStringAndClear();
861     }
862     return aLangStr;
863 }
864 
865 // =======================================================================
866 
867 // static
868 LanguageType MsLangId::convertIsoNamesToLanguage( const rtl::OUString& rLang,
869         const rtl::OUString& rCountry )
870 {
871     // language is lower case in table
872     rtl::OUString aLowerLang = rLang.toAsciiLowerCase();
873     // country is upper case in table
874     rtl::OUString aUpperCountry = rCountry.toAsciiUpperCase();
875 
876     //  first look for exact match
877     const IsoLangEntry* pFirstLang = NULL;
878     const IsoLangEntry* pEntry = aImplIsoLangEntries;
879     do
880     {
881         if ( aLowerLang.equalsAscii( pEntry->maLangStr ) )
882         {
883             if ( !aUpperCountry.getLength() ||
884                  aUpperCountry.equalsAscii( pEntry->maCountry ) )
885                 return pEntry->mnLang;
886             if ( !pFirstLang )
887                 pFirstLang = pEntry;
888             else if ( !*pEntry->maCountry )
889                 pFirstLang = pEntry;
890         }
891         ++pEntry;
892     }
893     while ( pEntry->mnLang != LANGUAGE_DONTKNOW );
894 
895     // some eng countries should be mapped to a specific english language
896     if ( aLowerLang.equalsAscii( "en" ) )
897     {
898         const IsoLangEngEntry* pEngEntry = aImplIsoLangEngEntries;
899         do
900         {
901             if ( aUpperCountry.equalsAscii( pEngEntry->maCountry ) )
902                 return pEngEntry->mnLang;
903             ++pEngEntry;
904         }
905         while ( pEngEntry->mnLang != LANGUAGE_DONTKNOW );
906     }
907 
908     // test for specific languages which are not used standard ISO 3166 codes
909     const IsoLangNoneStdEntry* pNoneStdEntry = aImplIsoNoneStdLangEntries;
910     do
911     {
912         if ( aLowerLang.equalsAscii( pNoneStdEntry->maLangStr ) )
913         {
914             // The countries in this table are not all in upper case
915             if ( aUpperCountry.equalsIgnoreAsciiCaseAscii( pNoneStdEntry->maCountry ) )
916                 return pNoneStdEntry->mnLang;
917         }
918         ++pNoneStdEntry;
919     }
920     while ( pNoneStdEntry->mnLang != LANGUAGE_DONTKNOW );
921     pNoneStdEntry = aImplIsoNoneStdLangEntries2;
922     do
923     {
924         if ( aLowerLang.equalsAscii( pNoneStdEntry->maLangStr ) )
925         {
926             // The countries in this table are not all in upper case
927             if ( aUpperCountry.equalsIgnoreAsciiCaseAscii( pNoneStdEntry->maCountry ) )
928                 return pNoneStdEntry->mnLang;
929         }
930         ++pNoneStdEntry;
931     }
932     while ( pNoneStdEntry->mnLang != LANGUAGE_DONTKNOW );
933 
934     // If the language is correct, than we return the default language
935     if ( pFirstLang )
936         return pFirstLang->mnLang;
937 
938     //  if only the country is set, look for any entry matching the country
939     //  (to allow reading country and language in separate steps, in any order)
940     if ( rCountry.getLength() && !rLang.getLength() )
941     {
942         const IsoLangEntry* pEntry2 = aImplIsoLangEntries;
943         do
944         {
945             if ( aUpperCountry.equalsAscii( pEntry2->maCountry ) )
946                 return pEntry2->mnLang;
947             ++pEntry2;
948         }
949         while ( pEntry2->mnLang != LANGUAGE_DONTKNOW );
950 
951         aLowerLang = aUpperCountry.toAsciiLowerCase();
952     }
953 
954     // Now look for all other definitions, which are not standard
955     const IsoLangOtherEntry* pOtherEntry = aImplOtherEntries;
956     do
957     {
958         if ( aLowerLang.equalsAscii( pOtherEntry->mpLangStr ) )
959             return pOtherEntry->mnLang;
960         ++pOtherEntry;
961     }
962     while ( pOtherEntry->mnLang != LANGUAGE_DONTKNOW );
963 
964     return LANGUAGE_DONTKNOW;
965 }
966 
967 // -----------------------------------------------------------------------
968 
969 // static
970 LanguageType MsLangId::convertIsoNamesToLanguage( const rtl::OString& rLang,
971         const rtl::OString& rCountry )
972 {
973     rtl::OUString aLang = OStringToOUString( rLang, RTL_TEXTENCODING_ASCII_US);
974     rtl::OUString aCountry = OStringToOUString( rCountry, RTL_TEXTENCODING_ASCII_US);
975     return convertIsoNamesToLanguage( aLang, aCountry);
976 }
977 
978 // -----------------------------------------------------------------------
979 
980 // static
981 LanguageType MsLangId::convertIsoStringToLanguage(
982         const rtl::OUString& rString, sal_Unicode cSep )
983 {
984     rtl::OUString   aLang;
985     rtl::OUString   aCountry;
986     sal_Int32  nSepPos = rString.indexOf( cSep );
987     if ( nSepPos >= 0 )
988     {
989         aLang = rString.copy( 0, nSepPos );
990         aCountry = rString.copy( nSepPos+1 );
991     }
992     else
993         aLang = rString;
994 
995     return convertIsoNamesToLanguage( aLang, aCountry );
996 }
997 
998 // -----------------------------------------------------------------------
999 
1000 // static
1001 LanguageType MsLangId::convertIsoByteStringToLanguage(
1002         const rtl::OString& rString, sal_Char cSep )
1003 {
1004     rtl::OString  aLang;
1005     rtl::OString  aCountry;
1006     sal_Int32  nSepPos = rString.indexOf( cSep );
1007     if ( nSepPos >= 0 )
1008     {
1009         aLang = rString.copy( 0, nSepPos );
1010         aCountry = rString.copy( nSepPos+1 );
1011     }
1012     else
1013         aLang = rString;
1014 
1015     return convertIsoNamesToLanguage( aLang, aCountry );
1016 }
1017 
1018 // -----------------------------------------------------------------------
1019 
1020 struct IsoLangGLIBCModifiersEntry
1021 {
1022     LanguageType  mnLang;
1023     sal_Char      maLangStr[4];
1024     sal_Char      maCountry[3];
1025     sal_Char      maAtString[9];
1026 };
1027 
1028 static IsoLangGLIBCModifiersEntry const aImplIsoLangGLIBCModifiersEntries[] =
1029 {
1030     // MS-LANGID codes               ISO639-1/2/3 ISO3166            glibc modifier
1031     { LANGUAGE_BOSNIAN_CYRILLIC_BOSNIA_HERZEGOVINA, "bs", "BA", "cyrillic" },
1032     { LANGUAGE_USER_SERBIAN_LATIN_SERBIA,           "sr", "RS", "latin" },   // Serbian Latin in Serbia
1033     { LANGUAGE_SERBIAN_LATIN,                       "sr", "CS", "latin" },   // Serbian Latin in Serbia and Montenegro
1034     { LANGUAGE_USER_SERBIAN_LATIN_MONTENEGRO,       "sr", "ME", "latin" },   // Serbian Latin in Montenegro
1035     { LANGUAGE_SERBIAN_LATIN_NEUTRAL,               "sr", "",   "latin" },
1036     { LANGUAGE_AZERI_CYRILLIC,                      "az", "AZ", "cyrillic" },
1037     { LANGUAGE_UZBEK_CYRILLIC,                      "uz", "UZ", "cyrillic" },
1038     { LANGUAGE_DONTKNOW,                            "",   "",   ""   }       // marks end of table
1039 };
1040 
1041 // convert a unix locale string into LanguageType
1042 
1043 // static
1044 LanguageType MsLangId::convertUnxByteStringToLanguage(
1045         const rtl::OString& rString )
1046 {
1047     rtl::OString  aLang;
1048     rtl::OString  aCountry;
1049     rtl::OString  aAtString;
1050 
1051     sal_Int32  nLangSepPos    = rString.indexOf( (sal_Char)'_' );
1052     sal_Int32  nCountrySepPos = rString.indexOf( (sal_Char)'.' );
1053     sal_Int32  nAtPos         = rString.indexOf( (sal_Char)'@' );
1054 
1055     if (nCountrySepPos < 0)
1056         nCountrySepPos = nAtPos;
1057     if (nCountrySepPos < 0)
1058         nCountrySepPos = rString.getLength();
1059 
1060     if (nAtPos >= 0)
1061         aAtString = rString.copy( nAtPos+1 );
1062 
1063     if (   ((nLangSepPos >= 0) && (nLangSepPos > nCountrySepPos))
1064         || ((nLangSepPos < 0)) )
1065     {
1066         // eg. "el.sun_eu_greek", "tchinese", "es.ISO8859-15"
1067         aLang    = rString.copy( 0, nCountrySepPos );
1068     }
1069     else if ( nLangSepPos >= 0 )
1070     {
1071         // well formed iso names like "en_US.UTF-8", "sh_BA.ISO8859-2@bosnia"
1072         aLang    = rString.copy( 0, nLangSepPos );
1073         aCountry = rString.copy( nLangSepPos+1, nCountrySepPos - nLangSepPos - 1);
1074     }
1075 
1076     //  if there is a glibc modifier, first look for exact match in modifier table
1077     if (aAtString.getLength())
1078     {
1079         // language is lower case in table
1080         rtl::OString aLowerLang = aLang.toAsciiLowerCase();
1081         // country is upper case in table
1082         rtl::OString aUpperCountry = aCountry.toAsciiUpperCase();
1083         const IsoLangGLIBCModifiersEntry* pGLIBCModifiersEntry = aImplIsoLangGLIBCModifiersEntries;
1084         do
1085         {
1086             if (( aLowerLang.equals( pGLIBCModifiersEntry->maLangStr ) ) &&
1087                ( aAtString.equals( pGLIBCModifiersEntry->maAtString ) ))
1088             {
1089                 if ( !aUpperCountry.getLength() ||
1090                      aUpperCountry.equals( pGLIBCModifiersEntry->maCountry ) )
1091                {
1092                     return pGLIBCModifiersEntry->mnLang;
1093                }
1094             }
1095             ++pGLIBCModifiersEntry;
1096         }
1097         while ( pGLIBCModifiersEntry->mnLang != LANGUAGE_DONTKNOW );
1098     }
1099 
1100     return convertIsoNamesToLanguage( aLang, aCountry );
1101 }
1102 
1103 // -----------------------------------------------------------------------
1104 // pass one IsoLangEntry to the outer world of the resource compiler
1105 
1106 // static
1107 const MsLangId::IsoLangEntry* MsLangId::getIsoLangEntry( size_t nIndex )
1108 {
1109     if (nIndex < sizeof( aImplIsoLangEntries) / sizeof( IsoLangEntry))
1110         return &aImplIsoLangEntries[ nIndex];
1111     return 0;
1112 }
1113