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 .
24 #include <osl/process.h>
25 #include <rtl/locale.h>
26 #include <rtl/ustring.hxx>
29 #include <rtl/string.hxx>
32 #include <rtl/instance.hxx>
33 #include "i18nlangtag/languagetag.hxx"
34 #include "i18nlangtag/mslangid.hxx"
36 // =======================================================================
38 static LanguageType nImplSystemLanguage
= LANGUAGE_DONTKNOW
;
39 static LanguageType nImplSystemUILanguage
= LANGUAGE_DONTKNOW
;
41 // -----------------------------------------------------------------------
43 // Get locale of category LC_CTYPE of environment variables
44 static const sal_Char
* getLangFromEnvironment()
46 static const sal_Char
* pFallback
= "C";
47 const sal_Char
*pLang
= NULL
;
49 pLang
= getenv ( "LC_ALL" );
50 if (! pLang
|| pLang
[0] == 0)
51 pLang
= getenv ( "LC_CTYPE" );
52 if (! pLang
|| pLang
[0] == 0)
53 pLang
= getenv( "LANG" );
54 if (! pLang
|| pLang
[0] == 0)
60 // -----------------------------------------------------------------------
62 // Get locale of category LC_MESSAGES of environment variables
63 static const sal_Char
* getUILangFromEnvironment()
65 static const sal_Char
* pFallback
= "C";
66 const sal_Char
*pLang
= NULL
;
68 pLang
= getenv ( "LANGUAGE" ); // respect the GNU extension
69 if (! pLang
|| pLang
[0] == 0)
70 pLang
= getenv ( "LC_ALL" );
71 if (! pLang
|| pLang
[0] == 0)
72 pLang
= getenv ( "LC_MESSAGES" );
73 if (! pLang
|| pLang
[0] == 0)
74 pLang
= getenv( "LANG" );
75 if (! pLang
|| pLang
[0] == 0)
81 // -----------------------------------------------------------------------
83 typedef const sal_Char
* (*getLangFromEnv
)();
85 static void getPlatformSystemLanguageImpl( LanguageType
& rSystemLanguage
,
86 getLangFromEnv pGetLangFromEnv
)
88 /* get the language from the user environment */
89 LanguageType nLang
= rSystemLanguage
;
90 if ( nLang
== LANGUAGE_DONTKNOW
)
92 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex());
93 nLang
= rSystemLanguage
;
94 if ( nLang
== LANGUAGE_DONTKNOW
)
97 rtl_Locale
*procLocale
;
98 (void) pGetLangFromEnv
; /* unused */
100 if ( osl_getProcessLocale(&procLocale
) == osl_Process_E_None
)
102 nLang
= LanguageTag( *procLocale
).makeFallback().getLanguageType();
103 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
104 rSystemLanguage
= nLang
;
106 if ( rSystemLanguage
== LANGUAGE_DONTKNOW
)
107 fprintf( stderr
, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
111 OString
aUnxLang( (pGetLangFromEnv
)() );
112 nLang
= MsLangId::convertUnxByteStringToLanguage( aUnxLang
);
113 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
114 rSystemLanguage
= nLang
;
118 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
123 // -----------------------------------------------------------------------
125 LanguageType
MsLangId::getPlatformSystemLanguage()
127 getPlatformSystemLanguageImpl( nImplSystemLanguage
, &getLangFromEnvironment
);
128 return nImplSystemLanguage
;
131 // -----------------------------------------------------------------------
133 LanguageType
MsLangId::getPlatformSystemUILanguage()
135 getPlatformSystemLanguageImpl( nImplSystemUILanguage
, &getUILangFromEnvironment
);
136 return nImplSystemUILanguage
;
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */