Rewire and corrected Doxygen comment for gnc_get_locale() c++ function.

There's no point in going through the cache dance on MinGW, that will
just throw.
This commit is contained in:
John Ralls
2019-04-30 15:00:06 -07:00
parent 529a6cb067
commit bfbb89f6e2

View File

@@ -27,29 +27,29 @@ extern "C"
#include <boost/locale.hpp> #include <boost/locale.hpp>
#include "gnc-locale-utils.hpp" #include "gnc-locale-utils.hpp"
/* This function addresses two separate problems: First, if we set /** Cache the UI locale
* std::locale::global then all streams automagically imbue
* themselves with it and we have to re-imbue all of the backends and
* logging streams with std::locale::classic() so that data and log
* files aren't localized. Second, calling std::locale("") is slow,
* so we want to do it only once. Worse, the standard C++ library in
* Mingw64 chokes on at least some Microsoft-style locale strings
* (e.g. "Spanish_Spain") but libc's setlocale(LC_ALL, NULL) emits
* them even if originally fed a Unix-style locale ("es_ES").
* *
* The solution is this function which caches the setlocale() locale * We don't want to set the default locale because we need
* the first time it's called and which uses a boost::locale * std::locale::classic for consistency in stored data. We also don't
* generator, which does know what to do with (sometimes adjusted) * want to call std::locale("") more than once, it's slow... and we
* Microsoft locale strings. * don't want to call it on MinGW32 at all because that supports only
* std::locale::classic and throws if you try to construct anything
* else. Boost::locale doesn't support std::locale::facet required by
* boost::datetime (go figure).
*
* @return a copy of std::locale::classic() on MinGW32 and a copy of
* the cached result of std::locale("") otherwise.
*/ */
const std::locale& const std::locale&
gnc_get_locale() gnc_get_locale()
{ {
#ifdef __MINGW32__
return std::locale::classic(); // Nothing else supported.
#else
static std::locale cached; static std::locale cached;
static bool tried_already = false; static bool tried_already = false;
if (!tried_already) if (!tried_already)
{ {
boost::locale::generator gen;
tried_already = true; tried_already = true;
try try
{ {
@@ -57,11 +57,7 @@ gnc_get_locale()
} }
catch (const std::runtime_error& err) catch (const std::runtime_error& err)
{ {
#ifdef __MINGW32__
char* locale = g_win32_getlocale();
#else
char* locale = g_strdup(setlocale(LC_ALL, "")); char* locale = g_strdup(setlocale(LC_ALL, ""));
#endif
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
"Failed to create C++ default locale from" "Failed to create C++ default locale from"
@@ -72,6 +68,7 @@ gnc_get_locale()
} }
} }
return cached; return cached;
#endif
} }