Merge pull request #493

f3724ae Fix startup crash when using a locale boost does not like (moneromooo-monero)
0c1dae3 i18n: allow language to be passed as a parameter (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-11-21 13:08:39 +02:00
commit 9276233b9e
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
9 changed files with 40 additions and 6 deletions

View File

@ -94,6 +94,8 @@ int main(int argc, char* argv[])
uint32_t log_level = 0; uint32_t log_level = 0;
uint64_t block_stop = 0; uint64_t block_stop = 0;
tools::sanitize_locale();
boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_data_path {tools::get_default_data_dir()};
boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
boost::filesystem::path output_file_path; boost::filesystem::path output_file_path;

View File

@ -42,6 +42,8 @@ int main(int argc, char* argv[])
uint64_t block_stop = 0; uint64_t block_stop = 0;
bool blocks_dat = false; bool blocks_dat = false;
tools::sanitize_locale();
boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_data_path {tools::get_default_data_dir()};
boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
boost::filesystem::path output_file_path; boost::filesystem::path output_file_path;

View File

@ -538,6 +538,8 @@ int main(int argc, char* argv[])
std::string m_config_folder; std::string m_config_folder;
std::string db_arg_str; std::string db_arg_str;
tools::sanitize_locale();
boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_data_path {tools::get_default_data_dir()};
boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
std::string import_file_path; std::string import_file_path;

View File

@ -45,7 +45,7 @@ static std::map<std::string,std::string> i18n_entries;
// #define i18n_log(x) do { std::cout << __FILE__ << ":" << __LINE__ << ": " << x << std::endl; std::cout << std::flush; } while(0) // #define i18n_log(x) do { std::cout << __FILE__ << ":" << __LINE__ << ": " << x << std::endl; std::cout << std::flush; } while(0)
#define i18n_log(x) ((void)0) #define i18n_log(x) ((void)0)
static std::string get_language() std::string i18n_get_language()
{ {
const char *e; 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); 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; const unsigned char *data;
size_t datalen; size_t datalen;
size_t idx; size_t idx;
@ -128,7 +128,8 @@ int i18n_set_language(const char *directory, const char *base)
if (!directory || !base) if (!directory || !base)
return -1; return -1;
language = get_language(); if (language.empty())
language = i18n_get_language();
filename = std::string(directory) + "/" + base + "_" + language + ".qm"; filename = std::string(directory) + "/" + base + "_" + language + ".qm";
i18n_log("Loading translations for language " << language); i18n_log("Loading translations for language " << language);

View File

@ -30,7 +30,8 @@
#define QT_TRANSLATE_NOOP(context,str) i18n_translate(str,context) #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); const char *i18n_translate(const char *str, const std::string &context);
static inline std::string get_default_i18n_context() { return std::string(); } 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()); } static inline const char *tr(const char *str) { return i18n_translate(str,get_default_i18n_context()); }

View File

@ -43,6 +43,7 @@ using namespace epee;
#else #else
#include <sys/utsname.h> #include <sys/utsname.h>
#endif #endif
#include <boost/filesystem.hpp>
namespace tools namespace tools
@ -389,4 +390,22 @@ std::string get_nix_version_display_string()
#endif #endif
return std::error_code(code, std::system_category()); 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;
}
} }

View File

@ -88,6 +88,8 @@ namespace tools
*/ */
std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name); 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) inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot)
{ {
std::string s; std::string s;

View File

@ -54,6 +54,8 @@ int main(int argc, char const * argv[])
_note_c("dbg/main", "Begin of main()"); _note_c("dbg/main", "Begin of main()");
// TODO parse the debug options like set log level right here at start // TODO parse the debug options like set log level right here at start
tools::sanitize_locale();
epee::string_tools::set_module_name_and_folder(argv[0]); epee::string_tools::set_module_name_and_folder(argv[0]);
// Build argument description // Build argument description

View File

@ -2164,6 +2164,9 @@ int main(int argc, char* argv[])
//TRY_ENTRY(); //TRY_ENTRY();
std::string lang = i18n_get_language();
tools::sanitize_locale();
string_tools::set_module_name_and_folder(argv[0]); string_tools::set_module_name_and_folder(argv[0]);
po::options_description desc_general(sw::tr("General options")); po::options_description desc_general(sw::tr("General options"));
@ -2212,7 +2215,7 @@ int main(int argc, char* argv[])
po::positional_options_description positional_options; po::positional_options_description positional_options;
positional_options.add(arg_command.name, -1); positional_options.add(arg_command.name, -1);
i18n_set_language("translations", "monero"); i18n_set_language("translations", "monero", lang);
po::options_description desc_all; po::options_description desc_all;
desc_all.add(desc_general).add(desc_params); desc_all.add(desc_general).add(desc_params);