1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include <stdlib.h> // for getenv()
35 #include <osl/process.h>
36 #include <rtl/locale.h>
37 #include <rtl/ustring.hxx>
40 #include <rtl/string.hxx>
43 #include <rtl/instance.hxx>
44 #include "i18npool/mslangid.hxx"
46 // =======================================================================
48 static LanguageType nImplSystemLanguage
= LANGUAGE_DONTKNOW
;
49 static LanguageType nImplSystemUILanguage
= LANGUAGE_DONTKNOW
;
51 // -----------------------------------------------------------------------
53 // Get locale of category LC_CTYPE of environment variables
54 static const sal_Char
* getLangFromEnvironment()
56 static const sal_Char
* pFallback
= "C";
57 const sal_Char
*pLang
= NULL
;
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)
70 // -----------------------------------------------------------------------
72 // Get locale of category LC_MESSAGES of environment variables
73 static const sal_Char
* getUILangFromEnvironment()
75 static const sal_Char
* pFallback
= "C";
76 const sal_Char
*pLang
= NULL
;
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)
91 // -----------------------------------------------------------------------
93 typedef const sal_Char
* (*getLangFromEnv
)();
95 static void getPlatformSystemLanguageImpl( LanguageType
& rSystemLanguage
,
96 getLangFromEnv pGetLangFromEnv
)
98 /* get the language from the user environment */
99 LanguageType nLang
= rSystemLanguage
;
100 if ( nLang
== LANGUAGE_DONTKNOW
)
102 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex());
103 nLang
= rSystemLanguage
;
104 if ( nLang
== LANGUAGE_DONTKNOW
)
107 rtl_Locale
*procLocale
;
108 (void) pGetLangFromEnv
; /* unused */
110 if ( osl_getProcessLocale(&procLocale
) == osl_Process_E_None
)
112 rtl::OUString
rLang( procLocale
->Language
);
113 rtl::OUString
rCountry( procLocale
->Country
);
115 nLang
= MsLangId::convertIsoNamesToLanguage( rLang
, rCountry
);
116 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
117 rSystemLanguage
= nLang
;
119 if ( rSystemLanguage
== LANGUAGE_DONTKNOW
)
120 fprintf( stderr
, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
124 rtl::OString
aUnxLang( (pGetLangFromEnv
)() );
125 nLang
= MsLangId::convertUnxByteStringToLanguage( aUnxLang
);
126 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
127 rSystemLanguage
= nLang
;
131 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
136 // -----------------------------------------------------------------------
138 LanguageType
MsLangId::getPlatformSystemLanguage()
140 getPlatformSystemLanguageImpl( nImplSystemLanguage
, &getLangFromEnvironment
);
141 return nImplSystemLanguage
;
144 // -----------------------------------------------------------------------
146 LanguageType
MsLangId::getPlatformSystemUILanguage()
148 getPlatformSystemLanguageImpl( nImplSystemUILanguage
, &getUILangFromEnvironment
);
149 return nImplSystemUILanguage
;
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */