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