Add wxUILocale::IsSupported()

This function can now be implemented relatively straightforwardly
(although it does require an extra check under Mac), so add it, as it
can be generally useful and we're also going to need it for our own
tests in the upcoming commit.
This commit is contained in:
Vadim Zeitlin
2021-08-30 21:02:00 +02:00
parent aa4cce8f7d
commit 32316af106
5 changed files with 38 additions and 2 deletions

View File

@@ -48,6 +48,10 @@ public:
// Create the object corresponding to the given locale.
explicit wxUILocale(const wxLocaleIdent& localeId);
// Check if the locale is actually supported by the current system: if it's
// not supported, the other functions will behave as for the "C" locale.
bool IsSupported() const;
// Get the platform-dependent name of the current locale.
wxString GetName() const;

View File

@@ -156,6 +156,17 @@ public:
*/
wxString GetInfo(wxLocaleInfo index,
wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT) const;
/**
Return true if locale is supported on the current system.
If this function returns @a false, the other functions of this class,
such as GetInfo() and CompareStrings(), behave as in "C" locale, i.e.
it's still safe to call them, but their results don't reflect the rules
for the locale in question, but just use the default (i.e. US English)
conventions.
*/
bool IsSupported() const;
};
/**

View File

@@ -116,6 +116,11 @@ void wxUILocale::SetImpl(wxUILocaleImpl* impl)
m_impl = impl;
}
bool wxUILocale::IsSupported() const
{
return m_impl != NULL;
}
wxString wxUILocale::GetName() const
{
if ( !m_impl )

View File

@@ -80,8 +80,17 @@ public:
static wxUILocaleImplCF* Create(const wxLocaleIdent& locId)
{
CFLocaleRef cfloc = CFLocaleCreate(kCFAllocatorDefault,
wxCFStringRef(locId.GetName()));
// Surprisingly, CFLocaleCreate() always succeeds, even for completely
// invalid strings, so we need to check if the name is actually in the
// list of the supported locales ourselves.
static wxCFRef<CFArrayRef>
all = CFLocaleCopyAvailableLocaleIdentifiers();
wxCFStringRef cfName(locId.GetName());
if ( !CFArrayContainsValue(all, CFRangeMake(0, CFArrayGetCount(all)), cfName) )
return NULL;
CFLocaleRef cfloc = CFLocaleCreate(kCFAllocatorDefault, cfName);
if ( !cfloc )
return NULL;

View File

@@ -240,6 +240,13 @@ TEST_CASE("wxLocale::Default", "[locale]")
#endif // wxUSE_UNICODE
TEST_CASE("wxUILocale::IsSupported", "[uilocale]")
{
CHECK( wxUILocale("en").IsSupported() );
CHECK( wxUILocale(wxLocaleIdent("fr").Region("FR")).IsSupported() );
CHECK( !wxUILocale("bloordyblop").IsSupported() );
}
TEST_CASE("wxUILocale::GetInfo", "[uilocale]")
{
CHECK( wxUILocale("en").GetInfo(wxLOCALE_DECIMAL_POINT) == "." );