From 45ffc40fc2c0cbb7cf722f0bf8538dbc1e5b9245 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 30 Aug 2021 00:17:26 +0200 Subject: [PATCH] Use nl_langinfo() in wxUILocaleImplUnix::GetInfo() This function can be used for all GetInfo() items, so using it is simpler than the code in the Unix version of wxLocale::GetInfo() which uses either it or localeconv(), and there should be no real drawbacks to using it nowadays as it should be available everywhere. No real changes yet. --- include/wx/unix/private/uilocale.h | 3 ++ src/common/intl.cpp | 68 ++++++++++-------------------- src/unix/uilocale.cpp | 40 ++++++++++++++++++ 3 files changed, 66 insertions(+), 45 deletions(-) diff --git a/include/wx/unix/private/uilocale.h b/include/wx/unix/private/uilocale.h index 7a882810d0..d8cacebb79 100644 --- a/include/wx/unix/private/uilocale.h +++ b/include/wx/unix/private/uilocale.h @@ -30,4 +30,7 @@ inline wxString ExtractNotLang(const wxString& langFull) const char *wxSetlocaleTryAll(int c, const wxString& lc); +// Extract date format from D_T_FMT value. +wxString wxGetDateFormatOnly(const wxString& fmt); + #endif // _WX_UNIX_PRIVATE_UILOCALE_H_ diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 5f2a74788f..65efd8dff0 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -1720,34 +1720,10 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) #else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX -namespace -{ - -wxString GetDateFormatFromLangInfo(wxLocaleInfo index) -{ #ifdef HAVE_LANGINFO_H - // array containing parameters for nl_langinfo() indexes by offset of index - // from wxLOCALE_SHORT_DATE_FMT - static const nl_item items[] = - { - D_FMT, D_T_FMT, D_T_FMT, T_FMT, - }; - - const int nlidx = index - wxLOCALE_SHORT_DATE_FMT; - if ( nlidx < 0 || nlidx >= (int)WXSIZEOF(items) ) - { - wxFAIL_MSG( "logic error in GetInfo() code" ); - return wxString(); - } - - const wxString fmt(nl_langinfo(items[nlidx])); - - // just return the format returned by nl_langinfo() except for long date - // format which we need to recover from date/time format ourselves (but not - // if we failed completely) - if ( fmt.empty() || index != wxLOCALE_LONG_DATE_FMT ) - return fmt; +wxString wxGetDateFormatOnly(const wxString& fmt) +{ // this is not 100% precise but the idea is that a typical date/time format // under POSIX systems is a combination of a long date format with time one // so we should be able to get just the long date format by removing all @@ -1789,19 +1765,9 @@ wxString GetDateFormatFromLangInfo(wxLocaleInfo index) } return fmtDateOnly; -#else // !HAVE_LANGINFO_H - wxUnusedVar(index); - - // no fallback, let the application deal with unavailability of - // nl_langinfo() itself as there is no good way for us to do it (well, we - // could try to reverse engineer the format from strftime() output but this - // looks like too much trouble considering the relatively small number of - // systems without nl_langinfo() still in use) - return wxString(); -#endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H } -} // anonymous namespace +#endif // HAVE_LANGINFO_H/ /* static */ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) @@ -1843,18 +1809,30 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) } break; +#ifdef HAVE_LANGINFO_H + case wxLOCALE_SHORT_DATE_FMT: + return nl_langinfo(D_FMT); + + case wxLOCALE_DATE_TIME_FMT: + return nl_langinfo(D_T_FMT); + + case wxLOCALE_TIME_FMT: + return nl_langinfo(T_FMT); + + case wxLOCALE_LONG_DATE_FMT: + return wxGetDateFormatOnly(nl_langinfo(D_T_FMT)); +#else // !HAVE_LANGINFO_H case wxLOCALE_SHORT_DATE_FMT: case wxLOCALE_LONG_DATE_FMT: case wxLOCALE_DATE_TIME_FMT: case wxLOCALE_TIME_FMT: - if ( cat != wxLOCALE_CAT_DATE && cat != wxLOCALE_CAT_DEFAULT ) - { - wxFAIL_MSG( "invalid wxLocaleCategory" ); - break; - } - - return GetDateFormatFromLangInfo(index); - + // no fallback, let the application deal with unavailability of + // nl_langinfo() itself as there is no good way for us to do it (well, we + // could try to reverse engineer the format from strftime() output but this + // looks like too much trouble considering the relatively small number of + // systems without nl_langinfo() still in use) + break; +#endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H default: wxFAIL_MSG( "unknown wxLocaleInfo value" ); diff --git a/src/unix/uilocale.cpp b/src/unix/uilocale.cpp index 36075d789e..405f53df89 100644 --- a/src/unix/uilocale.cpp +++ b/src/unix/uilocale.cpp @@ -27,6 +27,9 @@ #include "wx/intl.h" #include +#ifdef HAVE_LANGINFO_H + #include +#endif namespace { @@ -153,6 +156,42 @@ wxUILocaleImplUnix::GetName() const wxString wxUILocaleImplUnix::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) const { +#ifdef HAVE_LANGINFO_H + switch ( index ) + { + case wxLOCALE_THOUSANDS_SEP: +#ifdef MON_THOUSANDS_SEP + if ( cat == wxLOCALE_CAT_MONEY ) + return nl_langinfo(MON_THOUSANDS_SEP); +#endif + return nl_langinfo(THOUSEP); + + case wxLOCALE_DECIMAL_POINT: +#ifdef MON_DECIMAL_POINT + if ( cat == wxLOCALE_CAT_MONEY ) + return nl_langinfo(MON_DECIMAL_POINT); +#endif + + return nl_langinfo(RADIXCHAR); + + case wxLOCALE_SHORT_DATE_FMT: + return nl_langinfo(D_FMT); + + case wxLOCALE_DATE_TIME_FMT: + return nl_langinfo(D_T_FMT); + + case wxLOCALE_TIME_FMT: + return nl_langinfo(T_FMT); + + case wxLOCALE_LONG_DATE_FMT: + return wxGetDateFormatOnly(nl_langinfo(D_T_FMT)); + + default: + wxFAIL_MSG( "unknown wxLocaleInfo value" ); + } + + return wxString(); +#else // !HAVE_LANGINFO_H // Currently we rely on the user code not calling setlocale() itself, so // that the current locale is still the same as was set in the ctor. // @@ -160,6 +199,7 @@ wxUILocaleImplUnix::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) const // temporarily change the locale here (maybe only if setlocale(NULL) result // differs from the expected one). return wxLocale::GetInfo(index, cat); +#endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H } /* static */