From 67f2147f3d939dd86d35266b3fcc6de1953a969b Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 27 Mar 2013 22:48:16 +0000 Subject: [PATCH] Improved locale detection on Mac. Respect the user's language selection in the system settings, and try those languages in turn. If a language is explicitly specified, try that first regardless. --- src/Main/locale.cxx | 81 ++++++++++++++++++++++++++++++++++----------- src/Main/locale.hxx | 10 +++--- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/Main/locale.cxx b/src/Main/locale.cxx index 680ee68bd..cdf4f6912 100644 --- a/src/Main/locale.cxx +++ b/src/Main/locale.cxx @@ -26,6 +26,8 @@ #include #endif +#include + #include #include @@ -57,9 +59,10 @@ FGLocale::~FGLocale() * * This software comes with no warranty. Use at your own risk. */ -const char* +string_list FGLocale::getUserLanguage() { + string_list result; static char locale[100] = {0}; if (GetLocaleInfo(LOCALE_USER_DEFAULT, @@ -72,23 +75,55 @@ FGLocale::getUserLanguage() if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, locale+i, (int)(sizeof(locale)-i))) - return locale; - + { + result.push_back(locale); + return result; + } + locale[--i] = 0; SG_LOG(SG_GENERAL, SG_WARN, "Failed to detected locale's country setting."); - return locale; + result.push_back(locale); + return result; } - return NULL; + return result; +} +#elif __APPLE__ + +// determine locale / langauge on Mac +#include + +string_list +FGLocale::getUserLanguage() +{ + string_list result; + CFArrayRef langs = CFLocaleCopyPreferredLanguages(); + + char buffer[64]; + for (int i=0; i")); - } - - // Use plain C locale if nothing is available. - if ((language == NULL)||(language[0]==0)) - { + string_list languages = getUserLanguage(); + if (languages.empty()) { + // Use plain C locale if nothing is available. SG_LOG(SG_GENERAL, SG_WARN, "Unable to detect system language" ); - language = "C"; + languages.push_back("C"); + } + + // if we were passed a language option, try it first + if ((language != NULL) && (strlen(language) > 0)) { + languages.insert(languages.begin(), string(language)); } - SGPropertyNode *locale = findLocaleNode(language); + + SGPropertyNode *locale = NULL; + BOOST_FOREACH(string lang, languages) { + locale = findLocaleNode(lang); + if (locale) { + break; + } + } + if (!locale) { SG_LOG(SG_GENERAL, SG_ALERT, diff --git a/src/Main/locale.hxx b/src/Main/locale.hxx index 8ccd2d218..984a4c802 100644 --- a/src/Main/locale.hxx +++ b/src/Main/locale.hxx @@ -72,10 +72,7 @@ public: */ static void utf8toLatin1 (std::string& s); - /** - * Obtain user's default language setting. - */ - const char* getUserLanguage(); + protected: /** @@ -98,6 +95,11 @@ protected: */ simgear::PropertyList getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context); + /** + * Obtain user's default language setting. + */ + string_list getUserLanguage(); + SGPropertyNode_ptr _intl; SGPropertyNode_ptr _currentLocale; SGPropertyNode_ptr _defaultLocale; -- 2.39.5