void wxLocale::DoCommonInit()
{
- m_pszOldLocale = NULL;
+ // Store the current locale in order to be able to restore it in the dtor.
+ m_pszOldLocale = wxSetlocale(LC_ALL, NULL);
+ if ( m_pszOldLocale )
+ m_pszOldLocale = wxStrdup(m_pszOldLocale);
+
m_pOldLocale = wxSetLocale(this);
wxS("no locale to set in wxLocale::Init()") );
}
- const char *oldLocale = wxSetlocale(LC_ALL, szLocale);
- if ( oldLocale )
- m_pszOldLocale = wxStrdup(oldLocale);
- else
- m_pszOldLocale = NULL;
-
- if ( m_pszOldLocale == NULL )
+ if ( !wxSetlocale(LC_ALL, szLocale) )
{
wxLogError(_("locale '%s' cannot be set."), szLocale);
}
#endif // !WX_NO_LOCALE_SUPPORT
}
+namespace
+{
+
+// Small helper function: get the value of the given environment variable and
+// return true only if the variable was found and has non-empty value.
+inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value)
+{
+ return wxGetEnv(name, value) && !value->empty();
+}
+
+} // anonymous namespace
+
/*static*/ int wxLocale::GetSystemLanguage()
{
CreateLanguagesDB();
str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode)));
langFull += str.AsString();
#else
- if (!wxGetEnv(wxS("LC_ALL"), &langFull) &&
- !wxGetEnv(wxS("LC_MESSAGES"), &langFull) &&
- !wxGetEnv(wxS("LANG"), &langFull))
+ if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull) &&
+ !wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull) &&
+ !wxGetNonEmptyEnvVar(wxS("LANG"), &langFull))
{
// no language specified, treat it as English
return wxLANGUAGE_ENGLISH_US;
UINT codepage = ::GetACP();
encname.Printf(wxS("windows-%u"), codepage);
#elif defined(__WXMAC__)
- // default is just empty string, this resolves to the default system
- // encoding later
+ encname = wxCFStringRef::AsString(
+ CFStringGetNameOfEncoding(CFStringGetSystemEncoding())
+ );
#elif defined(__UNIX_LIKE__)
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
/* static */
wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
{
- wxUint32 lcid = LOCALE_USER_DEFAULT;
- if ( wxGetLocale() )
+ const wxLanguageInfo * const
+ info = wxGetLocale() ? GetLanguageInfo(wxGetLocale()->GetLanguage())
+ : NULL;
+ if ( !info )
{
- const wxLanguageInfo * const
- info = GetLanguageInfo(wxGetLocale()->GetLanguage());
- if ( info )
- lcid = info->GetLCID();
+ // wxSetLocale() hadn't been called yet of failed, hence CRT must be
+ // using "C" locale -- but check it to detect bugs that would happen if
+ // this were not the case.
+ wxASSERT_MSG( strcmp(setlocale(LC_ALL, NULL), "C") == 0,
+ wxS("You probably called setlocale() directly instead ")
+ wxS("of using wxLocale and now there is a ")
+ wxS("mismatch between C/C++ and Windows locale.\n")
+ wxS("Things are going to break, please only change ")
+ wxS("locale by creating wxLocale objects to avoid this!") );
+
+
+ // Return the hard coded values for C locale. This is really the right
+ // thing to do as there is no LCID we can use in the code below in this
+ // case, even LOCALE_INVARIANT is not quite the same as C locale (the
+ // only difference is that it uses %Y instead of %y in the date format
+ // but this difference is significant enough).
+ switch ( index )
+ {
+ case wxLOCALE_THOUSANDS_SEP:
+ return wxString();
+
+ case wxLOCALE_DECIMAL_POINT:
+ return ".";
+
+ case wxLOCALE_SHORT_DATE_FMT:
+ return "%m/%d/%y";
+
+ case wxLOCALE_LONG_DATE_FMT:
+ return "%A, %B %d, %Y";
+
+ case wxLOCALE_TIME_FMT:
+ return "%H:%M:%S";
+
+ case wxLOCALE_DATE_TIME_FMT:
+ return "%m/%d/%y %H:%M:%S";
+
+ default:
+ wxFAIL_MSG( "unknown wxLocaleInfo" );
+ }
}
+ const wxUint32 lcid = info->GetLCID();
+
wxString str;
wxChar buf[256];