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.
This commit is contained in:
Vadim Zeitlin
2021-08-30 00:17:26 +02:00
parent 592e1678ce
commit 45ffc40fc2
3 changed files with 66 additions and 45 deletions

View File

@@ -30,4 +30,7 @@ inline wxString ExtractNotLang(const wxString& langFull)
const char *wxSetlocaleTryAll(int c, const wxString& lc); 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_ #endif // _WX_UNIX_PRIVATE_UILOCALE_H_

View File

@@ -1720,34 +1720,10 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
#else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX #else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX
namespace
{
wxString GetDateFormatFromLangInfo(wxLocaleInfo index)
{
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
// array containing parameters for nl_langinfo() indexes by offset of index
// from wxLOCALE_SHORT_DATE_FMT wxString wxGetDateFormatOnly(const wxString& 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;
// this is not 100% precise but the idea is that a typical date/time format // 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 // 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 // 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; 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 */ /* static */
wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
@@ -1843,18 +1809,30 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
} }
break; 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_SHORT_DATE_FMT:
case wxLOCALE_LONG_DATE_FMT: case wxLOCALE_LONG_DATE_FMT:
case wxLOCALE_DATE_TIME_FMT: case wxLOCALE_DATE_TIME_FMT:
case wxLOCALE_TIME_FMT: case wxLOCALE_TIME_FMT:
if ( cat != wxLOCALE_CAT_DATE && cat != wxLOCALE_CAT_DEFAULT ) // 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
wxFAIL_MSG( "invalid wxLocaleCategory" ); // 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; break;
} #endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H
return GetDateFormatFromLangInfo(index);
default: default:
wxFAIL_MSG( "unknown wxLocaleInfo value" ); wxFAIL_MSG( "unknown wxLocaleInfo value" );

View File

@@ -27,6 +27,9 @@
#include "wx/intl.h" #include "wx/intl.h"
#include <locale.h> #include <locale.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
namespace namespace
{ {
@@ -153,6 +156,42 @@ wxUILocaleImplUnix::GetName() const
wxString wxString
wxUILocaleImplUnix::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) const 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 // 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. // 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 // temporarily change the locale here (maybe only if setlocale(NULL) result
// differs from the expected one). // differs from the expected one).
return wxLocale::GetInfo(index, cat); return wxLocale::GetInfo(index, cat);
#endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H
} }
/* static */ /* static */