From 86d62b51c011577ae57be054db8b1213725440f4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 11 Feb 2008 23:40:03 +0000 Subject: [PATCH] add wxLanguageInfo::GetLocaleName(), this simplifies the current code and will be used with wxXLocale git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51669 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/intl.h | 10 ++++++ src/common/intl.cpp | 81 ++++++++++++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/include/wx/intl.h b/include/wx/intl.h index 57a3ec0638..8b02196d99 100644 --- a/include/wx/intl.h +++ b/include/wx/intl.h @@ -332,8 +332,18 @@ struct WXDLLIMPEXP_BASE wxLanguageInfo // return the LCID corresponding to this language wxUint32 GetLCID() const; #endif // __WXMSW__ + + // return the locale name corresponding to this language usable with + // setlocale() on the current system + wxString GetLocaleName() const; }; +// for Unix systems GetLocaleName() is trivial so implement it inline here, for +// MSW it's implemented in intl.cpp +#ifndef __WXMSW__ +inline wxString wxLanguageInfo::GetLocaleName() const { return CanonicalName; } +#endif // !__WXMSW__ + // ---------------------------------------------------------------------------- // wxLocaleCategory: the category of locale settings // ---------------------------------------------------------------------------- diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 9294c25045..2a4f5e16e7 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -1023,6 +1023,38 @@ wxUint32 wxLanguageInfo::GetLCID() const return MAKELCID(MAKELANGID(WinLang, WinSublang), SORT_DEFAULT); } +wxString wxLanguageInfo::GetLocaleName() const +{ + wxString locale; + + const LCID lcid = GetLCID(); + + wxChar buffer[256]; + buffer[0] = _T('\0'); + if ( !::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, WXSIZEOF(buffer)) ) + { + wxLogLastError(_T("GetLocaleInfo(LOCALE_SENGLANGUAGE)")); + return locale; + } + + locale << buffer; + if ( ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, + buffer, WXSIZEOF(buffer)) > 0 ) + { + locale << _T('_') << buffer; + } + + if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, + buffer, WXSIZEOF(buffer)) > 0 ) + { + if ( buffer[0] != _T('0') || buffer[1] != _T('\0') ) + locale << _T('.') << buffer; + //else: this locale doesn't use ANSI code page + } + + return locale; +} + #endif // __WXMSW__ // ---------------------------------------------------------------------------- @@ -1806,7 +1838,7 @@ bool wxLocale::Init(int language, int flags) // Unicode. Therefore wxSetlocale call failed, but we don't want // to report it as an error -- so that at least message catalogs // can be used. Watch for code marked with - // #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS bellow. + // #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS below. #define SETLOCALE_FAILS_ON_UNICODE_LANGS #endif @@ -1820,49 +1852,38 @@ bool wxLocale::Init(int language, int flags) } else { - int codepage - #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS - = -1 - #endif - ; const wxUint32 lcid = info->GetLCID(); // FIXME #ifndef __WXWINCE__ - SetThreadLocale(lcid); -#endif - // NB: we must translate LCID to CRT's setlocale string ourselves, - // because SetThreadLocale does not modify change the - // interpretation of setlocale(LC_ALL, "") call: - wxChar buffer[256]; - buffer[0] = wxS('\0'); - GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, 256); - locale << buffer; - if (GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, buffer, 256) > 0) - locale << wxS("_") << buffer; - if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, buffer, 256) > 0) - { - codepage = wxAtoi(buffer); - if (codepage != 0) - locale << wxS(".") << buffer; - } - if (locale.empty()) + // change locale used by Windows functions + ::SetThreadLocale(lcid); +#endif + // and also call setlocale() to change locale used by the CRT + locale = info->GetLocaleName(); + if ( locale.empty() ) { - wxLogLastError(wxS("SetThreadLocale")); ret = false; } - else + else // have a valid locale { - // FIXME + // FIXME #ifndef __WXWINCE__ retloc = wxSetlocale(LC_ALL, locale); #endif #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS - if (codepage == 0 && retloc == NULL) + if ( !retloc ) { - retloc = "C"; + // GetLocaleName() returns a string without period only if + // there is no associated ANSI code page + if ( locale.find(_T('.')) == wxString::npos ) + { + retloc = "C"; + } + //else: locale has code page information and hence this is + // a real error } -#endif +#endif // SETLOCALE_FAILS_ON_UNICODE_LANGS } } } -- 2.45.2