Use wxLocaleIdent in Unix wxLocale and wxUILocale code

No real changes, just prefer using wxLocaleIdent to strings in a few
places.
This commit is contained in:
Vadim Zeitlin
2021-09-05 00:42:09 +02:00
parent 6f4dd01b5b
commit c7ad052c71
3 changed files with 54 additions and 37 deletions

View File

@@ -28,7 +28,7 @@ inline wxString ExtractNotLang(const wxString& langFull)
return wxString(); return wxString();
} }
const char *wxSetlocaleTryAll(int c, const wxString& lc); const char *wxSetlocaleTryAll(int c, const wxLocaleIdent& lc);
// Extract date format from D_T_FMT value. // Extract date format from D_T_FMT value.
wxString wxGetDateFormatOnly(const wxString& fmt); wxString wxGetDateFormatOnly(const wxString& fmt);

View File

@@ -1014,7 +1014,21 @@ bool wxLocale::IsAvailable(int lang)
// Test if setting the locale works, then set it back. // Test if setting the locale works, then set it back.
char * const oldLocale = wxStrdupA(setlocale(LC_ALL, NULL)); char * const oldLocale = wxStrdupA(setlocale(LC_ALL, NULL));
const bool available = wxSetlocaleTryAll(LC_ALL, info->CanonicalName); wxLocaleIdent locId;
wxString region;
locId.Language(info->CanonicalName.BeforeFirst('_', &region));
if ( !region.empty() )
{
// We never have encoding in our canonical names, but we can have
// modifiers, so take them into account.
wxString mod;
locId.Region(region.BeforeFirst('@', &mod));
if ( !mod.empty() )
locId.Modifier(mod);
}
const bool available = wxSetlocaleTryAll(LC_ALL, locId);
// restore the original locale // restore the original locale
wxSetlocale(LC_ALL, oldLocale); wxSetlocale(LC_ALL, oldLocale);

View File

@@ -179,54 +179,53 @@ wxString wxLocaleIdent::GetName() const
// Helper of wxSetlocaleTryAll() below which tries setting the given locale // Helper of wxSetlocaleTryAll() below which tries setting the given locale
// with and without UTF-8 suffix. Don't use this one directly. // with and without UTF-8 suffix. Don't use this one directly.
static const char *wxSetlocaleTryUTF8(int c, const wxString& lc) static const char *wxSetlocaleTryUTF8(int c, const wxLocaleIdent& locId)
{ {
const char *l = NULL; const char *l = NULL;
// NB: We prefer to set UTF-8 locale if it's possible and only fall back to // NB: We prefer to set UTF-8 locale if it's possible and only fall back to
// non-UTF-8 locale if it fails. // non-UTF-8 locale if it fails.
#if wxUSE_UNICODE #if wxUSE_UNICODE
if ( !lc.empty() ) if ( locId.GetCharset().empty() )
{ {
wxString buf(lc); wxLocaleIdent locIdUTF8(locId);
wxString buf2; locIdUTF8.Charset(wxS(".UTF-8"));
buf2 = buf + wxS(".UTF-8");
l = wxSetlocale(c, buf2); l = wxSetlocale(c, locIdUTF8.GetName());
if ( !l ) if ( !l )
{ {
buf2 = buf + wxS(".utf-8"); locIdUTF8.Charset(wxS(".utf-8"));
l = wxSetlocale(c, buf2); l = wxSetlocale(c, locIdUTF8.GetName());
} }
if ( !l ) if ( !l )
{ {
buf2 = buf + wxS(".UTF8"); locIdUTF8.Charset(wxS(".UTF8"));
l = wxSetlocale(c, buf2); l = wxSetlocale(c, locIdUTF8.GetName());
} }
if ( !l ) if ( !l )
{ {
buf2 = buf + wxS(".utf8"); locIdUTF8.Charset(wxS(".utf8"));
l = wxSetlocale(c, buf2); l = wxSetlocale(c, locIdUTF8.GetName());
} }
} }
// if we can't set UTF-8 locale, try non-UTF-8 one: // if we can't set UTF-8 locale, try non-UTF-8 one:
if ( !l ) if ( !l )
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE
l = wxSetlocale(c, lc); l = wxSetlocale(c, locId.GetName());
return l; return l;
} }
// Try setting all possible versions of the given locale, i.e. with and without // Try setting all possible versions of the given locale, i.e. with and without
// UTF-8 encoding, and with or without the "_territory" part. // UTF-8 encoding, and with or without the "_territory" part.
const char *wxSetlocaleTryAll(int c, const wxString& lc) const char *wxSetlocaleTryAll(int c, const wxLocaleIdent& locId)
{ {
const char* l = wxSetlocaleTryUTF8(c, lc); const char* l = wxSetlocaleTryUTF8(c, locId);
if ( !l ) if ( !l )
{ {
const wxString& lcOnlyLang = ExtractLang(lc); if ( !locId.GetRegion().empty() )
if ( lcOnlyLang != lc ) l = wxSetlocaleTryUTF8(c, wxLocaleIdent(locId).Region(wxString()));
l = wxSetlocaleTryUTF8(c, lcOnlyLang);
} }
return l; return l;
@@ -265,27 +264,31 @@ wxUILocaleImplUnix::Use()
return; return;
} }
const wxString& shortName = m_locId.GetName(); if ( !wxSetlocaleTryAll(LC_ALL, m_locId) )
if ( !wxSetlocaleTryAll(LC_ALL, shortName) )
{ {
// Some C libraries (namely glibc) still use old ISO 639, // Some C libraries (namely glibc) still use old ISO 639,
// so will translate the abbrev for them // so will translate the abbrev for them
wxString localeAlt; wxLocaleIdent locIdAlt(m_locId);
const wxString& langOnly = ExtractLang(shortName);
if ( langOnly == wxS("he") )
localeAlt = wxS("iw") + ExtractNotLang(shortName);
else if ( langOnly == wxS("id") )
localeAlt = wxS("in") + ExtractNotLang(shortName);
else if ( langOnly == wxS("yi") )
localeAlt = wxS("ji") + ExtractNotLang(shortName);
else if ( langOnly == wxS("nb") )
localeAlt = wxS("no_NO");
else if ( langOnly == wxS("nn") )
localeAlt = wxS("no_NY");
if ( !localeAlt.empty() ) const wxString& langOnly = m_locId.GetLanguage();
wxSetlocaleTryAll(LC_ALL, localeAlt); if ( langOnly == wxS("he") )
locIdAlt.Language(wxS("iw"));
else if ( langOnly == wxS("id") )
locIdAlt.Language(wxS("in"));
else if ( langOnly == wxS("yi") )
locIdAlt.Language(wxS("ji"));
else if ( langOnly == wxS("nb") || langOnly == wxS("nn") )
{
locIdAlt.Language(wxS("no"));
locIdAlt.Region(langOnly == wxS("nb") ? wxS("NO") : wxS("NY"));
}
else
{
// Nothing else to try.
return;
}
wxSetlocaleTryAll(LC_ALL, locIdAlt);
} }
} }