From d3a1abaab208e8ba05f250a6dac0e5fccc11a4be Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 20 Aug 2021 23:35:16 +0200 Subject: [PATCH] Untangle Mac and POSIX code in wxLocale::GetSystemLanguage() Most of the code in this function was Unix-specific and didn't make sense for Mac, where we never have to apply the workarounds in it nor deal with languages specified without the territory etc, so separate Mac branch using CoreFouundation API from the rest of the Unix code to make things more understandable. No real changes. --- src/common/intl.cpp | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 4d900c45ad..e0966452e3 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -634,9 +634,6 @@ inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value) size_t i = 0, count = ms_languagesDB->GetCount(); -#if defined(__UNIX__) - // first get the string identifying the language from the environment - wxString langFull; #ifdef __WXOSX__ wxCFRef userLocaleRef(CFLocaleCopyCurrent()); @@ -644,10 +641,34 @@ inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value) // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode))); - langFull = str.AsString()+"_"; + const wxString langPrefix = str.AsString() + "_"; + str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode))); - langFull += str.AsString(); -#else + const wxString langFull = langPrefix + str.AsString(); + + int langOnlyMatchIndex = wxNOT_FOUND; + for ( i = 0; i < count; i++ ) + { + const wxString& fullname = ms_languagesDB->Item(i).CanonicalName; + if ( langFull == fullname ) + { + // Exact match, no need to look any further. + break; + } + + if ( fullname.StartsWith(langPrefix) ) + { + // Matched just the language, keep looking, but we'll keep this if + // we don't find an exact match later. + langOnlyMatchIndex = i; + } + } + + if ( i == count && langOnlyMatchIndex != wxNOT_FOUND ) + i = langOnlyMatchIndex; +#elif defined(__UNIX__) + // first get the string identifying the language from the environment + wxString langFull; if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull) && !wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull) && !wxGetNonEmptyEnvVar(wxS("LANG"), &langFull)) @@ -656,8 +677,6 @@ inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value) return wxLANGUAGE_ENGLISH_US; } -#endif - // the language string has the following form // // lang[_LANG][.encoding][@modifier]