xref: /aoo41x/main/i18npool/inc/i18npool/mslangid.hxx (revision f7bd9df4)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_I18NPOOL_MSLANGID_HXX
25 #define INCLUDED_I18NPOOL_MSLANGID_HXX
26 
27 #include <sal/config.h>
28 
29 #ifndef INCLUDED_I18NPOOL_DLLAPI_H
30 #include "i18npool/i18npooldllapi.h"
31 #endif
32 #include "i18npool/lang.h"
33 #include <com/sun/star/lang/Locale.hpp>
34 
35 
36 /** Methods related to Microsoft language IDs. For details about MS-LANGIDs
37     please see lang.h */
38 class I18NPOOL_DLLPUBLIC MsLangId
39 {
40 public:
41 
42     /// Create a LangID from a primary and a sublanguage.
makeLangID(LanguageType nSubLangId,LanguageType nPriLangId)43     static inline LanguageType makeLangID( LanguageType nSubLangId, LanguageType nPriLangId)
44     {
45         return (nSubLangId << 10) | nPriLangId;
46     }
47 
48     /// Get the primary language of a LangID.
getPrimaryLanguage(LanguageType nLangID)49     static inline LanguageType getPrimaryLanguage( LanguageType nLangID)
50     {
51         return nLangID & LANGUAGE_MASK_PRIMARY;
52     }
53 
54     /// Get the sublanguage of a LangID.
getSubLanguage(LanguageType nLangID)55     static inline LanguageType getSubLanguage( LanguageType nLangID)
56     {
57         return (nLangID & ~LANGUAGE_MASK_PRIMARY) >> 10;
58     }
59 
60     /** Language/locale of category LC_CTYPE (on Unix, else the system
61         language).
62         Evaluation order: LC_ALL, LC_CTYPE, LANG */
63     static LanguageType getSystemLanguage();
64 
65     /** Language/locale of category LC_MESSAGES (on Unix, else same as
66         GetSystemLanguage()).
67         Evaluation order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG */
68     static LanguageType getSystemUILanguage();
69 
70 
71     /** @short: A proper language/locale if the nLang parameter designates some
72                 special value.
73 
74         @descr: NOTE: The "system" values may be overridden by the
75                 application's configuration. If you need to access the system
76                 values use <method>getRealLanguageWithoutConfig()</method>
77                 instead.
78 
79         @returns
80             case LANGUAGE_PROCESS_OR_USER_DEFAULT : configured or system language
81             case LANGUAGE_SYSTEM_DEFAULT :          configured or system language
82             case LANGUAGE_SYSTEM :                  configured or system language
83             case LANGUAGE_NONE :                    configured or system UI language
84             case LANGUAGE_DONTKNOW :                LANGUAGE_ENGLISH_US
85             else: nLang
86 
87             In case the configured language is LANGUAGE_SYSTEM, which is also
88             the initial default, the system language is obtained. In case the
89             configured or resulting system language is LANGUAGE_DONTKNOW,
90             LANGUAGE_ENGLISH_US is returned instead.
91       */
92     static LanguageType getRealLanguage( LanguageType nLang );
93 
94 
95     /** @short: Convert a LanguageType to a Locale, resolving LANGUAGE_SYSTEM.
96 
97         @ATTENTION: A round trip convertLanguageToLocale(
98                 convertLocaleToLanguage( ...)) is NOT possible because this
99                 method substitutes LANGUAGE_SYSTEM and the like. If round-trip
100                 is desired, you MUST use convertLanguageToLocale( ..., false)
101                 instead.
102      */
103     static void convertLanguageToLocale( LanguageType nLang,
104             ::com::sun::star::lang::Locale & rLocale );
105 
106 
107     /** @short: Convert a LanguageType to a Locale with handling of
108                 getRealLanguage().
109 
110         @descr: If bResolveSystem==true don't use to convert a Language to a
111                 Locale for file storage because it substitutes LANGUAGE_SYSTEM
112                 and LANGUAGE_NONE and similar, use only at runtime! If
113                 bResolveSystem==false a LANGUAGE_SYSTEM results in an empty
114                 Locale.
115 
116         @ATTENTION: A round trip convertLanguageToLocale(
117                 convertLocaleToLanguage( ...)) using the default parameter is
118                 NOT possible because this method
119                 substitutes LANGUAGE_SYSTEM and the like. If round-trip is
120                 desired, you MUST use convertLanguageToLocale( ..., false)
121                 instead.
122       */
123     static ::com::sun::star::lang::Locale convertLanguageToLocale(
124             LanguageType nLang, bool bResolveSystem = true );
125 
126 
127     /** Convert a Locale to a LanguageType with handling of an empty language
128         name designating the SYSTEM language.
129       */
130     static LanguageType convertLocaleToLanguage( const ::com::sun::star::lang::Locale & rLocale );
131 
132 
133     /** Convert a LanguageType to a Locale, resolving LANGUAGE_SYSTEM, falling
134         back to a default locale if no exact match was found.
135      */
136     static ::com::sun::star::lang::Locale convertLanguageToLocaleWithFallback( LanguageType nLang );
137 
138 
139     /** Convert a Locale to a LanguageType with handling of an empty language
140         name designating the SYSTEM language, falling back to a default locale
141         if no exact match was found.
142       */
143     static LanguageType convertLocaleToLanguageWithFallback(
144             const ::com::sun::star::lang::Locale & rLocale );
145 
146 
147     /** Get fall-back Locale for Locale with handling of an empty language name
148         designating the SYSTEM language. Returns the same Locale if an exact
149         match was found.
150       */
151     static ::com::sun::star::lang::Locale getFallbackLocale(
152             const ::com::sun::star::lang::Locale & rLocale );
153 
154 
155     /** Get fall-back LanguageType for LanguageType, resolving LANGUAGE_SYSTEM.
156         Returns the same LanguageType if an exact match was found.
157       */
158     static LanguageType getFallbackLanguage( LanguageType nLang );
159 
160 
161     // -----------------------------
162     // - ConvertLanguageToIsoNames -
163     // -----------------------------
164 
165     static void convertLanguageToIsoNames( LanguageType nLang,
166             rtl::OUString& rLangStr, rtl::OUString& rCountry );
167     static void convertLanguageToIsoNames( LanguageType nLang,
168             rtl::OString& rLangStr, rtl::OString& rCountry );
169     static rtl::OUString convertLanguageToIsoString( LanguageType nLang,
170             sal_Unicode cSep = '-' );
171     static rtl::OString convertLanguageToIsoByteString( LanguageType nLang,
172             sal_Char cSep = '-' );
173 
174     // -----------------------------
175     // - ConvertIsoNamesToLanguage -
176     // -----------------------------
177 
178     static LanguageType convertIsoNamesToLanguage( const rtl::OUString& rLang,
179             const rtl::OUString& rCountry );
180     static LanguageType convertIsoNamesToLanguage( const rtl::OString& rLang,
181             const rtl::OString& rCountry );
182     static LanguageType convertIsoStringToLanguage(
183             const rtl::OUString& rString, sal_Unicode cSep = '-' );
184     static LanguageType convertIsoByteStringToLanguage(
185             const rtl::OString& rString, sal_Char cSep = '-' );
186     static LanguageType convertUnxByteStringToLanguage(
187             const rtl::OString& rString );
188 
189 
190     /** @short: A real language/locale if the nLang parameter designates some
191                 special value.
192 
193         @descr: NOTE: This is a raw interface to the system and does not take
194                 any application configuration into account. If that is wanted,
195                 which is most likely, use <method>getRealLanguage()</method>
196                 instead.
197 
198         @returns
199             case LANGUAGE_PROCESS_OR_USER_DEFAULT : getSystemLanguage()
200             case LANGUAGE_SYSTEM_DEFAULT :          getSystemLanguage()
201             case LANGUAGE_SYSTEM :                  getSystemLanguage()
202             case LANGUAGE_NONE :                    getSystemUILanguage()
203             case LANGUAGE_DONTKNOW :                LANGUAGE_ENGLISH_US
204             else: nLang
205 
206             In case getSystemLanguage() or getSystemUILanguage() returned
207             LANGUAGE_DONTKNOW, LANGUAGE_ENGLISH_US is returned instead.
208       */
209     static LanguageType getRealLanguageWithoutConfig( LanguageType nLang );
210 
211 
212     static LanguageType resolveSystemLanguageByScriptType( LanguageType nLang, sal_Int16 nType );
213 
214 
215     /** Whether locale has a Right-To-Left orientation. */
216     static bool isRightToLeft( LanguageType nLang );
217 
218 
219     /** Whether there are "forbidden characters at start or end of line" in
220         this locale. CJK locales.
221 
222         @see offapi/com/sun/star/i18n/ForbiddenCharacters.idl
223       */
224     static bool hasForbiddenCharacters( LanguageType nLang );
225 
226 
227     /** Whether locale needs input sequence checking. CTL locales. */
228     static bool needsSequenceChecking( LanguageType nLang );
229 
230 
231     /** Get ::com::sun::star::i18n::ScriptType of locale. */
232     static sal_Int16 getScriptType( LanguageType nLang );
233 
234 
235     /** Map an obsolete user defined LANGID (see lang.h
236         LANGUAGE_OBSOLETE_USER_...) to the new value defined by MS in the
237         meantime. */
238     static LanguageType getReplacementForObsoleteLanguage( LanguageType nLang );
239 
240 
241     /** @ATTENTION: these are _ONLY_ to be called by the application's
242         configuration! */
243     static void setConfiguredSystemLanguage( LanguageType nLang );
244     static void setConfiguredSystemUILanguage( LanguageType nLang );
245     static void setConfiguredWesternFallback( LanguageType nLang );
246     static void setConfiguredComplexFallback( LanguageType nLang );
247     static void setConfiguredAsianFallback( LanguageType nLang );
248 
249 // ---------------------------------------------------------------------------
250 
251     /** @internal - Access to fields of an element of the simple conversion table.
252         For resource compiler build environment usage only! */
253     struct IsoLangEntry
254     {
255         LanguageType  mnLang;
256         sal_Char      maLangStr[4];
257         sal_Char      maCountry[3];
258     };
259 
260     /** @internal - Return a pointer to the IsoLangEntry of the underlying table,
261         matching the offset passed by nIndex. Only meaningful for the resource
262         compiler to build a list of known languages.
263 
264         @returns address of IsoLangEntry, or NULL pointer if nIndex exceeds the
265         table elements' count.
266      */
267     static const IsoLangEntry* getIsoLangEntry( size_t nIndex );
268 
269 // ---------------------------------------------------------------------------
270 
271 private:
272 
273     static LanguageType         nConfiguredSystemLanguage;
274     static LanguageType         nConfiguredSystemUILanguage;
275 
276     static LanguageType         nConfiguredWesternFallback;
277     static LanguageType         nConfiguredAsianFallback;
278     static LanguageType         nConfiguredComplexFallback;
279 
280     static LanguageType getPlatformSystemLanguage();
281     static LanguageType getPlatformSystemUILanguage();
282 
283     // Substitute LANGUAGE_SYSTEM for LANGUAGE_SYSTEM_DEFAULT and
284     // LANGUAGE_PROCESS_OR_USER_DEFAULT, other values aren't touched.
285     I18NPOOL_DLLPRIVATE static inline LanguageType simplifySystemLanguages( LanguageType nLang );
286 
287     // Several locale lookups with fall-back
288     I18NPOOL_DLLPRIVATE static LanguageType lookupFallbackLanguage( LanguageType nLang );
289     I18NPOOL_DLLPRIVATE static LanguageType lookupFallbackLanguage(
290             const ::com::sun::star::lang::Locale & rLocale );
291     I18NPOOL_DLLPRIVATE static ::com::sun::star::lang::Locale lookupFallbackLocale( LanguageType nLang );
292     I18NPOOL_DLLPRIVATE static ::com::sun::star::lang::Locale lookupFallbackLocale(
293             const ::com::sun::star::lang::Locale & rLocale );
294 };
295 
296 
297 // static
getSystemLanguage()298 inline LanguageType MsLangId::getSystemLanguage()
299 {
300     return getPlatformSystemLanguage();
301 }
302 
303 
304 // static
getSystemUILanguage()305 inline LanguageType MsLangId::getSystemUILanguage()
306 {
307     return getPlatformSystemUILanguage();
308 }
309 
310 #endif // INCLUDED_I18NPOOL_MSLANGID_HXX
311