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