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>
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 "i18nlangtag/languagetag.hxx"
34 : #include "i18nlangtag/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 165 : static const sal_Char* getLangFromEnvironment()
45 : {
46 : static const sal_Char* pFallback = "C";
47 165 : const sal_Char *pLang = NULL;
48 :
49 165 : pLang = getenv ( "LC_ALL" );
50 165 : if (! pLang || pLang[0] == 0)
51 165 : pLang = getenv ( "LC_CTYPE" );
52 165 : if (! pLang || pLang[0] == 0)
53 165 : pLang = getenv( "LANG" );
54 165 : if (! pLang || pLang[0] == 0)
55 0 : pLang = pFallback;
56 :
57 165 : return pLang;
58 : }
59 :
60 :
61 :
62 : // Get locale of category LC_MESSAGES of environment variables
63 166 : static const sal_Char* getUILangFromEnvironment()
64 : {
65 : static const sal_Char* pFallback = "C";
66 166 : const sal_Char *pLang = NULL;
67 :
68 166 : pLang = getenv ( "LANGUAGE" ); // respect the GNU extension
69 166 : if (! pLang || pLang[0] == 0)
70 166 : pLang = getenv ( "LC_ALL" );
71 166 : if (! pLang || pLang[0] == 0)
72 166 : pLang = getenv ( "LC_MESSAGES" );
73 166 : if (! pLang || pLang[0] == 0)
74 166 : pLang = getenv( "LANG" );
75 166 : if (! pLang || pLang[0] == 0)
76 0 : pLang = pFallback;
77 :
78 166 : return pLang;
79 : }
80 :
81 :
82 :
83 : typedef const sal_Char * (*getLangFromEnv)();
84 :
85 66555 : static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
86 : getLangFromEnv pGetLangFromEnv )
87 : {
88 : /* get the language from the user environment */
89 66555 : LanguageType nLang = rSystemLanguage;
90 66555 : if ( nLang == LANGUAGE_DONTKNOW )
91 : {
92 331 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
93 331 : nLang = rSystemLanguage;
94 331 : 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 : nLang = LanguageTag( *procLocale ).makeFallback().getLanguageType();
103 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
104 : rSystemLanguage = nLang;
105 : #ifdef DEBUG
106 : if ( rSystemLanguage == LANGUAGE_DONTKNOW )
107 : fprintf( stderr, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
108 : #endif
109 : }
110 : #else /* MACOSX */
111 331 : OString aUnxLang( (pGetLangFromEnv)() );
112 331 : nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
113 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
114 331 : rSystemLanguage = nLang;
115 : #endif /* MACOSX */
116 : }
117 : else {
118 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
119 331 : }
120 : }
121 66555 : }
122 :
123 :
124 :
125 66341 : LanguageType MsLangId::getPlatformSystemLanguage()
126 : {
127 66341 : getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
128 66341 : return nImplSystemLanguage;
129 : }
130 :
131 :
132 :
133 214 : LanguageType MsLangId::getPlatformSystemUILanguage()
134 : {
135 214 : getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
136 214 : return nImplSystemUILanguage;
137 : }
138 :
139 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|