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
;
45 void MsLangId::setConfiguredSystemLanguage( LanguageType nLang
)
47 nConfiguredSystemLanguage
= nLang
;
52 void MsLangId::setConfiguredSystemUILanguage( LanguageType nLang
)
54 nConfiguredSystemUILanguage
= nLang
;
58 void MsLangId::setConfiguredWesternFallback( LanguageType nLang
)
60 nConfiguredWesternFallback
= nLang
;
64 void MsLangId::setConfiguredAsianFallback( LanguageType nLang
)
66 nConfiguredAsianFallback
= nLang
;
70 void MsLangId::setConfiguredComplexFallback( LanguageType nLang
)
72 nConfiguredComplexFallback
= nLang
;
76 inline LanguageType
MsLangId::simplifySystemLanguages( LanguageType nLang
)
80 case LANGUAGE_PROCESS_OR_USER_DEFAULT
:
81 case LANGUAGE_SYSTEM_DEFAULT
:
82 case LANGUAGE_SYSTEM
:
83 nLang
= LANGUAGE_SYSTEM
;
92 LanguageType
MsLangId::getRealLanguage( LanguageType nLang
)
94 switch (simplifySystemLanguages( nLang
))
96 case LANGUAGE_SYSTEM
:
97 if (nConfiguredSystemLanguage
== LANGUAGE_SYSTEM
)
98 nLang
= getSystemLanguage();
100 nLang
= nConfiguredSystemLanguage
;
103 if (nConfiguredSystemUILanguage
== LANGUAGE_SYSTEM
)
104 nLang
= getSystemUILanguage();
106 nLang
= nConfiguredSystemUILanguage
;
109 /* TODO: would this be useful here? */
110 //nLang = MsLangId::getReplacementForObsoleteLanguage( nLang);
113 if (nLang
== LANGUAGE_DONTKNOW
)
114 nLang
= LANGUAGE_ENGLISH_US
;
120 LanguageType
MsLangId::resolveSystemLanguageByScriptType( LanguageType nLang
, sal_Int16 nType
)
122 if (nLang
== LANGUAGE_NONE
)
125 nLang
= getRealLanguage(nLang
);
126 if (nType
!= ::com::sun::star::i18n::ScriptType::WEAK
&& getScriptType(nLang
) != nType
)
130 case ::com::sun::star::i18n::ScriptType::ASIAN
:
131 if (nConfiguredAsianFallback
== LANGUAGE_SYSTEM
)
132 nLang
= LANGUAGE_CHINESE_SIMPLIFIED
;
134 nLang
= nConfiguredAsianFallback
;
136 case ::com::sun::star::i18n::ScriptType::COMPLEX
:
137 if (nConfiguredComplexFallback
== LANGUAGE_SYSTEM
)
138 nLang
= LANGUAGE_HINDI
;
140 nLang
= nConfiguredComplexFallback
;
143 if (nConfiguredWesternFallback
== LANGUAGE_SYSTEM
)
144 nLang
= LANGUAGE_ENGLISH_US
;
146 nLang
= nConfiguredWesternFallback
;
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
);
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
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
);
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
,
192 if (nRet
== LANGUAGE_DONTKNOW
)
193 nRet
= LANGUAGE_SYSTEM
;
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
);
212 ::com::sun::star::lang::Locale
MsLangId::convertLanguageToLocaleWithFallback(
215 return lookupFallbackLocale( MsLangId::getRealLanguage( nLang
));
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
);
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
:
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
:
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
:
287 sal_Int16
MsLangId::getScriptType( LanguageType nLang
)
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
;
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
:
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
:
316 case LANGUAGE_KANNADA
:
317 case LANGUAGE_KASHMIRI
:
318 case LANGUAGE_KASHMIRI_INDIA
:
321 case LANGUAGE_MALAYALAM
:
322 case LANGUAGE_MANIPURI
:
323 case LANGUAGE_MONGOLIAN_MONGOLIAN
:
324 case LANGUAGE_NEPALI
:
325 case LANGUAGE_NEPALI_INDIA
:
327 case LANGUAGE_SANSKRIT
:
328 case LANGUAGE_SINDHI
:
329 case LANGUAGE_SINDHI_PAKISTAN
:
330 case LANGUAGE_SINHALESE_SRI_LANKA
:
331 case LANGUAGE_SYRIAC
:
333 case LANGUAGE_TELUGU
:
335 case LANGUAGE_TIBETAN
:
336 case LANGUAGE_DZONGKHA
:
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
;
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
363 switch ( nLang
& LANGUAGE_MASK_PRIMARY
)
366 case LANGUAGE_CHINESE
& LANGUAGE_MASK_PRIMARY
:
367 nScript
= ::com::sun::star::i18n::ScriptType::ASIAN
;
370 case LANGUAGE_ARABIC_SAUDI_ARABIA
& LANGUAGE_MASK_PRIMARY
:
371 nScript
= ::com::sun::star::i18n::ScriptType::COMPLEX
;
373 // Western (actually not necessarily Latin but also Cyrillic, for example)
375 nScript
= ::com::sun::star::i18n::ScriptType::LATIN
;
384 LanguageType
MsLangId::getReplacementForObsoleteLanguage( LanguageType nLang
, bool bUserInterfaceSelection
)
390 case LANGUAGE_OBSOLETE_USER_LATIN
:
391 nLang
= LANGUAGE_LATIN
;
393 case LANGUAGE_OBSOLETE_USER_MAORI
:
394 nLang
= LANGUAGE_MAORI_NEW_ZEALAND
;
396 case LANGUAGE_OBSOLETE_USER_KINYARWANDA
:
397 nLang
= LANGUAGE_KINYARWANDA_RWANDA
;
399 case LANGUAGE_OBSOLETE_USER_UPPER_SORBIAN
:
400 nLang
= LANGUAGE_UPPER_SORBIAN_GERMANY
;
402 case LANGUAGE_OBSOLETE_USER_LOWER_SORBIAN
:
403 nLang
= LANGUAGE_LOWER_SORBIAN_GERMANY
;
405 case LANGUAGE_OBSOLETE_USER_OCCITAN
:
406 nLang
= LANGUAGE_OCCITAN_FRANCE
;
408 case LANGUAGE_OBSOLETE_USER_BRETON
:
409 nLang
= LANGUAGE_BRETON_FRANCE
;
411 case LANGUAGE_OBSOLETE_USER_KALAALLISUT
:
412 nLang
= LANGUAGE_KALAALLISUT_GREENLAND
;
414 case LANGUAGE_OBSOLETE_USER_LUXEMBOURGISH
:
415 nLang
= LANGUAGE_LUXEMBOURGISH_LUXEMBOURG
;
417 case LANGUAGE_OBSOLETE_USER_KABYLE
:
418 nLang
= LANGUAGE_TAMAZIGHT_LATIN
;
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
;
429 // #i94435# A Spanish variant that differs only in collation details we
431 case LANGUAGE_SPANISH_DATED
:
432 nLang
= LANGUAGE_SPANISH_MODERN
;
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
;
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */