merge the formfield patch from ooo-build
[ooovba.git] / svx / source / msfilter / countryid.cxx
bloba6fcd73d0a221a6169fdfb99f08550448d67666f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: countryid.cxx,v $
10 * $Revision: 1.8.242.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 // ============================================================================
35 #include "countryid.hxx"
37 #include <algorithm>
39 // ----------------------------------------------------------------------------
41 namespace svx {
43 // Mapping table ==============================================================
45 namespace {
47 // ----------------------------------------------------------------------------
49 /** Table entry for Windows country ID <-> language type conversion.
51 The first member is the Windows country ID, as defined in the header.
53 The second member contains the corresponding language type for each country
54 ID. This must be a full language, not only the primary language type.
56 The last bool flag defines, if the sub language type should be evaluated to
57 find the country ID from a language. If not set, all languages map to the
58 country which contain the given primary language type.
60 Example: The language entry (COUNTRY_USA,LANGUAGE_ENGLISH_US,false) maps
61 the country ID for USA to the language LANGUAGE_ENGLISH_US. The clear sub
62 language flag causes all english languages LANGUAGE_ENGLISH_*** to map to
63 this country ID by default. To map the special case LANGUAGE_ENGLISH_EIRE
64 to the country ID COUNTRY_IRELAND, the sub language flag must be set in the
65 respective table entry, here (COUNTRY_IRELAND,LANGUAGE_ENGLISH_EIRE,true).
67 struct CountryEntry
69 CountryId meCountry; /// Windows country ID.
70 LanguageType meLanguage; /// Corresponding language type.
71 bool mbUseSubLang; /// false = Primary only, true = Primary and sub language.
74 // ----------------------------------------------------------------------------
76 /** Table for Windows country ID <-> language type conversion.
78 To map the same language to different country IDs, some of the entries
79 should contain a set sub language flag (see description of CountryEntry).
80 All table entries with a set flag take priority over the entry with the
81 same primary language, but cleared sub language flag, regardless of the
82 position in the table.
84 To map different languages to the same country ID, several entries with the
85 same country ID may be inserted. In this case the conversion to a language
86 is done with the first found entry (starting from top) containing the given
87 country ID.
89 For now all entries are sorted by country ID, but this is not required.
91 static const CountryEntry pTable[] =
93 { COUNTRY_USA, LANGUAGE_ENGLISH_US, false },
94 { COUNTRY_DOMINICAN_REPUBLIC, LANGUAGE_SPANISH_DOMINICAN_REPUBLIC, true },
95 { COUNTRY_JAMAICA, LANGUAGE_ENGLISH_JAMAICA, true },
96 { COUNTRY_PUERTO_RICO, LANGUAGE_SPANISH_PUERTO_RICO, true },
97 { COUNTRY_TRINIDAD_Y_TOBAGO, LANGUAGE_ENGLISH_TRINIDAD, true },
98 { COUNTRY_CANADA, LANGUAGE_ENGLISH_CAN, true },
99 { COUNTRY_CANADA, LANGUAGE_FRENCH_CANADIAN, true },
100 { COUNTRY_RUSSIA, LANGUAGE_RUSSIAN, false },
101 { COUNTRY_KAZAKHSTAN, LANGUAGE_KAZAK, false },
102 { COUNTRY_TATARSTAN, LANGUAGE_TATAR, false },
103 { COUNTRY_EGYPT, LANGUAGE_ARABIC_EGYPT, true },
104 { COUNTRY_SOUTH_AFRICA, LANGUAGE_AFRIKAANS, false },
105 { COUNTRY_SOUTH_AFRICA, LANGUAGE_ENGLISH_SAFRICA, true },
106 { COUNTRY_SOUTH_AFRICA, LANGUAGE_TSONGA, false },
107 { COUNTRY_SOUTH_AFRICA, LANGUAGE_VENDA, false },
108 { COUNTRY_SOUTH_AFRICA, LANGUAGE_XHOSA, false },
109 { COUNTRY_SOUTH_AFRICA, LANGUAGE_ZULU, false },
110 { COUNTRY_GREECE, LANGUAGE_GREEK, false },
111 { COUNTRY_NETHERLANDS, LANGUAGE_DUTCH, false },
112 { COUNTRY_NETHERLANDS, LANGUAGE_FRISIAN_NETHERLANDS, false },
113 { COUNTRY_BELGIUM, LANGUAGE_DUTCH_BELGIAN, true },
114 { COUNTRY_BELGIUM, LANGUAGE_FRENCH_BELGIAN, true },
115 { COUNTRY_FRANCE, LANGUAGE_FRENCH, false },
116 { COUNTRY_SPAIN, LANGUAGE_SPANISH_MODERN, false },
117 { COUNTRY_SPAIN, LANGUAGE_SPANISH_DATED, false },
118 { COUNTRY_SPAIN, LANGUAGE_CATALAN, false },
119 { COUNTRY_SPAIN, LANGUAGE_BASQUE, false },
120 { COUNTRY_SPAIN, LANGUAGE_GALICIAN, false },
121 { COUNTRY_HUNGARY, LANGUAGE_HUNGARIAN, false },
122 { COUNTRY_ITALY, LANGUAGE_ITALIAN, false },
123 { COUNTRY_ROMANIA, LANGUAGE_ROMANIAN, false },
124 { COUNTRY_SWITZERLAND, LANGUAGE_GERMAN_SWISS, true },
125 { COUNTRY_SWITZERLAND, LANGUAGE_FRENCH_SWISS, true },
126 { COUNTRY_SWITZERLAND, LANGUAGE_ITALIAN_SWISS, true },
127 { COUNTRY_SWITZERLAND, LANGUAGE_RHAETO_ROMAN, false },
128 { COUNTRY_AUSTRIA, LANGUAGE_GERMAN_AUSTRIAN, true },
129 { COUNTRY_UNITED_KINGDOM, LANGUAGE_ENGLISH_UK, true },
130 { COUNTRY_UNITED_KINGDOM, LANGUAGE_GAELIC_SCOTLAND, true },
131 { COUNTRY_UNITED_KINGDOM, LANGUAGE_WELSH, false },
132 { COUNTRY_DENMARK, LANGUAGE_DANISH, false },
133 { COUNTRY_SWEDEN, LANGUAGE_SWEDISH, false },
134 { COUNTRY_SWEDEN, LANGUAGE_SAMI_LAPPISH, false },
135 { COUNTRY_NORWAY, LANGUAGE_NORWEGIAN_BOKMAL, false },
136 { COUNTRY_POLAND, LANGUAGE_POLISH, false },
137 { COUNTRY_GERMANY, LANGUAGE_GERMAN, false },
138 { COUNTRY_GERMANY, LANGUAGE_SORBIAN, false },
139 { COUNTRY_PERU, LANGUAGE_SPANISH_PERU, true },
140 { COUNTRY_MEXICO, LANGUAGE_SPANISH_MEXICAN, true },
141 { COUNTRY_ARGENTINIA, LANGUAGE_SPANISH_ARGENTINA, true },
142 { COUNTRY_BRAZIL, LANGUAGE_PORTUGUESE_BRAZILIAN, true },
143 { COUNTRY_CHILE, LANGUAGE_SPANISH_CHILE, true },
144 { COUNTRY_COLOMBIA, LANGUAGE_SPANISH_COLOMBIA, true },
145 { COUNTRY_VENEZUELA, LANGUAGE_SPANISH_VENEZUELA, true },
146 { COUNTRY_MALAYSIA, LANGUAGE_MALAY_MALAYSIA, false },
147 { COUNTRY_AUSTRALIA, LANGUAGE_ENGLISH_AUS, true },
148 { COUNTRY_INDONESIA, LANGUAGE_INDONESIAN, false },
149 { COUNTRY_PHILIPPINES, LANGUAGE_ENGLISH_PHILIPPINES, true },
150 { COUNTRY_NEW_ZEALAND, LANGUAGE_MAORI_NEW_ZEALAND, false },
151 { COUNTRY_NEW_ZEALAND, LANGUAGE_ENGLISH_NZ, true },
152 { COUNTRY_SINGAPORE, LANGUAGE_CHINESE_SINGAPORE, true },
153 { COUNTRY_THAILAND, LANGUAGE_THAI, false },
154 { COUNTRY_JAPAN, LANGUAGE_JAPANESE, false },
155 { COUNTRY_SOUTH_KOREA, LANGUAGE_KOREAN, false },
156 { COUNTRY_VIET_NAM, LANGUAGE_VIETNAMESE, false },
157 { COUNTRY_PR_CHINA, LANGUAGE_CHINESE_SIMPLIFIED, false },
158 { COUNTRY_TIBET, LANGUAGE_TIBETAN, false },
159 { COUNTRY_TURKEY, LANGUAGE_TURKISH, false },
160 { COUNTRY_INDIA, LANGUAGE_HINDI, false },
161 { COUNTRY_INDIA, LANGUAGE_URDU_INDIA, true },
162 { COUNTRY_INDIA, LANGUAGE_PUNJABI, false },
163 { COUNTRY_INDIA, LANGUAGE_GUJARATI, false },
164 { COUNTRY_INDIA, LANGUAGE_ORIYA, false },
165 { COUNTRY_INDIA, LANGUAGE_TAMIL, false },
166 { COUNTRY_INDIA, LANGUAGE_TELUGU, false },
167 { COUNTRY_INDIA, LANGUAGE_KANNADA, false },
168 { COUNTRY_INDIA, LANGUAGE_MALAYALAM, false },
169 { COUNTRY_INDIA, LANGUAGE_ASSAMESE, false },
170 { COUNTRY_INDIA, LANGUAGE_MARATHI, false },
171 { COUNTRY_INDIA, LANGUAGE_SANSKRIT, false },
172 { COUNTRY_INDIA, LANGUAGE_KONKANI, false },
173 { COUNTRY_INDIA, LANGUAGE_MANIPURI, false },
174 { COUNTRY_INDIA, LANGUAGE_SINDHI, false },
175 { COUNTRY_INDIA, LANGUAGE_KASHMIRI, false },
176 { COUNTRY_PAKISTAN, LANGUAGE_URDU_PAKISTAN, false },
177 { COUNTRY_MYANMAR, LANGUAGE_BURMESE, false },
178 { COUNTRY_MOROCCO, LANGUAGE_ARABIC_MOROCCO, true },
179 { COUNTRY_ALGERIA, LANGUAGE_ARABIC_ALGERIA, true },
180 { COUNTRY_TUNISIA, LANGUAGE_ARABIC_TUNISIA, true },
181 { COUNTRY_LIBYA, LANGUAGE_ARABIC_LIBYA, true },
182 { COUNTRY_SENEGAL, LANGUAGE_FRENCH_SENEGAL, true },
183 { COUNTRY_MALI, LANGUAGE_FRENCH_MALI, true },
184 { COUNTRY_COTE_D_IVOIRE, LANGUAGE_FRENCH_COTE_D_IVOIRE, true },
185 { COUNTRY_CAMEROON, LANGUAGE_FRENCH_CAMEROON, true },
186 { COUNTRY_ZAIRE, LANGUAGE_FRENCH_ZAIRE, true },
187 { COUNTRY_RWANDA, LANGUAGE_KINYARWANDA_RWANDA, false },
188 { COUNTRY_KENYA, LANGUAGE_SWAHILI, false },
189 { COUNTRY_REUNION, LANGUAGE_FRENCH_REUNION, true },
190 { COUNTRY_ZIMBABWE, LANGUAGE_ENGLISH_ZIMBABWE, true },
191 { COUNTRY_LESOTHO, LANGUAGE_SESOTHO, false },
192 { COUNTRY_BOTSWANA, LANGUAGE_TSWANA, false },
193 { COUNTRY_FAEROE_ISLANDS, LANGUAGE_FAEROESE, false },
194 { COUNTRY_PORTUGAL, LANGUAGE_PORTUGUESE, false },
195 { COUNTRY_LUXEMBOURG, LANGUAGE_GERMAN_LUXEMBOURG, true },
196 { COUNTRY_LUXEMBOURG, LANGUAGE_FRENCH_LUXEMBOURG, true },
197 { COUNTRY_IRELAND, LANGUAGE_ENGLISH_EIRE, true },
198 { COUNTRY_IRELAND, LANGUAGE_GAELIC_IRELAND, true },
199 { COUNTRY_ICELAND, LANGUAGE_ICELANDIC, false },
200 { COUNTRY_ALBANIA, LANGUAGE_ALBANIAN, false },
201 { COUNTRY_MALTA, LANGUAGE_MALTESE, false },
202 { COUNTRY_FINLAND, LANGUAGE_FINNISH, false },
203 { COUNTRY_FINLAND, LANGUAGE_SWEDISH_FINLAND, true },
204 { COUNTRY_BULGARIA, LANGUAGE_BULGARIAN, false },
205 { COUNTRY_LITHUANIA, LANGUAGE_LITHUANIAN, false },
206 { COUNTRY_LATVIA, LANGUAGE_LATVIAN, false },
207 { COUNTRY_ESTONIA, LANGUAGE_ESTONIAN, false },
208 { COUNTRY_MOLDOVA, LANGUAGE_ROMANIAN_MOLDOVA, true },
209 { COUNTRY_MOLDOVA, LANGUAGE_RUSSIAN_MOLDOVA, true },
210 { COUNTRY_ARMENIA, LANGUAGE_ARMENIAN, false },
211 { COUNTRY_BELARUS, LANGUAGE_BELARUSIAN, false },
212 { COUNTRY_MONACO, LANGUAGE_FRENCH_MONACO, true },
213 { COUNTRY_UKRAINE, LANGUAGE_UKRAINIAN, false },
214 { COUNTRY_SERBIA, LANGUAGE_SERBIAN_LATIN, false },
215 { COUNTRY_CROATIA, LANGUAGE_CROATIAN, true }, // sub type of LANGUAGE_SERBIAN
216 { COUNTRY_SLOVENIA, LANGUAGE_SLOVENIAN, false },
217 { COUNTRY_MACEDONIA, LANGUAGE_MACEDONIAN, false },
218 { COUNTRY_CZECH, LANGUAGE_CZECH, false },
219 { COUNTRY_SLOVAK, LANGUAGE_SLOVAK, false },
220 { COUNTRY_LIECHTENSTEIN, LANGUAGE_GERMAN_LIECHTENSTEIN, true },
221 { COUNTRY_BELIZE, LANGUAGE_ENGLISH_BELIZE, true },
222 { COUNTRY_GUATEMALA, LANGUAGE_SPANISH_GUATEMALA, true },
223 { COUNTRY_EL_SALVADOR, LANGUAGE_SPANISH_EL_SALVADOR, true },
224 { COUNTRY_HONDURAS, LANGUAGE_SPANISH_HONDURAS, true },
225 { COUNTRY_NICARAGUA, LANGUAGE_SPANISH_NICARAGUA, true },
226 { COUNTRY_COSTA_RICA, LANGUAGE_SPANISH_COSTARICA, true },
227 { COUNTRY_PANAMA, LANGUAGE_SPANISH_PANAMA, true },
228 { COUNTRY_BOLIVIA, LANGUAGE_SPANISH_BOLIVIA, true },
229 { COUNTRY_ECUADOR, LANGUAGE_SPANISH_ECUADOR, true },
230 { COUNTRY_PARAGUAY, LANGUAGE_SPANISH_PARAGUAY, true },
231 { COUNTRY_URUGUAY, LANGUAGE_SPANISH_URUGUAY, true },
232 { COUNTRY_BRUNEI_DARUSSALAM, LANGUAGE_MALAY_BRUNEI_DARUSSALAM, true },
233 { COUNTRY_HONG_KONG, LANGUAGE_CHINESE_HONGKONG, true },
234 { COUNTRY_MACAU, LANGUAGE_CHINESE_MACAU, true },
235 { COUNTRY_CAMBODIA, LANGUAGE_KHMER, false },
236 { COUNTRY_LAOS, LANGUAGE_LAO, false },
237 { COUNTRY_BANGLADESH, LANGUAGE_BENGALI, false },
238 { COUNTRY_TAIWAN, LANGUAGE_CHINESE_TRADITIONAL, true },
239 { COUNTRY_MALDIVES, LANGUAGE_DHIVEHI, false },
240 { COUNTRY_LEBANON, LANGUAGE_ARABIC_LEBANON, true },
241 { COUNTRY_JORDAN, LANGUAGE_ARABIC_JORDAN, true },
242 { COUNTRY_SYRIA, LANGUAGE_ARABIC_SYRIA, true },
243 { COUNTRY_IRAQ, LANGUAGE_ARABIC_IRAQ, true },
244 { COUNTRY_KUWAIT, LANGUAGE_ARABIC_KUWAIT, true },
245 { COUNTRY_SAUDI_ARABIA, LANGUAGE_ARABIC_SAUDI_ARABIA, true },
246 { COUNTRY_YEMEN, LANGUAGE_ARABIC_YEMEN, true },
247 { COUNTRY_OMAN, LANGUAGE_ARABIC_OMAN, true },
248 { COUNTRY_UAE, LANGUAGE_ARABIC_UAE, true },
249 { COUNTRY_ISRAEL, LANGUAGE_HEBREW, false },
250 { COUNTRY_BAHRAIN, LANGUAGE_ARABIC_BAHRAIN, true },
251 { COUNTRY_QATAR, LANGUAGE_ARABIC_QATAR, true },
252 { COUNTRY_MONGOLIA, LANGUAGE_MONGOLIAN, false },
253 { COUNTRY_NEPAL, LANGUAGE_NEPALI, false },
254 { COUNTRY_IRAN, LANGUAGE_FARSI, false },
255 { COUNTRY_TAJIKISTAN, LANGUAGE_TAJIK, false },
256 { COUNTRY_TURKMENISTAN, LANGUAGE_TURKMEN, false },
257 { COUNTRY_AZERBAIJAN, LANGUAGE_AZERI_LATIN, false },
258 { COUNTRY_GEORGIA, LANGUAGE_GEORGIAN, false },
259 { COUNTRY_KYRGYZSTAN, LANGUAGE_KIRGHIZ, false },
260 { COUNTRY_UZBEKISTAN, LANGUAGE_UZBEK_LATIN, false }
263 const CountryEntry * const pEnd = pTable + sizeof( pTable ) / sizeof( pTable[ 0 ] );
265 // ----------------------------------------------------------------------------
267 /** Predicate comparing a country ID with the member of a CountryEntry. */
268 struct CountryEntryPred_Country
270 CountryId meCountry;
272 inline explicit CountryEntryPred_Country( CountryId eCountry ) :
273 meCountry( eCountry ) {}
275 inline bool operator()( const CountryEntry& rCmp ) const
276 { return rCmp.meCountry == meCountry; }
279 // ----------------------------------------------------------------------------
281 /** Predicate comparing a language type with the member of a CountryEntry.
283 Compares by primary language only, if the passed CountryEntry allows it
284 (the member mbUseSubLang is cleared), otherwise by full language type. */
285 struct CountryEntryPred_Language
287 LanguageType meLanguage;
289 inline explicit CountryEntryPred_Language( LanguageType eLanguage ) :
290 meLanguage( eLanguage ) {}
292 inline bool operator()( const CountryEntry& rCmp ) const;
295 inline bool CountryEntryPred_Language::operator()( const CountryEntry& rCmp ) const
297 // rCmp.mbUseSubLang==true -> compare full language type
298 // rCmp.mbUseSubLang==false -> compare primary language only
299 return rCmp.mbUseSubLang ? (meLanguage == rCmp.meLanguage) :
300 ((meLanguage & 0x03FF) == (rCmp.meLanguage & 0x03FF));
303 // ----------------------------------------------------------------------------
305 } // namespace
307 // Country ID <-> Language type conversion ====================================
309 CountryId ConvertLanguageToCountry( LanguageType eLanguage )
311 // country of a found primary language type
312 CountryId ePrimCountry = COUNTRY_DONTKNOW;
314 // find an exact match and a primary-language-only match, in one pass
315 const CountryEntry* pEntry = pTable;
318 pEntry = std::find_if( pEntry, pEnd, CountryEntryPred_Language( eLanguage ) );
319 if( pEntry != pEnd )
321 if( pEntry->mbUseSubLang )
322 return pEntry->meCountry; // exact match found -> return
323 if( ePrimCountry == COUNTRY_DONTKNOW )
324 ePrimCountry = pEntry->meCountry;
325 ++pEntry; // one entry forward for next find_if() call
328 while( pEntry != pEnd );
330 return ePrimCountry;
333 LanguageType ConvertCountryToLanguage( CountryId eCountry )
335 // just find the first occurance of eCountry and return the language type
336 const CountryEntry* pEntry = std::find_if( pTable, pEnd, CountryEntryPred_Country( eCountry ) );
337 return (pEntry != pEnd) ? pEntry->meLanguage : LANGUAGE_DONTKNOW;
340 // ============================================================================
342 } // namespace svx
344 // ============================================================================