+ if ( s_pfnGetUserPreferredUILanguages )
+ {
+ ULONG numLangs;
+ ULONG bufferSize = 0;
+ if ( (*s_pfnGetUserPreferredUILanguages)(MUI_LANGUAGE_NAME,
+ &numLangs,
+ NULL,
+ &bufferSize) )
+ {
+ wxScopedArray<WCHAR> langs(new WCHAR[bufferSize]);
+ if ( (*s_pfnGetUserPreferredUILanguages)(MUI_LANGUAGE_NAME,
+ &numLangs,
+ langs.get(),
+ &bufferSize) )
+ {
+ wxArrayString preferred;
+
+ WCHAR *buf = langs.get();
+ for ( unsigned i = 0; i < numLangs; i++ )
+ {
+ const wxString lang(buf);
+ preferred.push_back(lang);
+ buf += lang.length() + 1;
+ }
+ LogTraceArray(" - system preferred languages", preferred);
+
+ for ( wxArrayString::const_iterator j = preferred.begin();
+ j != preferred.end();
+ ++j )
+ {
+ wxString lang(*j);
+ lang.Replace("-", "_");
+ if ( available.Index(lang) != wxNOT_FOUND )
+ return lang;
+ size_t pos = lang.find('_');
+ if ( pos != wxString::npos )
+ {
+ lang = lang.substr(0, pos);
+ if ( available.Index(lang) != wxNOT_FOUND )
+ return lang;
+ }
+ }
+ }
+ }
+ }
+
+ return GetPreferredUILanguageFallback(available);
+}
+
+#elif defined(__WXOSX__)
+
+void LogTraceArray(const char *prefix, CFArrayRef arr)
+{
+ wxString s;
+ const unsigned count = CFArrayGetCount(arr);
+ if ( count )
+ {
+ s += wxCFStringRef::AsString((CFStringRef)CFArrayGetValueAtIndex(arr, 0));
+ for ( unsigned i = 1 ; i < count; i++ )
+ s += "," + wxCFStringRef::AsString((CFStringRef)CFArrayGetValueAtIndex(arr, i));
+ }
+ wxLogTrace(TRACE_I18N, "%s: [%s]", prefix, s);
+}
+
+wxString GetPreferredUILanguage(const wxArrayString& available)
+{
+ wxStringToStringHashMap availableNormalized;
+ wxCFRef<CFMutableArrayRef> availableArr(
+ CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
+
+ for ( wxArrayString::const_iterator i = available.begin();
+ i != available.end();
+ ++i )
+ {
+ wxString lang(*i);
+ wxCFStringRef code_wx(*i);
+ wxCFStringRef code_norm(
+ CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault, code_wx));
+ CFArrayAppendValue(availableArr, code_norm);
+ availableNormalized[code_norm.AsString()] = *i;
+ }
+ LogTraceArray(" - normalized available list", availableArr);
+
+ wxCFRef<CFArrayRef> prefArr(
+ CFBundleCopyLocalizationsForPreferences(availableArr, NULL));
+ LogTraceArray(" - system preferred languages", prefArr);
+
+ unsigned prefArrLength = CFArrayGetCount(prefArr);
+ if ( prefArrLength > 0 )
+ {
+ // Lookup the name in 'available' by index -- we need to get the
+ // original value corresponding to the normalized one chosen.
+ wxString lang(wxCFStringRef::AsString((CFStringRef)CFArrayGetValueAtIndex(prefArr, 0)));
+ wxStringToStringHashMap::const_iterator i = availableNormalized.find(lang);
+ if ( i == availableNormalized.end() )
+ return lang;
+ else
+ return i->second;
+ }
+
+ return GetPreferredUILanguageFallback(available);
+}
+
+#else
+
+// On Unix, there's just one language=locale setting, so we should always
+// use that.
+#define GetPreferredUILanguage GetPreferredUILanguageFallback
+
+#endif
+
+} // anonymous namespace