merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / isolang / mslangid.cxx
blob5fe767d0baf607aad57d836b45d888133a2c5283
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: mslangid.cxx,v $
10 * $Revision: 1.10.24.4 $
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_i18npool.hxx"
33 #include <sal/config.h>
34 #include <rtl/ustring.hxx>
35 #include <rtl/string.hxx>
36 #include <com/sun/star/i18n/ScriptType.hpp>
38 #include "i18npool/mslangid.hxx"
41 LanguageType MsLangId::nConfiguredSystemLanguage = LANGUAGE_SYSTEM;
42 LanguageType MsLangId::nConfiguredSystemUILanguage = LANGUAGE_SYSTEM;
44 LanguageType MsLangId::nConfiguredWesternFallback = LANGUAGE_SYSTEM;
45 LanguageType MsLangId::nConfiguredAsianFallback = LANGUAGE_SYSTEM;
46 LanguageType MsLangId::nConfiguredComplexFallback = LANGUAGE_SYSTEM;
48 // static
49 void MsLangId::setConfiguredSystemLanguage( LanguageType nLang )
51 nConfiguredSystemLanguage = nLang;
55 // static
56 void MsLangId::setConfiguredSystemUILanguage( LanguageType nLang )
58 nConfiguredSystemUILanguage = nLang;
61 // static
62 void MsLangId::setConfiguredWesternFallback( LanguageType nLang )
64 nConfiguredWesternFallback = nLang;
67 // static
68 void MsLangId::setConfiguredAsianFallback( LanguageType nLang )
70 nConfiguredAsianFallback = nLang;
73 // static
74 void MsLangId::setConfiguredComplexFallback( LanguageType nLang )
76 nConfiguredComplexFallback = nLang;
79 // static
80 inline LanguageType MsLangId::simplifySystemLanguages( LanguageType nLang )
82 switch (nLang)
84 case LANGUAGE_PROCESS_OR_USER_DEFAULT :
85 case LANGUAGE_SYSTEM_DEFAULT :
86 case LANGUAGE_SYSTEM :
87 nLang = LANGUAGE_SYSTEM;
88 break;
89 default:
90 ; // nothing
92 return nLang;
96 // static
97 LanguageType MsLangId::getRealLanguageWithoutConfig( LanguageType nLang )
99 switch (simplifySystemLanguages( nLang))
101 case LANGUAGE_SYSTEM :
102 nLang = getSystemLanguage();
103 break;
104 case LANGUAGE_NONE :
105 nLang = getSystemUILanguage();
106 break;
107 default:
108 ; // nothing
110 if (nLang == LANGUAGE_DONTKNOW)
111 nLang = LANGUAGE_ENGLISH_US;
112 return nLang;
116 // static
117 LanguageType MsLangId::getRealLanguage( LanguageType nLang )
119 switch (simplifySystemLanguages( nLang))
121 case LANGUAGE_SYSTEM :
122 if (nConfiguredSystemLanguage == LANGUAGE_SYSTEM)
123 nLang = getSystemLanguage();
124 else
125 nLang = nConfiguredSystemLanguage;
126 break;
127 case LANGUAGE_NONE :
128 if (nConfiguredSystemUILanguage == LANGUAGE_SYSTEM)
129 nLang = getSystemUILanguage();
130 else
131 nLang = nConfiguredSystemUILanguage;
132 break;
133 default:
134 ; // nothing
136 if (nLang == LANGUAGE_DONTKNOW)
137 nLang = LANGUAGE_ENGLISH_US;
138 return nLang;
142 // static
143 LanguageType MsLangId::resolveSystemLanguageByScriptType( LanguageType nLang, sal_Int16 nType )
145 if (nLang == LANGUAGE_NONE)
146 return nLang;
148 nLang = getRealLanguage(nLang);
149 if (nType != ::com::sun::star::i18n::ScriptType::WEAK && getScriptType(nLang) != nType)
151 switch(nType)
153 case ::com::sun::star::i18n::ScriptType::ASIAN:
154 if (nConfiguredAsianFallback == LANGUAGE_SYSTEM)
155 nLang = LANGUAGE_CHINESE_SIMPLIFIED;
156 else
157 nLang = nConfiguredComplexFallback;
158 break;
159 case ::com::sun::star::i18n::ScriptType::COMPLEX:
160 if (nConfiguredComplexFallback == LANGUAGE_SYSTEM)
161 nLang = LANGUAGE_HINDI;
162 else
163 nLang = nConfiguredComplexFallback;
164 break;
165 default:
166 if (nConfiguredWesternFallback == LANGUAGE_SYSTEM)
167 nLang = LANGUAGE_ENGLISH_US;
168 else
169 nLang = nConfiguredWesternFallback;
170 break;
173 return nLang;
176 // static
177 void MsLangId::convertLanguageToLocale( LanguageType nLang,
178 ::com::sun::star::lang::Locale & rLocale )
180 if (rLocale.Variant.getLength())
181 rLocale.Variant = rtl::OUString();
182 convertLanguageToIsoNames( nLang, rLocale.Language, rLocale.Country);
186 // static
187 ::com::sun::star::lang::Locale MsLangId::convertLanguageToLocale(
188 LanguageType nLang, bool bResolveSystem )
190 ::com::sun::star::lang::Locale aLocale;
191 if (!bResolveSystem && simplifySystemLanguages( nLang) == LANGUAGE_SYSTEM)
192 ; // nothing => empty locale
193 else
195 // Still resolve LANGUAGE_DONTKNOW if resolving is not requested,
196 // but not LANGUAGE_NONE or others.
197 if (bResolveSystem || nLang == LANGUAGE_DONTKNOW)
198 nLang = MsLangId::getRealLanguage( nLang);
199 convertLanguageToLocale( nLang, aLocale);
201 return aLocale;
205 // static
206 LanguageType MsLangId::convertLocaleToLanguage(
207 const ::com::sun::star::lang::Locale& rLocale )
209 // empty language => LANGUAGE_SYSTEM
210 if (rLocale.Language.getLength() == 0)
211 return LANGUAGE_SYSTEM;
213 LanguageType nRet = convertIsoNamesToLanguage( rLocale.Language,
214 rLocale.Country);
215 if (nRet == LANGUAGE_DONTKNOW)
216 nRet = LANGUAGE_SYSTEM;
218 return nRet;
222 // static
223 LanguageType MsLangId::convertLocaleToLanguageWithFallback(
224 const ::com::sun::star::lang::Locale & rLocale )
226 // empty language => LANGUAGE_SYSTEM
227 if (rLocale.Language.getLength() == 0)
228 return lookupFallbackLanguage( LANGUAGE_SYSTEM);
230 return lookupFallbackLanguage( rLocale);
234 // static
235 ::com::sun::star::lang::Locale MsLangId::convertLanguageToLocaleWithFallback(
236 LanguageType nLang )
238 return lookupFallbackLocale( MsLangId::getRealLanguage( nLang));
242 // static
243 ::com::sun::star::lang::Locale MsLangId::getFallbackLocale(
244 const ::com::sun::star::lang::Locale & rLocale )
246 // empty language => LANGUAGE_SYSTEM
247 if (rLocale.Language.getLength() == 0)
248 return convertLanguageToLocaleWithFallback( LANGUAGE_SYSTEM);
250 return lookupFallbackLocale( rLocale);
254 // static
255 LanguageType MsLangId::getFallbackLanguage( LanguageType nLang )
257 return lookupFallbackLanguage( MsLangId::getRealLanguage( nLang));
261 // static
262 bool MsLangId::isRightToLeft( LanguageType nLang )
264 switch( nLang & LANGUAGE_MASK_PRIMARY )
266 case LANGUAGE_ARABIC_SAUDI_ARABIA & LANGUAGE_MASK_PRIMARY :
267 case LANGUAGE_HEBREW & LANGUAGE_MASK_PRIMARY :
268 case LANGUAGE_YIDDISH & LANGUAGE_MASK_PRIMARY :
269 case LANGUAGE_URDU & LANGUAGE_MASK_PRIMARY :
270 case LANGUAGE_FARSI & LANGUAGE_MASK_PRIMARY :
271 case LANGUAGE_KASHMIRI & LANGUAGE_MASK_PRIMARY :
272 case LANGUAGE_SINDHI & LANGUAGE_MASK_PRIMARY :
273 case LANGUAGE_UIGHUR_CHINA & LANGUAGE_MASK_PRIMARY :
274 return true;
276 default:
277 break;
279 return false;
283 // static
284 bool MsLangId::hasForbiddenCharacters( LanguageType nLang )
286 switch (nLang & LANGUAGE_MASK_PRIMARY)
288 case LANGUAGE_CHINESE & LANGUAGE_MASK_PRIMARY:
289 case LANGUAGE_JAPANESE & LANGUAGE_MASK_PRIMARY:
290 case LANGUAGE_KOREAN & LANGUAGE_MASK_PRIMARY:
291 return true;
292 default:
293 break;
295 return false;
299 // static
300 bool MsLangId::needsSequenceChecking( LanguageType nLang )
302 switch (nLang & LANGUAGE_MASK_PRIMARY)
304 case LANGUAGE_BURMESE & LANGUAGE_MASK_PRIMARY:
305 case LANGUAGE_KHMER & LANGUAGE_MASK_PRIMARY:
306 case LANGUAGE_LAO & LANGUAGE_MASK_PRIMARY:
307 case LANGUAGE_THAI & LANGUAGE_MASK_PRIMARY:
308 return true;
309 default:
310 break;
312 return false;
316 // static
317 sal_Int16 MsLangId::getScriptType( LanguageType nLang )
319 sal_Int16 nScript;
320 switch( nLang )
322 // CJK
323 // all LANGUAGE_CHINESE_... are caught below
324 case LANGUAGE_JAPANESE:
325 case LANGUAGE_KOREAN:
326 case LANGUAGE_KOREAN_JOHAB:
327 case LANGUAGE_USER_KOREAN_NORTH:
328 nScript = ::com::sun::star::i18n::ScriptType::ASIAN;
329 break;
331 // CTL
332 // all LANGUAGE_ARABIC_... are caught below
333 case LANGUAGE_AMHARIC_ETHIOPIA:
334 case LANGUAGE_ASSAMESE:
335 case LANGUAGE_BENGALI:
336 case LANGUAGE_BENGALI_BANGLADESH:
337 case LANGUAGE_BURMESE:
338 case LANGUAGE_FARSI:
339 case LANGUAGE_HEBREW:
340 case LANGUAGE_YIDDISH:
341 case LANGUAGE_MARATHI:
342 case LANGUAGE_PUNJABI:
343 case LANGUAGE_GUJARATI:
344 case LANGUAGE_HINDI:
345 case LANGUAGE_KANNADA:
346 case LANGUAGE_KASHMIRI:
347 case LANGUAGE_KASHMIRI_INDIA:
348 case LANGUAGE_KHMER:
349 case LANGUAGE_LAO:
350 case LANGUAGE_MALAYALAM:
351 case LANGUAGE_MANIPURI:
352 case LANGUAGE_MONGOLIAN_MONGOLIAN:
353 case LANGUAGE_NEPALI:
354 case LANGUAGE_NEPALI_INDIA:
355 case LANGUAGE_ORIYA:
356 case LANGUAGE_SANSKRIT:
357 case LANGUAGE_SINDHI:
358 case LANGUAGE_SINDHI_PAKISTAN:
359 case LANGUAGE_SINHALESE_SRI_LANKA:
360 case LANGUAGE_SYRIAC:
361 case LANGUAGE_TAMIL:
362 case LANGUAGE_TELUGU:
363 case LANGUAGE_THAI:
364 case LANGUAGE_TIBETAN:
365 case LANGUAGE_DZONGKHA:
366 case LANGUAGE_URDU:
367 case LANGUAGE_URDU_PAKISTAN:
368 case LANGUAGE_URDU_INDIA:
369 case LANGUAGE_USER_KURDISH_IRAQ:
370 case LANGUAGE_USER_KURDISH_IRAN:
371 case LANGUAGE_DHIVEHI:
372 case LANGUAGE_USER_BODO_INDIA:
373 case LANGUAGE_USER_DOGRI_INDIA:
374 case LANGUAGE_USER_MAITHILI_INDIA:
375 nScript = ::com::sun::star::i18n::ScriptType::COMPLEX;
376 break;
378 // currently not knowing scripttype - defaulted to LATIN:
380 #define LANGUAGE_ARMENIAN 0x042B
381 #define LANGUAGE_INDONESIAN 0x0421
382 #define LANGUAGE_KAZAK 0x043F
383 #define LANGUAGE_KONKANI 0x0457
384 #define LANGUAGE_MACEDONIAN 0x042F
385 #define LANGUAGE_TATAR 0x0444
388 default:
389 switch ( nLang & LANGUAGE_MASK_PRIMARY )
391 // CJK catcher
392 case LANGUAGE_CHINESE & LANGUAGE_MASK_PRIMARY:
393 nScript = ::com::sun::star::i18n::ScriptType::ASIAN;
394 break;
395 // CTL catcher
396 case LANGUAGE_ARABIC_SAUDI_ARABIA & LANGUAGE_MASK_PRIMARY:
397 nScript = ::com::sun::star::i18n::ScriptType::COMPLEX;
398 break;
399 // Western (actually not necessarily Latin but also Cyrillic, for example)
400 default:
401 nScript = ::com::sun::star::i18n::ScriptType::LATIN;
403 break;
405 return nScript;
409 // static
410 LanguageType MsLangId::getReplacementForObsoleteLanguage( LanguageType nLang )
412 switch (nLang)
414 default:
415 break; // nothing
416 case LANGUAGE_OBSOLETE_USER_LATIN:
417 nLang = LANGUAGE_LATIN;
418 break;
419 case LANGUAGE_OBSOLETE_USER_MAORI:
420 nLang = LANGUAGE_MAORI_NEW_ZEALAND;
421 break;
422 case LANGUAGE_OBSOLETE_USER_KINYARWANDA:
423 nLang = LANGUAGE_KINYARWANDA_RWANDA;
424 break;
425 case LANGUAGE_OBSOLETE_USER_UPPER_SORBIAN:
426 nLang = LANGUAGE_UPPER_SORBIAN_GERMANY;
427 break;
428 case LANGUAGE_OBSOLETE_USER_LOWER_SORBIAN:
429 nLang = LANGUAGE_LOWER_SORBIAN_GERMANY;
430 break;
431 case LANGUAGE_OBSOLETE_USER_OCCITAN:
432 nLang = LANGUAGE_OCCITAN_FRANCE;
433 break;
434 case LANGUAGE_OBSOLETE_USER_BRETON:
435 nLang = LANGUAGE_BRETON_FRANCE;
436 break;
437 case LANGUAGE_OBSOLETE_USER_KALAALLISUT:
438 nLang = LANGUAGE_KALAALLISUT_GREENLAND;
439 break;
440 case LANGUAGE_OBSOLETE_USER_LUXEMBOURGISH:
441 nLang = LANGUAGE_LUXEMBOURGISH_LUXEMBOURG;
442 break;
444 // The following are not strictly obsolete but should be mapped to a
445 // replacement locale when encountered.
447 // no_NO is an alias for nb_NO
448 case LANGUAGE_NORWEGIAN:
449 nLang = LANGUAGE_NORWEGIAN_BOKMAL;
450 break;
452 // #i94435# A Spanish variant that differs only in collation details we
453 // do not support.
454 case LANGUAGE_SPANISH_DATED:
455 nLang = LANGUAGE_SPANISH_MODERN;
456 break;
458 return nLang;