From 0c1dae32b4e47417ec46f261014a888d4ca33348 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 21 Nov 2015 10:11:50 +0000 Subject: i18n: allow language to be passed as a parameter If empty, it will still be fetched from the environment --- src/common/i18n.cpp | 9 +++++---- src/common/i18n.h | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/i18n.cpp b/src/common/i18n.cpp index 9add06cb9..06b4423a7 100644 --- a/src/common/i18n.cpp +++ b/src/common/i18n.cpp @@ -45,7 +45,7 @@ static std::map i18n_entries; // #define i18n_log(x) do { std::cout << __FILE__ << ":" << __LINE__ << ": " << x << std::endl; std::cout << std::flush; } while(0) #define i18n_log(x) ((void)0) -static std::string get_language() +std::string i18n_get_language() { const char *e; @@ -111,9 +111,9 @@ static std::string utf8(const unsigned char *data, uint32_t len) return std::string((const char *)data,len); } -int i18n_set_language(const char *directory, const char *base) +int i18n_set_language(const char *directory, const char *base, std::string language) { - std::string language, filename, contents; + std::string filename, contents; const unsigned char *data; size_t datalen; size_t idx; @@ -128,7 +128,8 @@ int i18n_set_language(const char *directory, const char *base) if (!directory || !base) return -1; - language = get_language(); + if (language.empty()) + language = i18n_get_language(); filename = std::string(directory) + "/" + base + "_" + language + ".qm"; i18n_log("Loading translations for language " << language); diff --git a/src/common/i18n.h b/src/common/i18n.h index 2aeac2287..8a1f3cf3d 100644 --- a/src/common/i18n.h +++ b/src/common/i18n.h @@ -30,7 +30,8 @@ #define QT_TRANSLATE_NOOP(context,str) i18n_translate(str,context) -int i18n_set_language(const char *directory, const char *base); +std::string i18n_get_language(); +int i18n_set_language(const char *directory, const char *base, std::string language = std::string()); const char *i18n_translate(const char *str, const std::string &context); static inline std::string get_default_i18n_context() { return std::string(); } static inline const char *tr(const char *str) { return i18n_translate(str,get_default_i18n_context()); } -- cgit v1.2.3 From f3724aef881ff2fdada98eddc792059e4e18c1c8 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 21 Nov 2015 10:13:10 +0000 Subject: Fix startup crash when using a locale boost does not like There are various locale related bugs in various versions of boost, where exceptions are thrown in boost::filesystem APIs when the current locale is not to boost's liking. It's not clear what "not to boost's liking" means in detail, though "en" and "en_US.UTF-8" are not to its liking. Fix it by running a test function that's known to throw in such a case, and resetting LANG and LC_ALL to C if an exception is thrown. In simplewallet, the locale is queried before that so the correct translations will still be used. --- src/common/util.cpp | 19 +++++++++++++++++++ src/common/util.h | 2 ++ 2 files changed, 21 insertions(+) (limited to 'src/common') diff --git a/src/common/util.cpp b/src/common/util.cpp index 7d39bc4f4..4c1b44004 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -43,6 +43,7 @@ using namespace epee; #else #include #endif +#include namespace tools @@ -389,4 +390,22 @@ std::string get_nix_version_display_string() #endif return std::error_code(code, std::system_category()); } + + bool sanitize_locale() + { + // boost::filesystem throws for "invalid" locales, such as en_US.UTF-8, or kjsdkfs, + // so reset it here before any calls to it + try + { + boost::filesystem::path p {std::string("test")}; + p /= std::string("test"); + } + catch (...) + { + setenv("LC_ALL", "C", 1); + setenv("LANG", "C", 1); + return true; + } + return false; + } } diff --git a/src/common/util.h b/src/common/util.h index 883fe1e0f..236a0b6f0 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -88,6 +88,8 @@ namespace tools */ std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name); + bool sanitize_locale(); + inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot) { std::string s; -- cgit v1.2.3