From f0b54bef2f5aef5487625ece22fa34845b9ad8e5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 6 Jan 2023 03:48:39 +0100 Subject: [PATCH] Handle wxLANGUAGE_DEFAULT specially in wxLocale::IsAvailable() Check if using Init(wxLANGUAGE_DEFAULT) is going to succeed by actually calling wxSetLocale(LC_ALL, "") in this special case, as we can't if it's going to work otherwise if we don't recognize the language. --- src/common/intl.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/common/intl.cpp b/src/common/intl.cpp index abcff7b1a0..c32d708173 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -732,11 +732,36 @@ bool wxLocale::IsAvailable(int lang) const wxLanguageInfo *info = wxLocale::GetLanguageInfo(lang); if ( !info ) { - // The language is unknown (this normally only happens when we're - // passed wxLANGUAGE_DEFAULT), so we can't support it. - wxASSERT_MSG( lang == wxLANGUAGE_DEFAULT, - wxS("No info for a valid language?") ); - return false; + // This must be wxLANGUAGE_DEFAULT as otherwise we should have found + // the matching entry. + wxCHECK_MSG( lang == wxLANGUAGE_DEFAULT, false, + wxS("No info for a valid language?") ); + + // For this one, we need to check whether using it later is going to + // actually work, i.e. if the CRT supports it. + const char* const origLocale = wxSetlocale(LC_ALL, nullptr); + if ( !origLocale ) + { + // This is not supposed to happen, we should always be able to + // query the current locale, but don't crash if it does. + return false; + } + + // Make a copy of the string because wxSetlocale() call below may + // change the buffer to which it points. + const wxString origLocaleStr = wxString::FromUTF8(origLocale); + + if ( !wxSetlocale(LC_ALL, "") ) + { + // Locale wasn't changed, so nothing else to do. + return false; + } + + // We support this locale, but restore the original one before + // returning. + wxSetlocale(LC_ALL, origLocaleStr.utf8_str()); + + return true; } wxString localeTag = info->GetCanonicalWithRegion();