tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / i18nlangtag / source / isolang / inunx.cxx
blob297c2c0c188b86d71ac84f666fa09d675064e4dd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <stdlib.h>
22 #ifdef MACOSX
23 #include <osl/process.h>
24 #include <rtl/locale.h>
25 #include <rtl/ustring.hxx>
26 #include <i18nlangtag/languagetag.hxx>
28 #else // MACOSX
29 #include <rtl/string.hxx>
30 #endif // MACOSX
32 #include <osl/mutex.hxx>
33 #include <osl/doublecheckedlocking.h>
34 #include <i18nlangtag/mslangid.hxx>
37 static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
38 static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
41 // Get locale of category LC_CTYPE of environment variables
42 static const char* getLangFromEnvironment( bool& rbColonList )
44 static const char* const pFallback = "C";
45 const char *pLang = nullptr;
47 rbColonList = false;
48 pLang = getenv ( "LC_ALL" );
49 if (! pLang || pLang[0] == 0)
50 pLang = getenv ( "LC_CTYPE" );
51 if (! pLang || pLang[0] == 0)
52 pLang = getenv( "LANG" );
53 if (! pLang || pLang[0] == 0)
54 pLang = pFallback;
56 return pLang;
60 // Get locale of category LC_MESSAGES of environment variables
61 static const char* getUILangFromEnvironment( bool& rbColonList )
63 static const char* const pFallback = "C";
64 const char *pLang = nullptr;
66 rbColonList = true;
67 pLang = getenv ( "LANGUAGE" ); // respect the GNU extension
68 if (! pLang || pLang[0] == 0)
70 rbColonList = false;
71 pLang = getenv ( "LC_ALL" );
73 if (! pLang || pLang[0] == 0)
74 pLang = getenv ( "LC_MESSAGES" );
75 if (! pLang || pLang[0] == 0)
76 pLang = getenv( "LANG" );
77 if (! pLang || pLang[0] == 0)
78 pLang = pFallback;
80 return pLang;
84 typedef const char * (*getLangFromEnv)( bool& rbColonList );
86 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
87 getLangFromEnv pGetLangFromEnv )
89 /* get the language from the user environment */
90 LanguageType nLang = rSystemLanguage;
91 if ( nLang != LANGUAGE_DONTKNOW )
92 return;
94 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
95 nLang = rSystemLanguage;
96 if ( nLang == LANGUAGE_DONTKNOW )
98 #ifdef MACOSX
99 rtl_Locale *procLocale;
100 (void) pGetLangFromEnv; /* unused */
102 if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
104 nLang = LanguageTag( *procLocale ).makeFallback().getLanguageType();
105 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
106 rSystemLanguage = nLang;
107 #if OSL_DEBUG_LEVEL >= 2
108 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
109 fprintf( stderr, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
110 #endif
112 #else /* MACOSX */
113 bool bColonList = false;
114 OString aUnxLang( pGetLangFromEnv( bColonList));
115 if (bColonList)
117 // Only a very simple "take first". If empty try second or keep empty.
118 sal_Int32 n = aUnxLang.indexOf(':');
119 if (n >= 0)
121 sal_Int32 s = 0;
122 if (n == 0 && aUnxLang.getLength() > 1)
124 n = aUnxLang.indexOf(':', 1);
125 if (n < 0)
126 n = aUnxLang.getLength();
127 if (n < 2)
128 s = n = 0;
129 else
131 s = 1;
132 --n;
135 aUnxLang = aUnxLang.copy(s,n);
138 nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
139 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
140 rSystemLanguage = nLang;
141 #endif /* MACOSX */
143 else {
144 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
149 LanguageType MsLangId::getPlatformSystemLanguage()
151 getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
152 return nImplSystemLanguage;
156 LanguageType MsLangId::getPlatformSystemUILanguage()
158 getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
159 return nImplSystemUILanguage;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */