xref: /aoo4110/main/i18npool/source/isolang/inunx.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // no include "precompiled_i18npool.hxx" because this file is included in insys.cxx
26 
27 #include <stdlib.h>  // for getenv()
28 #include <stdio.h>
29 
30 #ifdef MACOSX
31 #include <osl/process.h>
32 #include <rtl/locale.h>
33 #include <rtl/ustring.hxx>
34 
35 #else   // MACOSX
36 #include <rtl/string.hxx>
37 
38 #endif  // MACOSX
39 #include <rtl/instance.hxx>
40 #include "i18npool/mslangid.hxx"
41 
42 // =======================================================================
43 
44 static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
45 static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
46 
47 // -----------------------------------------------------------------------
48 
49 // Get locale of category LC_CTYPE of environment variables
getLangFromEnvironment()50 static const sal_Char* getLangFromEnvironment()
51 {
52     static const sal_Char* pFallback = "C";
53     const sal_Char *pLang = NULL;
54 
55     pLang = getenv ( "LC_ALL" );
56     if (! pLang || pLang[0] == 0)
57         pLang = getenv ( "LC_CTYPE" );
58     if (! pLang || pLang[0] == 0)
59         pLang = getenv( "LANG" );
60     if (! pLang || pLang[0] == 0)
61         pLang = pFallback;
62 
63     return pLang;
64 }
65 
66 // -----------------------------------------------------------------------
67 
68 // Get locale of category LC_MESSAGES of environment variables
getUILangFromEnvironment()69 static const sal_Char* getUILangFromEnvironment()
70 {
71     static const sal_Char* pFallback = "C";
72     const sal_Char *pLang = NULL;
73 
74     pLang = getenv ( "LANGUAGE" );      // respect the GNU extension
75     if (! pLang || pLang[0] == 0)
76         pLang = getenv ( "LC_ALL" );
77     if (! pLang || pLang[0] == 0)
78         pLang = getenv ( "LC_MESSAGES" );
79     if (! pLang || pLang[0] == 0)
80         pLang = getenv( "LANG" );
81     if (! pLang || pLang[0] == 0)
82         pLang = pFallback;
83 
84     return pLang;
85 }
86 
87 // -----------------------------------------------------------------------
88 
89 typedef const sal_Char * (*getLangFromEnv)();
90 
getPlatformSystemLanguageImpl(LanguageType & rSystemLanguage,getLangFromEnv pGetLangFromEnv)91 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
92         getLangFromEnv pGetLangFromEnv )
93 {
94     /* get the language from the user environment */
95     LanguageType nLang = rSystemLanguage;
96     if ( nLang == LANGUAGE_DONTKNOW )
97     {
98         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
99         nLang = rSystemLanguage;
100         if ( nLang == LANGUAGE_DONTKNOW )
101         {
102 #ifdef MACOSX
103             rtl_Locale    *procLocale;
104             (void) pGetLangFromEnv; /* unused */
105 
106             if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
107             {
108                 rtl::OUString     rLang( procLocale->Language );
109                 rtl::OUString     rCountry( procLocale->Country );
110 
111                 nLang = MsLangId::convertIsoNamesToLanguage( rLang, rCountry );
112                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
113                 rSystemLanguage = nLang;
114 #ifdef DEBUG
115                 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
116                     fprintf( stderr, "intnunx.cxx:  failed to convert osl_getProcessLocale() language to system language.\n" );
117 #endif
118             }
119 #else   /* MACOSX */
120             rtl::OString aUnxLang( (pGetLangFromEnv)() );
121             nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
122             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
123             rSystemLanguage = nLang;
124 #endif  /* MACOSX */
125         }
126         else {
127             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
128         }
129     }
130 }
131 
132 // -----------------------------------------------------------------------
133 
getPlatformSystemLanguage()134 LanguageType MsLangId::getPlatformSystemLanguage()
135 {
136     getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
137     return nImplSystemLanguage;
138 }
139 
140 // -----------------------------------------------------------------------
141 
getPlatformSystemUILanguage()142 LanguageType MsLangId::getPlatformSystemUILanguage()
143 {
144     getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
145     return nImplSystemUILanguage;
146 }
147