xref: /aoo41x/main/i18npool/source/isolang/inunx.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // no include "precompiled_i18npool.hxx" because this file is included in insys.cxx
30 
31 #include <stdlib.h>  // for getenv()
32 #include <stdio.h>
33 
34 #ifdef MACOSX
35 #include <osl/process.h>
36 #include <rtl/locale.h>
37 #include <rtl/ustring.hxx>
38 
39 #else   // MACOSX
40 #include <rtl/string.hxx>
41 
42 #endif  // MACOSX
43 #include <rtl/instance.hxx>
44 #include "i18npool/mslangid.hxx"
45 
46 // =======================================================================
47 
48 static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
49 static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
50 
51 // -----------------------------------------------------------------------
52 
53 // Get locale of category LC_CTYPE of environment variables
54 static const sal_Char* getLangFromEnvironment()
55 {
56     static const sal_Char* pFallback = "C";
57     const sal_Char *pLang = NULL;
58 
59     pLang = getenv ( "LC_ALL" );
60     if (! pLang || pLang[0] == 0)
61         pLang = getenv ( "LC_CTYPE" );
62     if (! pLang || pLang[0] == 0)
63         pLang = getenv( "LANG" );
64     if (! pLang || pLang[0] == 0)
65         pLang = pFallback;
66 
67     return pLang;
68 }
69 
70 // -----------------------------------------------------------------------
71 
72 // Get locale of category LC_MESSAGES of environment variables
73 static const sal_Char* getUILangFromEnvironment()
74 {
75     static const sal_Char* pFallback = "C";
76     const sal_Char *pLang = NULL;
77 
78     pLang = getenv ( "LANGUAGE" );      // respect the GNU extension
79     if (! pLang || pLang[0] == 0)
80         pLang = getenv ( "LC_ALL" );
81     if (! pLang || pLang[0] == 0)
82         pLang = getenv ( "LC_MESSAGES" );
83     if (! pLang || pLang[0] == 0)
84         pLang = getenv( "LANG" );
85     if (! pLang || pLang[0] == 0)
86         pLang = pFallback;
87 
88     return pLang;
89 }
90 
91 // -----------------------------------------------------------------------
92 
93 typedef const sal_Char * (*getLangFromEnv)();
94 
95 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
96         getLangFromEnv pGetLangFromEnv )
97 {
98     /* get the language from the user environment */
99     LanguageType nLang = rSystemLanguage;
100     if ( nLang == LANGUAGE_DONTKNOW )
101     {
102         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
103         nLang = rSystemLanguage;
104         if ( nLang == LANGUAGE_DONTKNOW )
105         {
106 #ifdef MACOSX
107             rtl_Locale    *procLocale;
108             (void) pGetLangFromEnv; /* unused */
109 
110             if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
111             {
112                 rtl::OUString     rLang( procLocale->Language );
113                 rtl::OUString     rCountry( procLocale->Country );
114 
115                 nLang = MsLangId::convertIsoNamesToLanguage( rLang, rCountry );
116                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
117                 rSystemLanguage = nLang;
118 #ifdef DEBUG
119                 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
120                     fprintf( stderr, "intnunx.cxx:  failed to convert osl_getProcessLocale() language to system language.\n" );
121 #endif
122             }
123 #else   /* MACOSX */
124             rtl::OString aUnxLang( (pGetLangFromEnv)() );
125             nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
126             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
127             rSystemLanguage = nLang;
128 #endif  /* MACOSX */
129         }
130         else {
131             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
132         }
133     }
134 }
135 
136 // -----------------------------------------------------------------------
137 
138 LanguageType MsLangId::getPlatformSystemLanguage()
139 {
140     getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
141     return nImplSystemLanguage;
142 }
143 
144 // -----------------------------------------------------------------------
145 
146 LanguageType MsLangId::getPlatformSystemUILanguage()
147 {
148     getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
149     return nImplSystemUILanguage;
150 }
151