1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_I18NLANGTAG_MSLANGID_HXX
21 #define INCLUDED_I18NLANGTAG_MSLANGID_HXX
23 #include <sal/config.h>
25 #include <i18nlangtag/i18nlangtagdllapi.h>
26 #include <i18nlangtag/lang.h>
27 #include <com/sun/star/lang/Locale.hpp>
29 #include <string_view>
33 struct IsoLanguageCountryEntry
;
34 struct IsoLanguageScriptCountryEntry
;
35 struct Bcp47CountryEntry
;
37 /** Methods related to Microsoft language IDs. For details about MS-LANGIDs
39 class SAL_WARN_UNUSED I18NLANGTAG_DLLPUBLIC MsLangId
43 /// Create a LangID from a primary and a sublanguage.
44 static LanguageType
makeLangID( LanguageType nSubLangId
, LanguageType nPriLangId
)
46 return LanguageType((sal_uInt16(nSubLangId
) << 10) | sal_uInt16(nPriLangId
));
49 /// Get the primary language of a LangID.
50 static LanguageType
getPrimaryLanguage( LanguageType nLangID
)
52 return LanguageType(sal_uInt16(nLangID
) & LANGUAGE_MASK_PRIMARY
);
55 /// Get the sublanguage of a LangID.
56 static LanguageType
getSubLanguage( LanguageType nLangID
)
58 return LanguageType((sal_uInt16(nLangID
) & ~LANGUAGE_MASK_PRIMARY
) >> 10);
61 /** Get the configured system language/locale.
62 If not set yet then getSystemLanguage().
64 Prefer this over getSystemLanguage() unless you exactly know why you
66 static LanguageType
getConfiguredSystemLanguage();
68 /** Get the configured system UI language/locale.
69 If not set yet then getSystemUILanguage().
71 Prefer this over getSystemUILanguage() unless you exactly know why you
73 static LanguageType
getConfiguredSystemUILanguage();
76 /** Language/locale of category LC_CTYPE (on Unix, else the system
78 Evaluation order: LC_ALL, LC_CTYPE, LANG
79 Note this may return an unknown unsupported locale. */
80 static LanguageType
getSystemLanguage();
82 /** Language/locale of category LC_MESSAGES (on Unix, else same as
84 Evaluation order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG
85 Note this may return an unknown unsupported locale. */
86 static LanguageType
getSystemUILanguage();
89 /** @short: A proper language/locale if the nLang parameter designates some
92 @descr: NOTE: The "system" values may be overridden by the
93 application's configuration.
96 case LANGUAGE_PROCESS_OR_USER_DEFAULT : configured or system language
97 case LANGUAGE_SYSTEM_DEFAULT : configured or system language
98 case LANGUAGE_SYSTEM : configured or system language
99 case LANGUAGE_HID_HUMAN_INTERFACE_DEVICE : configured or system UI language
100 case LANGUAGE_DONTKNOW : LANGUAGE_ENGLISH_US
103 In case the configured language is LANGUAGE_SYSTEM, which is also
104 the initial default, the system language is obtained. In case the
105 configured or resulting system language is LANGUAGE_DONTKNOW,
106 LANGUAGE_ENGLISH_US is returned instead.
108 static LanguageType
getRealLanguage( LanguageType nLang
);
111 // TODO: refactor to LanguageTag? Used only in
112 // i18npool/source/localedata/localedata.cxx
114 /** Get fall-back Locale for Locale with handling of an empty language name
115 designating the SYSTEM language. Returns the same Locale if an exact
118 static css::lang::Locale
getFallbackLocale( const css::lang::Locale
& rLocale
);
121 // TODO: refactor to LanguageTag, used only in
122 // i18npool/source/isolang/inunx.cxx to convert Unix locale string
124 static LanguageType
convertUnxByteStringToLanguage( std::string_view rString
);
127 static LanguageType
resolveSystemLanguageByScriptType( LanguageType nLang
, sal_Int16 nType
);
130 /** Whether the language uses hyphenation. */
131 static bool usesHyphenation( LanguageType nLang
);
133 /** Whether locale has a Right-To-Left orientation for text. */
134 static bool isRightToLeft( LanguageType nLang
);
136 /** Whether locale has a Right-To-Left orientation for math. */
137 static bool isRightToLeftMath( LanguageType nLang
);
139 /** Whether locale is a CJK locale */
140 static bool isCJK( LanguageType nLang
);
142 /** Whether locale is a chinese locale */
143 static bool isChinese( LanguageType nLang
);
145 /** Whether locale is a simplified chinese locale */
146 static bool isSimplifiedChinese( LanguageType nLang
);
148 /** Whether locale is a traditional chinese locale */
149 static bool isTraditionalChinese( LanguageType nLang
);
151 /** Whether locale is a korean locale */
152 static bool isKorean( LanguageType nLang
);
154 /** Whether locale is a simplified chinese locale */
155 static bool isSimplifiedChinese( const css::lang::Locale
& rLocale
);
157 /** Whether locale is a traditional chinese locale */
158 static bool isTraditionalChinese( const css::lang::Locale
& rLocale
);
160 /** Whether locale is one where family name comes first, e.g. Japan, Hungary, Samoa */
161 static bool isFamilyNameFirst( LanguageType nLang
);
163 /** Whether there are "forbidden characters at start or end of line" in
164 this locale. CJK locales.
166 @see offapi/com/sun/star/i18n/ForbiddenCharacters.idl
168 static bool hasForbiddenCharacters( LanguageType nLang
);
171 /** Whether locale needs input sequence checking. CTL locales. */
172 static bool needsSequenceChecking( LanguageType nLang
);
175 /** Get css::i18n::ScriptType of locale. */
176 static sal_Int16
getScriptType( LanguageType nLang
);
178 /** Whether locale is "Western" but not Latin script, e.g. Cyrillic or Greek. */
179 static bool isNonLatinWestern( LanguageType nLang
);
182 /** Map an obsolete user defined LANGID (see lang.h
183 LANGUAGE_OBSOLETE_USER_...) to the new value defined by MS in the
186 Also used to map UI localizations using reserved ISO codes to something
187 "official" but not identical in order to not pollute documents with
190 static LanguageType
getReplacementForObsoleteLanguage( LanguageType nLang
);
192 /** Whether locale is legacy, i.e. country or confederation doesn't exist anymore. */
193 static bool isLegacy( LanguageType nLang
);
196 /** @ATTENTION: these are _ONLY_ to be called by the application's
198 static void setConfiguredSystemUILanguage( LanguageType nLang
);
199 static void setConfiguredWesternFallback( LanguageType nLang
);
200 static void setConfiguredComplexFallback( LanguageType nLang
);
201 static void setConfiguredAsianFallback( LanguageType nLang
);
204 /** Encapsulated methods that shall only be accessed through
207 class LanguageTagAccess
211 friend class LanguageTag
;
213 /** Configured system locale needs always be synchronized with
214 LanguageTag's system locale.
216 I18NLANGTAG_DLLPRIVATE
static void setConfiguredSystemLanguage( LanguageType nLang
);
220 struct LanguagetagMapping
225 LanguagetagMapping( OUString aBcp47
, LanguageType nLang
) : maBcp47(std::move(aBcp47
)), mnLang(nLang
) {}
228 /** @internal - Obtain a list of known locales (i.e. those that have a
229 defined mapping between MS-LangID and ISO codes or tags) as BCP 47
230 language tag strings.
232 static ::std::vector
< LanguagetagMapping
> getDefinedLanguagetags();
235 /** Encapsulated conversion methods used by LanguageTag and conversions,
236 not to be used by anything else.
242 friend class LanguageTag
;
243 friend class LanguageTagImpl
;
245 friend css::lang::Locale
MsLangId::getFallbackLocale(
246 const css::lang::Locale
& rLocale
);
248 friend LanguageType
MsLangId::convertUnxByteStringToLanguage(
249 std::string_view rString
);
252 /** Convert a Locale to a LanguageType with handling of an empty
253 language name designating LANGUAGE_SYSTEM.
255 I18NLANGTAG_DLLPRIVATE
static LanguageType
convertLocaleToLanguage(
256 const css::lang::Locale
& rLocale
);
258 /** Used by convertLocaleToLanguage(Locale) */
259 I18NLANGTAG_DLLPRIVATE
static LanguageType
convertLocaleToLanguageImpl(
260 const css::lang::Locale
& rLocale
);
262 /** Convert x-... privateuse, used by convertLocaleToLanguageImpl(Locale) */
263 I18NLANGTAG_DLLPRIVATE
static LanguageType
convertPrivateUseToLanguage(
264 std::u16string_view rPriv
);
266 /** Used by LanguageTag::canonicalize() */
267 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
getOverride(
268 const css::lang::Locale
& rLocale
);
270 /** Used by convertLocaleToLanguageImpl(Locale) and LanguageTagImpl::convertLocaleToLang() */
271 I18NLANGTAG_DLLPRIVATE
static LanguageType
convertIsoNamesToLanguage(
272 const OUString
& rLang
, const OUString
& rCountry
, bool bSkipIsoTable
);
275 /** Used by convertUnxByteStringToLanguage(OString) */
276 I18NLANGTAG_DLLPRIVATE
static LanguageType
convertIsoNamesToLanguage(
277 std::string_view rLang
, std::string_view rCountry
);
280 /** Used by lookupFallbackLocale(Locale) */
281 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
getLocale(
282 const IsoLanguageCountryEntry
* pEntry
);
284 /** Used by lookupFallbackLocale(Locale) */
285 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
getLocale(
286 const IsoLanguageScriptCountryEntry
* pEntry
);
288 /** Used by lookupFallbackLocale(Locale) */
289 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
getLocale(
290 const Bcp47CountryEntry
* pEntry
);
293 /** Convert a LanguageType to a Locale. */
294 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
convertLanguageToLocale(
295 LanguageType nLang
, bool bIgnoreOverride
);
297 /** Used by convertLanguageToLocale(LanguageType,bool) and
298 getLocale(IsoLanguageCountryEntry*) and
299 getLocale(IsoLanguageScriptCountryEntry*) and
300 getLocale(Bcp47CountryEntry*)
302 @param bIgnoreOverride
303 If bIgnoreOverride==true, a matching entry is used even if
304 mnOverride is set, for conversion to an even outdated tag.
305 If bIgnoreOverride==false, a matching entry is skipped if
306 mnOverride is set and instead the override is followed.
308 @return rLocale set to mapped values, unchanged if no mapping was
309 found. E.g. pass empty Locale to obtain empty SYSTEM locale
312 I18NLANGTAG_DLLPRIVATE
static void convertLanguageToLocaleImpl(
313 LanguageType nLang
, css::lang::Locale
& rLocale
, bool bIgnoreOverride
);
316 I18NLANGTAG_DLLPRIVATE
static css::lang::Locale
lookupFallbackLocale(
317 const css::lang::Locale
& rLocale
);
322 static LanguageType nConfiguredSystemLanguage
;
323 static LanguageType nConfiguredSystemUILanguage
;
325 static LanguageType nConfiguredWesternFallback
;
326 static LanguageType nConfiguredAsianFallback
;
327 static LanguageType nConfiguredComplexFallback
;
329 static LanguageType
getPlatformSystemLanguage();
330 static LanguageType
getPlatformSystemUILanguage();
332 // Substitute LANGUAGE_SYSTEM for LANGUAGE_SYSTEM_DEFAULT and
333 // LANGUAGE_PROCESS_OR_USER_DEFAULT, other values aren't touched.
334 I18NLANGTAG_DLLPRIVATE
static inline LanguageType
simplifySystemLanguages( LanguageType nLang
);
337 #endif // INCLUDED_I18NLANGTAG_MSLANGID_HXX
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */