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 : #include <stdlib.h> // for getenv()
21 : #include <stdio.h>
22 :
23 : #ifdef MACOSX
24 : #include <osl/process.h>
25 : #include <rtl/locale.h>
26 : #include <rtl/ustring.hxx>
27 :
28 : #else // MACOSX
29 : #include <rtl/string.hxx>
30 :
31 : #endif // MACOSX
32 : #include <rtl/instance.hxx>
33 : #include "i18npool/languagetag.hxx"
34 : #include "i18npool/mslangid.hxx"
35 :
36 : // =======================================================================
37 :
38 : static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
39 : static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
40 :
41 : // -----------------------------------------------------------------------
42 :
43 : // Get locale of category LC_CTYPE of environment variables
44 33 : static const sal_Char* getLangFromEnvironment()
45 : {
46 : static const sal_Char* pFallback = "C";
47 33 : const sal_Char *pLang = NULL;
48 :
49 33 : pLang = getenv ( "LC_ALL" );
50 33 : if (! pLang || pLang[0] == 0)
51 33 : pLang = getenv ( "LC_CTYPE" );
52 33 : if (! pLang || pLang[0] == 0)
53 33 : pLang = getenv( "LANG" );
54 33 : if (! pLang || pLang[0] == 0)
55 0 : pLang = pFallback;
56 :
57 33 : return pLang;
58 : }
59 :
60 : // -----------------------------------------------------------------------
61 :
62 : // Get locale of category LC_MESSAGES of environment variables
63 32 : static const sal_Char* getUILangFromEnvironment()
64 : {
65 : static const sal_Char* pFallback = "C";
66 32 : const sal_Char *pLang = NULL;
67 :
68 32 : pLang = getenv ( "LANGUAGE" ); // respect the GNU extension
69 32 : if (! pLang || pLang[0] == 0)
70 32 : pLang = getenv ( "LC_ALL" );
71 32 : if (! pLang || pLang[0] == 0)
72 32 : pLang = getenv ( "LC_MESSAGES" );
73 32 : if (! pLang || pLang[0] == 0)
74 32 : pLang = getenv( "LANG" );
75 32 : if (! pLang || pLang[0] == 0)
76 0 : pLang = pFallback;
77 :
78 32 : return pLang;
79 : }
80 :
81 : // -----------------------------------------------------------------------
82 :
83 : typedef const sal_Char * (*getLangFromEnv)();
84 :
85 263 : static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
86 : getLangFromEnv pGetLangFromEnv )
87 : {
88 : /* get the language from the user environment */
89 263 : LanguageType nLang = rSystemLanguage;
90 263 : if ( nLang == LANGUAGE_DONTKNOW )
91 : {
92 65 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
93 65 : nLang = rSystemLanguage;
94 65 : if ( nLang == LANGUAGE_DONTKNOW )
95 : {
96 : #ifdef MACOSX
97 : rtl_Locale *procLocale;
98 : (void) pGetLangFromEnv; /* unused */
99 :
100 : if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
101 : {
102 : rtl::OUString rLang( procLocale->Language );
103 : rtl::OUString rCountry( procLocale->Country );
104 :
105 : nLang = LanguageTag( rLang, rCountry ).getLanguageType();
106 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
107 : rSystemLanguage = nLang;
108 : #ifdef DEBUG
109 : if ( rSystemLanguage == LANGUAGE_DONTKNOW )
110 : fprintf( stderr, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
111 : #endif
112 : }
113 : #else /* MACOSX */
114 65 : rtl::OString aUnxLang( (pGetLangFromEnv)() );
115 65 : nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
116 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
117 65 : rSystemLanguage = nLang;
118 : #endif /* MACOSX */
119 : }
120 : else {
121 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
122 65 : }
123 : }
124 263 : }
125 :
126 : // -----------------------------------------------------------------------
127 :
128 199 : LanguageType MsLangId::getPlatformSystemLanguage()
129 : {
130 199 : getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
131 199 : return nImplSystemLanguage;
132 : }
133 :
134 : // -----------------------------------------------------------------------
135 :
136 64 : LanguageType MsLangId::getPlatformSystemUILanguage()
137 : {
138 64 : getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
139 64 : return nImplSystemUILanguage;
140 : }
141 :
142 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|