Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_I18NLANGTAG_MSLANGID_HXX
21 : #define INCLUDED_I18NLANGTAG_MSLANGID_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : #include <i18nlangtag/i18nlangtagdllapi.h>
26 : #include <i18nlangtag/lang.h>
27 : #include <com/sun/star/lang/Locale.hpp>
28 : #include <vector>
29 :
30 : struct IsoLanguageCountryEntry;
31 : struct IsoLanguageScriptCountryEntry;
32 :
33 : /** Methods related to Microsoft language IDs. For details about MS-LANGIDs
34 : please see lang.h */
35 : class I18NLANGTAG_DLLPUBLIC MsLangId
36 : {
37 : public:
38 :
39 : /// Create a LangID from a primary and a sublanguage.
40 0 : static inline LanguageType makeLangID( LanguageType nSubLangId, LanguageType nPriLangId)
41 : {
42 0 : return (nSubLangId << 10) | nPriLangId;
43 : }
44 :
45 : /// Get the primary language of a LangID.
46 2 : static inline LanguageType getPrimaryLanguage( LanguageType nLangID)
47 : {
48 2 : return nLangID & LANGUAGE_MASK_PRIMARY;
49 : }
50 :
51 : /// Get the sublanguage of a LangID.
52 1 : static inline LanguageType getSubLanguage( LanguageType nLangID)
53 : {
54 1 : return (nLangID & ~LANGUAGE_MASK_PRIMARY) >> 10;
55 : }
56 :
57 : /** Language/locale of category LC_CTYPE (on Unix, else the system
58 : language).
59 : Evaluation order: LC_ALL, LC_CTYPE, LANG */
60 : static LanguageType getSystemLanguage();
61 :
62 : /** Language/locale of category LC_MESSAGES (on Unix, else same as
63 : GetSystemLanguage()).
64 : Evaluation order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG */
65 : static LanguageType getSystemUILanguage();
66 :
67 :
68 : /** @short: A proper language/locale if the nLang parameter designates some
69 : special value.
70 :
71 : @descr: NOTE: The "system" values may be overridden by the
72 : application's configuration.
73 :
74 : @returns
75 : case LANGUAGE_PROCESS_OR_USER_DEFAULT : configured or system language
76 : case LANGUAGE_SYSTEM_DEFAULT : configured or system language
77 : case LANGUAGE_SYSTEM : configured or system language
78 : case LANGUAGE_HID_HUMAN_INTERFACE_DEVICE : configured or system UI language
79 : case LANGUAGE_DONTKNOW : LANGUAGE_ENGLISH_US
80 : else: nLang
81 :
82 : In case the configured language is LANGUAGE_SYSTEM, which is also
83 : the initial default, the system language is obtained. In case the
84 : configured or resulting system language is LANGUAGE_DONTKNOW,
85 : LANGUAGE_ENGLISH_US is returned instead.
86 : */
87 : static LanguageType getRealLanguage( LanguageType nLang );
88 :
89 :
90 : // TODO: refactor to LanguageTag? Used only in
91 : // i18npool/source/localedata/localedata.cxx
92 :
93 : /** Get fall-back Locale for Locale with handling of an empty language name
94 : designating the SYSTEM language. Returns the same Locale if an exact
95 : match was found.
96 : */
97 : static ::com::sun::star::lang::Locale getFallbackLocale(
98 : const ::com::sun::star::lang::Locale & rLocale );
99 :
100 :
101 : // TODO: refactor to LanguageTag, used only in
102 : // i18npool/source/isolang/inunx.cxx to convert Unix locale string
103 :
104 : static LanguageType convertUnxByteStringToLanguage( const OString& rString );
105 :
106 :
107 : static LanguageType resolveSystemLanguageByScriptType( LanguageType nLang, sal_Int16 nType );
108 :
109 :
110 : /** Whether locale has a Right-To-Left orientation for text. */
111 : static bool isRightToLeft( LanguageType nLang );
112 :
113 : /** Whether locale has a Right-To-Left orientation for math. */
114 : static bool isRightToLeftMath( LanguageType nLang );
115 :
116 : /** Whether locale is a CJK locale */
117 : static bool isCJK( LanguageType nLang );
118 :
119 : /** Whether locale is a chinese locale */
120 : static bool isChinese( LanguageType nLang );
121 :
122 : /** Whether locale is a simplified chinese locale */
123 : static bool isSimplifiedChinese( LanguageType nLang );
124 :
125 : /** Whether locale is a traditional chinese locale */
126 : static bool isTraditionalChinese( LanguageType nLang );
127 :
128 : /** Whether locale is a korean locale */
129 : static bool isKorean( LanguageType nLang );
130 :
131 : /** Whether locale is a simplified chinese locale */
132 : static bool isSimplifiedChinese( const ::com::sun::star::lang::Locale & rLocale );
133 :
134 : /** Whether locale is a traditional chinese locale */
135 : static bool isTraditionalChinese( const ::com::sun::star::lang::Locale & rLocale );
136 :
137 : /** Whether locale is one where family name comes first, e.g. Japan, Hungary, Samoa */
138 : static bool isFamilyNameFirst( LanguageType nLang );
139 :
140 : /** Whether there are "forbidden characters at start or end of line" in
141 : this locale. CJK locales.
142 :
143 : @see offapi/com/sun/star/i18n/ForbiddenCharacters.idl
144 : */
145 : static bool hasForbiddenCharacters( LanguageType nLang );
146 :
147 :
148 : /** Whether locale needs input sequence checking. CTL locales. */
149 : static bool needsSequenceChecking( LanguageType nLang );
150 :
151 :
152 : /** Get ::com::sun::star::i18n::ScriptType of locale. */
153 : static sal_Int16 getScriptType( LanguageType nLang );
154 :
155 : /** Whether locale is "Western" but not Latin script, e.g. Cyrillic or Greek. */
156 : static bool isNonLatinWestern( LanguageType nLang );
157 :
158 :
159 : /** Map an obsolete user defined LANGID (see lang.h
160 : LANGUAGE_OBSOLETE_USER_...) to the new value defined by MS in the
161 : meantime.
162 :
163 : Also used to map UI localizations using reserved ISO codes to something
164 : "official" but not identical in order to not pollute documents with
165 : invalid ISO codes.
166 :
167 : @param bUserInterfaceSelection
168 : If TRUE, don't replace such UI-only locale. Only use for
169 : Tools->Options->LanguageSettings->UserInterface listbox.
170 : If FALSE, do replace.
171 : */
172 : static LanguageType getReplacementForObsoleteLanguage( LanguageType nLang,
173 : bool bUserInterfaceSelection = false );
174 :
175 :
176 : /** @ATTENTION: these are _ONLY_ to be called by the application's
177 : configuration! */
178 : static void setConfiguredSystemUILanguage( LanguageType nLang );
179 : static void setConfiguredWesternFallback( LanguageType nLang );
180 : static void setConfiguredComplexFallback( LanguageType nLang );
181 : static void setConfiguredAsianFallback( LanguageType nLang );
182 :
183 :
184 :
185 : /** Encapsulated methods that shall only be accessed through
186 : class LanguageTag.
187 : */
188 : class LanguageTagAccess
189 : {
190 : private:
191 :
192 : friend class LanguageTag;
193 :
194 : /** Configured system locale needs always be synchronized with
195 : LanguageTag's system locale.
196 : */
197 : I18NLANGTAG_DLLPRIVATE static void setConfiguredSystemLanguage( LanguageType nLang );
198 : };
199 :
200 :
201 :
202 0 : struct LanguagetagMapping
203 : {
204 : OUString maBcp47;
205 : LanguageType mnLang;
206 :
207 0 : LanguagetagMapping( const OUString & rBcp47, LanguageType nLang ) : maBcp47(rBcp47), mnLang(nLang) {}
208 : };
209 :
210 : /** @internal - Obtain a list of known locales (i.e. those that have a
211 : defined mapping between MS-LangID and ISO codes or tags) as BCP 47
212 : language tag strings.
213 : */
214 : static ::std::vector< LanguagetagMapping > getDefinedLanguagetags();
215 :
216 :
217 :
218 : /** Encapsulated conversion methods used by LanguageTag and conversions,
219 : not to be used by anything else.
220 : */
221 : class Conversion
222 : {
223 : private:
224 :
225 : friend class LanguageTag;
226 : friend class LanguageTagImpl;
227 :
228 : friend ::com::sun::star::lang::Locale MsLangId::getFallbackLocale(
229 : const ::com::sun::star::lang::Locale & rLocale );
230 :
231 : friend LanguageType MsLangId::convertUnxByteStringToLanguage(
232 : const OString& rString );
233 :
234 :
235 : /** Convert a Locale to a LanguageType with handling of an empty
236 : language name designating LANGUAGE_SYSTEM.
237 : */
238 : I18NLANGTAG_DLLPRIVATE static LanguageType convertLocaleToLanguage(
239 : const ::com::sun::star::lang::Locale & rLocale );
240 :
241 : /** Used by convertLocaleToLanguage(Locale) */
242 : I18NLANGTAG_DLLPRIVATE static LanguageType convertLocaleToLanguageImpl(
243 : const ::com::sun::star::lang::Locale & rLocale );
244 :
245 : /** Convert x-... privateuse, used by convertLocaleToLanguageImpl(Locale) */
246 : I18NLANGTAG_DLLPRIVATE static LanguageType convertPrivateUseToLanguage(
247 : const OUString& rPriv );
248 :
249 : /** Used by LanguageTag::canonicalize() */
250 : I18NLANGTAG_DLLPRIVATE static ::com::sun::star::lang::Locale getOverride(
251 : const ::com::sun::star::lang::Locale & rLocale );
252 :
253 : /** Used by convertLocaleToLanguage(Locale) */
254 : I18NLANGTAG_DLLPRIVATE static LanguageType convertIsoNamesToLanguage(
255 : const OUString& rLang, const OUString& rCountry );
256 :
257 :
258 : /** Used by convertUnxByteStringToLanguage(OString) */
259 : I18NLANGTAG_DLLPRIVATE static LanguageType convertIsoNamesToLanguage(
260 : const OString& rLang, const OString& rCountry );
261 :
262 :
263 : /** Used by lookupFallbackLocale(Locale) */
264 : I18NLANGTAG_DLLPRIVATE static com::sun::star::lang::Locale getLocale(
265 : const IsoLanguageCountryEntry * pEntry );
266 :
267 : /** Used by lookupFallbackLocale(Locale) */
268 : I18NLANGTAG_DLLPRIVATE static com::sun::star::lang::Locale getLocale(
269 : const IsoLanguageScriptCountryEntry * pEntry );
270 :
271 :
272 : /** Convert a LanguageType to a Locale.
273 :
274 : @param bResolveSystem
275 : If bResolveSystem==true, a LANGUAGE_SYSTEM is resolved.
276 : If bResolveSystem==false, a LANGUAGE_SYSTEM results in an
277 : empty Locale.
278 : */
279 : I18NLANGTAG_DLLPRIVATE static ::com::sun::star::lang::Locale convertLanguageToLocale(
280 : LanguageType nLang, bool bResolveSystem );
281 :
282 : /** Used by convertLanguageToLocale(LanguageType,bool) and
283 : getLocale(IsoLanguageCountryEntry*) and
284 : getLocale(IsoLanguageScriptCountryEntry)
285 :
286 : @param bIgnoreOverride
287 : If bIgnoreOverride==true, a matching entry is used even if
288 : mnOverride is set, for conversion to an even outdated tag.
289 : If bIgnoreOverride==false, a matching entry is skipped if
290 : mnOverride is set and instead the override is followed.
291 :
292 : @return rLocale set to mapped values, unchanged if no mapping was
293 : found. E.g. pass empty Locale to obtain empty SYSTEM locale
294 : for that case.
295 : */
296 : I18NLANGTAG_DLLPRIVATE static void convertLanguageToLocaleImpl(
297 : LanguageType nLang, ::com::sun::star::lang::Locale & rLocale, bool bIgnoreOverride );
298 :
299 :
300 : I18NLANGTAG_DLLPRIVATE static ::com::sun::star::lang::Locale lookupFallbackLocale(
301 : const ::com::sun::star::lang::Locale & rLocale );
302 : };
303 :
304 : private:
305 :
306 : static LanguageType nConfiguredSystemLanguage;
307 : static LanguageType nConfiguredSystemUILanguage;
308 :
309 : static LanguageType nConfiguredWesternFallback;
310 : static LanguageType nConfiguredAsianFallback;
311 : static LanguageType nConfiguredComplexFallback;
312 :
313 : static LanguageType getPlatformSystemLanguage();
314 : static LanguageType getPlatformSystemUILanguage();
315 :
316 : // Substitute LANGUAGE_SYSTEM for LANGUAGE_SYSTEM_DEFAULT and
317 : // LANGUAGE_PROCESS_OR_USER_DEFAULT, other values aren't touched.
318 : I18NLANGTAG_DLLPRIVATE static inline LanguageType simplifySystemLanguages( LanguageType nLang );
319 : };
320 :
321 :
322 : // static
323 1 : inline LanguageType MsLangId::getSystemLanguage()
324 : {
325 1 : return getPlatformSystemLanguage();
326 : }
327 :
328 :
329 : // static
330 0 : inline LanguageType MsLangId::getSystemUILanguage()
331 : {
332 0 : return getPlatformSystemUILanguage();
333 : }
334 :
335 : #endif // INCLUDED_I18NLANGTAG_MSLANGID_HXX
336 :
337 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|