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