Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18nlangtag / source / isolang / inunx.cxx
blobce9ea5ee83e9e631d70a0af36f0deffd5beab69e
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 sal_Char* getLangFromEnvironment()
44 static const sal_Char* const pFallback = "C";
45 const sal_Char *pLang = nullptr;
47 pLang = getenv ( "LC_ALL" );
48 if (! pLang || pLang[0] == 0)
49 pLang = getenv ( "LC_CTYPE" );
50 if (! pLang || pLang[0] == 0)
51 pLang = getenv( "LANG" );
52 if (! pLang || pLang[0] == 0)
53 pLang = pFallback;
55 return pLang;
59 // Get locale of category LC_MESSAGES of environment variables
60 static const sal_Char* getUILangFromEnvironment()
62 static const sal_Char* const pFallback = "C";
63 const sal_Char *pLang = nullptr;
65 pLang = getenv ( "LANGUAGE" ); // respect the GNU extension
66 if (! pLang || pLang[0] == 0)
67 pLang = getenv ( "LC_ALL" );
68 if (! pLang || pLang[0] == 0)
69 pLang = getenv ( "LC_MESSAGES" );
70 if (! pLang || pLang[0] == 0)
71 pLang = getenv( "LANG" );
72 if (! pLang || pLang[0] == 0)
73 pLang = pFallback;
75 return pLang;
79 typedef const sal_Char * (*getLangFromEnv)();
81 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
82 getLangFromEnv pGetLangFromEnv )
84 /* get the language from the user environment */
85 LanguageType nLang = rSystemLanguage;
86 if ( nLang == LANGUAGE_DONTKNOW )
88 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
89 nLang = rSystemLanguage;
90 if ( nLang == LANGUAGE_DONTKNOW )
92 #ifdef MACOSX
93 rtl_Locale *procLocale;
94 (void) pGetLangFromEnv; /* unused */
96 if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
98 nLang = LanguageTag( *procLocale ).makeFallback().getLanguageType();
99 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
100 rSystemLanguage = nLang;
101 #ifdef DEBUG
102 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
103 fprintf( stderr, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
104 #endif
106 #else /* MACOSX */
107 OString aUnxLang( pGetLangFromEnv() );
108 nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
109 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
110 rSystemLanguage = nLang;
111 #endif /* MACOSX */
113 else {
114 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
120 LanguageType MsLangId::getPlatformSystemLanguage()
122 getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
123 return nImplSystemLanguage;
127 LanguageType MsLangId::getPlatformSystemUILanguage()
129 getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
130 return nImplSystemUILanguage;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */