Add wxLanguageInfo::GetCanonicalWithRegion() helper

This simple function allows to simplify the code by avoid to test for
CanonicalRef before using either it or CanonicalName in multiple places.

No real changes.

See #22252.
This commit is contained in:
utelle
2022-04-01 11:26:22 +02:00
committed by Vadim Zeitlin
parent fdfb9cda1b
commit 280cb68887
4 changed files with 23 additions and 5 deletions

View File

@@ -151,6 +151,9 @@ struct WXDLLIMPEXP_BASE wxLanguageInfo
// supported
wxString GetLocaleName() const;
// returns CanonicalRef if set, otherwise CanonicalName
wxString GetCanonicalWithRegion() const;
// Call setlocale() and return non-null value if it works for this language.
//
// This function is mostly for internal use, as changing locale involves

View File

@@ -87,6 +87,16 @@ struct wxLanguageInfo
string is empty.
*/
wxString GetLocaleName() const;
/**
Return the canonical locale name including the region, if known.
The value is identical to @c CanonicalRef, if not empty,
otherwise it is identical to @c CanonicalName.
@since 3.1.6
*/
wxString GetCanonicalWithRegion() const;
};

View File

@@ -159,19 +159,24 @@ const char* wxLanguageInfo::TrySetLocale() const
const char* wxLanguageInfo::TrySetLocale() const
{
return wxSetlocale(LC_ALL, CanonicalRef.empty() ? CanonicalName : CanonicalRef);
return wxSetlocale(LC_ALL, GetCanonicalWithRegion());
}
#endif // __WINDOWS__/!__WINDOWS__
wxString wxLanguageInfo::GetLocaleName() const
{
wxString localeId = CanonicalRef.empty() ? CanonicalName : CanonicalRef;
wxString localeId = GetCanonicalWithRegion();
wxUILocale uiLocale = wxUILocale::FromTag(localeId);
wxString localeName = uiLocale.IsSupported() ? uiLocale.GetName() : wxString();
return localeName;
}
wxString wxLanguageInfo::GetCanonicalWithRegion() const
{
return CanonicalRef.empty() ? CanonicalName : CanonicalRef;
}
// ----------------------------------------------------------------------------
// wxUILocale
// ----------------------------------------------------------------------------
@@ -430,7 +435,7 @@ bool wxLocale::Init(int lang, int flags)
else
{
name = info->Description;
shortName = info->CanonicalRef.empty() ? info->CanonicalName : info->CanonicalRef;
shortName = info->GetCanonicalWithRegion();
}
DoInit(name, shortName, lang);
@@ -738,7 +743,7 @@ bool wxLocale::IsAvailable(int lang)
return false;
}
wxString localeTag = info->CanonicalRef.empty() ? info->LocaleTag : info->CanonicalRef;
wxString localeTag = info->GetCanonicalWithRegion();
wxUILocale uiLocale(wxLocaleIdent::FromTag(localeTag));
return uiLocale.IsSupported();

View File

@@ -157,7 +157,7 @@ locale_t TryCreateMatchingLocale(wxLocaleIdent& locId)
it != infos.end();
++it )
{
const wxString& fullname = it->CanonicalRef.empty() ? it->CanonicalName : it->CanonicalRef;
const wxString& fullname = it->GetCanonicalWithRegion();
if ( fullname.BeforeFirst('_') == lang )
{
// We never have encoding in our canonical names, but we