Support wxLANGUAGE_DEFAULT in wxLocale even when it is unknown

Don't give an error if we can't recognize the current system language,
as we may still be able to use it even in this case.

Closes #19082.
This commit is contained in:
Vadim Zeitlin
2021-02-28 20:00:27 +01:00
parent 3e395f9b5f
commit 18bf718f60

View File

@@ -427,43 +427,44 @@ static const char *wxSetlocaleTryUTF8(int c, const wxString& lc)
} }
#endif // __UNIX__ #endif // __UNIX__
bool wxLocale::Init(int language, int flags) bool wxLocale::Init(int lang, int flags)
{ {
#if WXWIN_COMPATIBILITY_2_8 #if WXWIN_COMPATIBILITY_2_8
wxASSERT_MSG( !(flags & wxLOCALE_CONV_ENCODING), wxASSERT_MSG( !(flags & wxLOCALE_CONV_ENCODING),
wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") ); wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") );
#endif #endif
int lang = language; wxString name, shortName;
if (lang == wxLANGUAGE_DEFAULT)
{
// auto detect the language
lang = GetSystemLanguage();
}
// We failed to detect system language, so we will use English:
if (lang == wxLANGUAGE_UNKNOWN)
{
return false;
}
const wxLanguageInfo *info = GetLanguageInfo(lang); const wxLanguageInfo *info = GetLanguageInfo(lang);
// Unknown language: // Unknown language:
if (info == NULL) if (info == NULL)
{
// This could have happened because some concrete language has been
// requested and we just don't know anything about it. In this case, we
// have no choice but to simply give up.
if ( lang != wxLANGUAGE_DEFAULT )
{ {
wxLogError(wxS("Unknown language %i."), lang); wxLogError(wxS("Unknown language %i."), lang);
return false; return false;
} }
const wxString& name = info->Description; // However in case we didn't recognize the default system language, we
DoInit(name, info->CanonicalName, lang); // can still try to use it, even though we don't know anything about it
// because setlocale() still might.
}
else
{
name = info->Description;
shortName = info->CanonicalName;
}
DoInit(name, shortName, lang);
// Set the locale: // Set the locale:
#if defined(__UNIX__) #if defined(__UNIX__)
const wxString& shortName = info->CanonicalName; const char *retloc = wxSetlocaleTryUTF8(LC_ALL, shortName);
const char *retloc = wxSetlocaleTryUTF8(LC_ALL, locale);
const wxString langOnly = ExtractLang(shortName); const wxString langOnly = ExtractLang(shortName);
if ( !retloc ) if ( !retloc )
@@ -510,11 +511,18 @@ bool wxLocale::Init(int language, int flags)
#endif // __AIX__ #endif // __AIX__
#elif defined(__WIN32__) #elif defined(__WIN32__)
const char *retloc = "C"; const char *retloc;
if ( info->WinLang == 0 ) if ( !info )
{
// We're using the system language, but we don't know what it is, so
// just use setlocale() to deal with it.
retloc = wxSetlocale(LC_ALL, "");
}
else if ( info->WinLang == 0 )
{ {
wxLogWarning(wxS("Locale '%s' not supported by OS."), name); wxLogWarning(wxS("Locale '%s' not supported by OS."), name);
// retloc already set to "C"
retloc = "C";
} }
else // language supported by Windows else // language supported by Windows
{ {
@@ -560,14 +568,11 @@ bool wxLocale::Init(int language, int flags)
#endif #endif
#ifndef WX_NO_LOCALE_SUPPORT #ifndef WX_NO_LOCALE_SUPPORT
// NB: don't use 'lang' here, 'language'
return DoCommonPostInit return DoCommonPostInit
( (
retloc != NULL, retloc != NULL,
name, name,
language == wxLANGUAGE_DEFAULT shortName,
? wxString()
: info->CanonicalName,
flags & wxLOCALE_LOAD_DEFAULT flags & wxLOCALE_LOAD_DEFAULT
); );
#endif // !WX_NO_LOCALE_SUPPORT #endif // !WX_NO_LOCALE_SUPPORT