Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / i18npool / source / isolang / mslangid.cxx
blob3cd877e4c91b23bca96f31371d0cf5529bfa1fea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <sal/config.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/string.hxx>
32 #include <com/sun/star/i18n/ScriptType.hpp>
34 #include "i18npool/mslangid.hxx"
37 LanguageType MsLangId::nConfiguredSystemLanguage = LANGUAGE_SYSTEM;
38 LanguageType MsLangId::nConfiguredSystemUILanguage = LANGUAGE_SYSTEM;
40 LanguageType MsLangId::nConfiguredWesternFallback = LANGUAGE_SYSTEM;
41 LanguageType MsLangId::nConfiguredAsianFallback = LANGUAGE_SYSTEM;
42 LanguageType MsLangId::nConfiguredComplexFallback = LANGUAGE_SYSTEM;
44 // static
45 void MsLangId::setConfiguredSystemLanguage( LanguageType nLang )
47 nConfiguredSystemLanguage = nLang;
51 // static
52 void MsLangId::setConfiguredSystemUILanguage( LanguageType nLang )
54 nConfiguredSystemUILanguage = nLang;
57 // static
58 void MsLangId::setConfiguredWesternFallback( LanguageType nLang )
60 nConfiguredWesternFallback = nLang;
63 // static
64 void MsLangId::setConfiguredAsianFallback( LanguageType nLang )
66 nConfiguredAsianFallback = nLang;
69 // static
70 void MsLangId::setConfiguredComplexFallback( LanguageType nLang )
72 nConfiguredComplexFallback = nLang;
75 // static
76 inline LanguageType MsLangId::simplifySystemLanguages( LanguageType nLang )
78 switch (nLang)
80 case LANGUAGE_PROCESS_OR_USER_DEFAULT :
81 case LANGUAGE_SYSTEM_DEFAULT :
82 case LANGUAGE_SYSTEM :
83 nLang = LANGUAGE_SYSTEM;
84 break;
85 default:
86 ; // nothing
88 return nLang;
91 // static
92 LanguageType MsLangId::getRealLanguage( LanguageType nLang )
94 switch (simplifySystemLanguages( nLang))
96 case LANGUAGE_SYSTEM :
97 if (nConfiguredSystemLanguage == LANGUAGE_SYSTEM)
98 nLang = getSystemLanguage();
99 else
100 nLang = nConfiguredSystemLanguage;
101 break;
102 case LANGUAGE_NONE :
103 if (nConfiguredSystemUILanguage == LANGUAGE_SYSTEM)
104 nLang = getSystemUILanguage();
105 else
106 nLang = nConfiguredSystemUILanguage;
107 break;
108 default:
109 /* TODO: would this be useful here? */
110 //nLang = MsLangId::getReplacementForObsoleteLanguage( nLang);
111 ; // nothing
113 if (nLang == LANGUAGE_DONTKNOW)
114 nLang = LANGUAGE_ENGLISH_US;
115 return nLang;
119 // static
120 LanguageType MsLangId::resolveSystemLanguageByScriptType( LanguageType nLang, sal_Int16 nType )
122 if (nLang == LANGUAGE_NONE)
123 return nLang;
125 nLang = getRealLanguage(nLang);
126 if (nType != ::com::sun::star::i18n::ScriptType::WEAK && getScriptType(nLang) != nType)
128 switch(nType)
130 case ::com::sun::star::i18n::ScriptType::ASIAN:
131 if (nConfiguredAsianFallback == LANGUAGE_SYSTEM)
132 nLang = LANGUAGE_CHINESE_SIMPLIFIED;
133 else
134 nLang = nConfiguredAsianFallback;
135 break;
136 case ::com::sun::star::i18n::ScriptType::COMPLEX:
137 if (nConfiguredComplexFallback == LANGUAGE_SYSTEM)
138 nLang = LANGUAGE_HINDI;
139 else
140 nLang = nConfiguredComplexFallback;
141 break;
142 default:
143 if (nConfiguredWesternFallback == LANGUAGE_SYSTEM)
144 nLang = LANGUAGE_ENGLISH_US;
145 else
146 nLang = nConfiguredWesternFallback;
147 break;
150 return nLang;
153 // static
154 void MsLangId::convertLanguageToLocale( LanguageType nLang,
155 ::com::sun::star::lang::Locale & rLocale )
157 if (!rLocale.Variant.isEmpty())
158 rLocale.Variant = rtl::OUString();
159 convertLanguageToIsoNames( nLang, rLocale.Language, rLocale.Country);
163 // static
164 ::com::sun::star::lang::Locale MsLangId::convertLanguageToLocale(
165 LanguageType nLang, bool bResolveSystem )
167 ::com::sun::star::lang::Locale aLocale;
168 if (!bResolveSystem && simplifySystemLanguages( nLang) == LANGUAGE_SYSTEM)
169 ; // nothing => empty locale
170 else
172 // Still resolve LANGUAGE_DONTKNOW if resolving is not requested,
173 // but not LANGUAGE_NONE or others.
174 if (bResolveSystem || nLang == LANGUAGE_DONTKNOW)
175 nLang = MsLangId::getRealLanguage( nLang);
176 convertLanguageToLocale( nLang, aLocale);
178 return aLocale;
182 // static
183 LanguageType MsLangId::convertLocaleToLanguage(
184 const ::com::sun::star::lang::Locale& rLocale )
186 // empty language => LANGUAGE_SYSTEM
187 if (rLocale.Language.isEmpty())
188 return LANGUAGE_SYSTEM;
190 LanguageType nRet = convertIsoNamesToLanguage( rLocale.Language,
191 rLocale.Country);
192 if (nRet == LANGUAGE_DONTKNOW)
193 nRet = LANGUAGE_SYSTEM;
195 return nRet;
199 // static
200 LanguageType MsLangId::convertLocaleToLanguageWithFallback(
201 const ::com::sun::star::lang::Locale & rLocale )
203 // empty language => LANGUAGE_SYSTEM
204 if (rLocale.Language.isEmpty())
205 return lookupFallbackLanguage( LANGUAGE_SYSTEM);
207 return lookupFallbackLanguage( rLocale);
211 // static
212 ::com::sun::star::lang::Locale MsLangId::convertLanguageToLocaleWithFallback(
213 LanguageType nLang )
215 return lookupFallbackLocale( MsLangId::getRealLanguage( nLang));
219 // static
220 ::com::sun::star::lang::Locale MsLangId::getFallbackLocale(
221 const ::com::sun::star::lang::Locale & rLocale )
223 // empty language => LANGUAGE_SYSTEM
224 if (rLocale.Language.isEmpty())
225 return convertLanguageToLocaleWithFallback( LANGUAGE_SYSTEM);
227 return lookupFallbackLocale( rLocale);
230 // static
231 bool MsLangId::isRightToLeft( LanguageType nLang )
233 switch( nLang & LANGUAGE_MASK_PRIMARY )
235 case LANGUAGE_ARABIC_SAUDI_ARABIA & LANGUAGE_MASK_PRIMARY :
236 case LANGUAGE_HEBREW & LANGUAGE_MASK_PRIMARY :
237 case LANGUAGE_YIDDISH & LANGUAGE_MASK_PRIMARY :
238 case LANGUAGE_URDU & LANGUAGE_MASK_PRIMARY :
239 case LANGUAGE_FARSI & LANGUAGE_MASK_PRIMARY :
240 case LANGUAGE_KASHMIRI & LANGUAGE_MASK_PRIMARY :
241 case LANGUAGE_SINDHI & LANGUAGE_MASK_PRIMARY :
242 case LANGUAGE_UIGHUR_CHINA & LANGUAGE_MASK_PRIMARY :
243 case LANGUAGE_USER_KYRGYZ_CHINA & LANGUAGE_MASK_PRIMARY :
244 return true;
246 default:
247 break;
249 return false;
253 // static
254 bool MsLangId::hasForbiddenCharacters( LanguageType nLang )
256 switch (nLang & LANGUAGE_MASK_PRIMARY)
258 case LANGUAGE_CHINESE & LANGUAGE_MASK_PRIMARY:
259 case LANGUAGE_JAPANESE & LANGUAGE_MASK_PRIMARY:
260 case LANGUAGE_KOREAN & LANGUAGE_MASK_PRIMARY:
261 return true;
262 default:
263 break;
265 return false;
269 // static
270 bool MsLangId::needsSequenceChecking( LanguageType nLang )
272 switch (nLang & LANGUAGE_MASK_PRIMARY)
274 case LANGUAGE_BURMESE & LANGUAGE_MASK_PRIMARY:
275 case LANGUAGE_KHMER & LANGUAGE_MASK_PRIMARY:
276 case LANGUAGE_LAO & LANGUAGE_MASK_PRIMARY:
277 case LANGUAGE_THAI & LANGUAGE_MASK_PRIMARY:
278 return true;
279 default:
280 break;
282 return false;
286 // static
287 sal_Int16 MsLangId::getScriptType( LanguageType nLang )
289 sal_Int16 nScript;
290 switch( nLang )
292 // CJK
293 // all LANGUAGE_CHINESE_... are caught below
294 case LANGUAGE_JAPANESE:
295 case LANGUAGE_KOREAN:
296 case LANGUAGE_KOREAN_JOHAB:
297 case LANGUAGE_USER_KOREAN_NORTH:
298 nScript = ::com::sun::star::i18n::ScriptType::ASIAN;
299 break;
301 // CTL
302 // all LANGUAGE_ARABIC_... are caught below
303 case LANGUAGE_AMHARIC_ETHIOPIA:
304 case LANGUAGE_ASSAMESE:
305 case LANGUAGE_BENGALI:
306 case LANGUAGE_BENGALI_BANGLADESH:
307 case LANGUAGE_BURMESE:
308 case LANGUAGE_FARSI:
309 case LANGUAGE_HEBREW:
310 case LANGUAGE_YIDDISH:
311 case LANGUAGE_USER_YIDDISH_US:
312 case LANGUAGE_MARATHI:
313 case LANGUAGE_PUNJABI:
314 case LANGUAGE_GUJARATI:
315 case LANGUAGE_HINDI:
316 case LANGUAGE_KANNADA:
317 case LANGUAGE_KASHMIRI:
318 case LANGUAGE_KASHMIRI_INDIA:
319 case LANGUAGE_KHMER:
320 case LANGUAGE_LAO:
321 case LANGUAGE_MALAYALAM:
322 case LANGUAGE_MANIPURI:
323 case LANGUAGE_MONGOLIAN_MONGOLIAN:
324 case LANGUAGE_NEPALI:
325 case LANGUAGE_NEPALI_INDIA:
326 case LANGUAGE_ORIYA:
327 case LANGUAGE_SANSKRIT:
328 case LANGUAGE_SINDHI:
329 case LANGUAGE_SINDHI_PAKISTAN:
330 case LANGUAGE_SINHALESE_SRI_LANKA:
331 case LANGUAGE_SYRIAC:
332 case LANGUAGE_TAMIL:
333 case LANGUAGE_TELUGU:
334 case LANGUAGE_THAI:
335 case LANGUAGE_TIBETAN:
336 case LANGUAGE_DZONGKHA:
337 case LANGUAGE_URDU:
338 case LANGUAGE_URDU_PAKISTAN:
339 case LANGUAGE_URDU_INDIA:
340 case LANGUAGE_USER_KURDISH_IRAQ:
341 case LANGUAGE_USER_KURDISH_IRAN:
342 case LANGUAGE_DHIVEHI:
343 case LANGUAGE_USER_BODO_INDIA:
344 case LANGUAGE_USER_DOGRI_INDIA:
345 case LANGUAGE_USER_MAITHILI_INDIA:
346 case LANGUAGE_UIGHUR_CHINA:
347 case LANGUAGE_USER_LIMBU:
348 case LANGUAGE_USER_KYRGYZ_CHINA:
349 nScript = ::com::sun::star::i18n::ScriptType::COMPLEX;
350 break;
352 // currently not knowing scripttype - defaulted to LATIN:
354 #define LANGUAGE_ARMENIAN 0x042B
355 #define LANGUAGE_INDONESIAN 0x0421
356 #define LANGUAGE_KAZAK 0x043F
357 #define LANGUAGE_KONKANI 0x0457
358 #define LANGUAGE_MACEDONIAN 0x042F
359 #define LANGUAGE_TATAR 0x0444
362 default:
363 switch ( nLang & LANGUAGE_MASK_PRIMARY )
365 // CJK catcher
366 case LANGUAGE_CHINESE & LANGUAGE_MASK_PRIMARY:
367 nScript = ::com::sun::star::i18n::ScriptType::ASIAN;
368 break;
369 // CTL catcher
370 case LANGUAGE_ARABIC_SAUDI_ARABIA & LANGUAGE_MASK_PRIMARY:
371 nScript = ::com::sun::star::i18n::ScriptType::COMPLEX;
372 break;
373 // Western (actually not necessarily Latin but also Cyrillic, for example)
374 default:
375 nScript = ::com::sun::star::i18n::ScriptType::LATIN;
377 break;
379 return nScript;
383 // static
384 LanguageType MsLangId::getReplacementForObsoleteLanguage( LanguageType nLang, bool bUserInterfaceSelection )
386 switch (nLang)
388 default:
389 break; // nothing
390 case LANGUAGE_OBSOLETE_USER_LATIN:
391 nLang = LANGUAGE_LATIN;
392 break;
393 case LANGUAGE_OBSOLETE_USER_MAORI:
394 nLang = LANGUAGE_MAORI_NEW_ZEALAND;
395 break;
396 case LANGUAGE_OBSOLETE_USER_KINYARWANDA:
397 nLang = LANGUAGE_KINYARWANDA_RWANDA;
398 break;
399 case LANGUAGE_OBSOLETE_USER_UPPER_SORBIAN:
400 nLang = LANGUAGE_UPPER_SORBIAN_GERMANY;
401 break;
402 case LANGUAGE_OBSOLETE_USER_LOWER_SORBIAN:
403 nLang = LANGUAGE_LOWER_SORBIAN_GERMANY;
404 break;
405 case LANGUAGE_OBSOLETE_USER_OCCITAN:
406 nLang = LANGUAGE_OCCITAN_FRANCE;
407 break;
408 case LANGUAGE_OBSOLETE_USER_BRETON:
409 nLang = LANGUAGE_BRETON_FRANCE;
410 break;
411 case LANGUAGE_OBSOLETE_USER_KALAALLISUT:
412 nLang = LANGUAGE_KALAALLISUT_GREENLAND;
413 break;
414 case LANGUAGE_OBSOLETE_USER_LUXEMBOURGISH:
415 nLang = LANGUAGE_LUXEMBOURGISH_LUXEMBOURG;
416 break;
417 case LANGUAGE_OBSOLETE_USER_KABYLE:
418 nLang = LANGUAGE_TAMAZIGHT_LATIN;
419 break;
421 // The following are not strictly obsolete but should be mapped to a
422 // replacement locale when encountered.
424 // no_NO is an alias for nb_NO
425 case LANGUAGE_NORWEGIAN:
426 nLang = LANGUAGE_NORWEGIAN_BOKMAL;
427 break;
429 // #i94435# A Spanish variant that differs only in collation details we
430 // do not support.
431 case LANGUAGE_SPANISH_DATED:
432 nLang = LANGUAGE_SPANISH_MODERN;
433 break;
435 // Do not use ca-XV for document content.
436 /* TODO: remove in case we implement BCP47 language tags. */
437 case LANGUAGE_USER_CATALAN_VALENCIAN:
438 if (!bUserInterfaceSelection)
439 nLang = LANGUAGE_CATALAN;
440 break;
442 return nLang;
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */