From 27594dcd81c444c46fb32dd453a67fa5911ef98e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 1 Sep 2021 02:01:12 +0200 Subject: [PATCH] Use NSLocale rather than CFLocale functions for consistency We need to use [NSString compare:options:range:locale:], as there is no corresponding CFString function, so use NSLocale functions rather than CFLocale ones too, for consistency. No real changes. --- src/osx/core/uilocale.mm | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/osx/core/uilocale.mm b/src/osx/core/uilocale.mm index e70d72050c..5cdc1fb576 100644 --- a/src/osx/core/uilocale.mm +++ b/src/osx/core/uilocale.mm @@ -26,9 +26,6 @@ #include "wx/osx/core/cfref.h" #include "wx/osx/core/cfstring.h" -#include -#include - #import #import @@ -73,28 +70,33 @@ namespace class wxUILocaleImplCF : public wxUILocaleImpl { public: - explicit wxUILocaleImplCF(const wxCFRef& cfloc) - : m_cfloc(cfloc) + explicit wxUILocaleImplCF(NSLocale* nsloc) + : m_nsloc([nsloc retain]) { } + ~wxUILocaleImplCF() wxOVERRIDE + { + [m_nsloc release]; + } + static wxUILocaleImplCF* Create(const wxLocaleIdent& locId) { - // 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. + // Surprisingly, localeWithLocaleIdentifier: 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 - all = CFLocaleCopyAvailableLocaleIdentifiers(); + all((CFArrayRef)[NSLocale availableLocaleIdentifiers]); wxCFStringRef cfName(locId.GetName()); - if ( !CFArrayContainsValue(all, CFRangeMake(0, CFArrayGetCount(all)), cfName) ) + if ( ![(NSArray*)all.get() containsObject: cfName.AsNSString()] ) return NULL; - CFLocaleRef cfloc = CFLocaleCreate(kCFAllocatorDefault, cfName); - if ( !cfloc ) + auto nsloc = [NSLocale localeWithLocaleIdentifier: cfName.AsNSString()]; + if ( !nsloc ) return NULL; - return new wxUILocaleImplCF(cfloc); + return new wxUILocaleImplCF(nsloc); } void Use() wxOVERRIDE; @@ -104,7 +106,7 @@ public: int flags) const wxOVERRIDE; private: - wxCFRef m_cfloc; + NSLocale* const m_nsloc; wxDECLARE_NO_COPY_CLASS(wxUILocaleImplCF); }; @@ -125,13 +127,13 @@ wxUILocaleImplCF::Use() wxString wxUILocaleImplCF::GetName() const { - return wxCFStringRef::AsString(CFLocaleGetIdentifier(m_cfloc)); + return wxCFStringRef::AsString([m_nsloc localeIdentifier]); } wxString wxUILocaleImplCF::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) const { - return wxGetInfoFromCFLocale(m_cfloc, index, cat); + return wxGetInfoFromCFLocale((CFLocaleRef)m_nsloc, index, cat); } /* static */ @@ -143,7 +145,7 @@ wxUILocaleImpl* wxUILocaleImpl::CreateStdC() /* static */ wxUILocaleImpl* wxUILocaleImpl::CreateUserDefault() { - return new wxUILocaleImplCF(CFLocaleCopyCurrent()); + return new wxUILocaleImplCF([NSLocale currentLocale]); } /* static */ @@ -167,7 +169,7 @@ wxUILocaleImplCF::CompareStrings(const wxString& lhs, const wxString& rhs, NSComparisonResult ret = [ns_lhs compare:ns_rhs options:options range:(NSRange){0, [ns_lhs length]} - locale:(id)(CFLocaleRef)m_cfloc]; + locale:m_nsloc]; switch (ret) {