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