In Unix ImplGetLocale, read env vars instead of calling setlocale

...to avoid the general problems with the latter (MT issues; changing global
state)

Change-Id: I21eb129b7e1422089b3449763f64f461371ffff1
This commit is contained in:
Stephan Bergmann
2017-03-23 10:48:54 +01:00
parent ed76d1d350
commit 77c2b0a33e

View File

@@ -154,21 +154,22 @@ namespace /* private */
#else #else
#include <rtl/ustrbuf.hxx> #include <rtl/ustrbuf.hxx>
#include <locale.h> #include <cstdlib>
#include <string.h> #include <cstring>
/* static OUString ImplGetLocale(char const * category)
* Note: setlocale is not at all thread safe, so is this code. It could
* especially interfere with the stuff VCL is doing, so make sure this
* is called from the main thread only.
*/
static OUString ImplGetLocale(int category)
{ {
const char *locale = setlocale(category, ""); const char *locale = std::getenv("LC_ALL");
if (locale == nullptr || *locale == '\0') {
locale = std::getenv(category);
if (locale == nullptr || *locale == '\0') {
locale = std::getenv("LANG");
}
}
// Return "en-US" for C locales // Return "en-US" for C locales
if( (locale == nullptr) || ( locale[0] == 'C' && locale[1] == '\0' ) ) if( (locale == nullptr) || *locale == '\0' || std::strcmp(locale, "C") == 0
|| std::strcmp(locale, "POSIX") == 0 )
return OUString( "en-US" ); return OUString( "en-US" );
@@ -227,7 +228,7 @@ OUString LocaleBackend::getLocale()
#elif defined (MACOSX) #elif defined (MACOSX)
return ImplGetLocale("AppleLocale"); return ImplGetLocale("AppleLocale");
#else #else
return ImplGetLocale(LC_CTYPE); return ImplGetLocale("LC_CTYPE");
#endif #endif
} }
@@ -239,7 +240,7 @@ OUString LocaleBackend::getUILocale()
#elif defined(MACOSX) #elif defined(MACOSX)
return ImplGetLocale("AppleLanguages"); return ImplGetLocale("AppleLanguages");
#else #else
return ImplGetLocale(LC_MESSAGES); return ImplGetLocale("LC_MESSAGES");
#endif #endif
} }