Clean up gettext initialization

master
ShadowNinja 2015-10-15 00:45:56 -04:00 committed by kwolekr
parent 7b8d372947
commit e067ceacb8
2 changed files with 49 additions and 65 deletions

View File

@ -118,106 +118,90 @@ const char* MSVC_LocaleLookup(const char* raw_shortname) {
/******************************************************************************/ /******************************************************************************/
#ifdef _MSC_VER #ifdef _MSC_VER
void init_gettext(const char *path, const std::string &configured_language, int argc, char** argv) { void init_gettext(const char *path, const std::string &configured_language,
int argc, const char *argv[])
#else #else
void init_gettext(const char *path, const std::string &configured_language) { void init_gettext(const char *path, const std::string &configured_language)
#endif #endif
{
#if USE_GETTEXT #if USE_GETTEXT
/** first try to set user override environment **/ // First, try to set user override environment
if (configured_language.length() != 0) { if (!configured_language.empty()) {
#ifndef _WIN32 #ifndef _WIN32
/* add user specified locale to environment */ // Add user specified locale to environment
setenv("LANGUAGE", configured_language.c_str(), 1); setenv("LANGUAGE", configured_language.c_str(), 1);
/* reload locale with changed environment */ // Reload locale with changed environment
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
std::string current_language_var(""); std::string current_language;
if (getenv("LANGUAGE") != 0) { const char *env_lang = getenv("LANGUAGE");
current_language_var = std::string(getenv("LANGUAGE")); if (env_lang)
} current_language = env_lang;
char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char)); _putenv(("LANGUAGE=" + configured_language).c_str());
strcat(lang_str, "LANGUAGE="); SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
strcat(lang_str, configured_language.c_str());
putenv(lang_str);
SetEnvironmentVariableA("LANGUAGE",configured_language.c_str());
#ifndef SERVER #ifndef SERVER
//very very dirty workaround to force gettext to see the right environment // Hack to force gettext to see the right environment
if (current_language_var != configured_language) { if (current_language != configured_language) {
STARTUPINFO startupinfo; errorstream << "MSVC localization workaround active. "
PROCESS_INFORMATION processinfo; "Restarting " PROJECT_NAME_C " in a new environment!" << std::endl;
memset(&startupinfo, 0, sizeof(startupinfo));
memset(&processinfo, 0, sizeof(processinfo));
errorstream << "MSVC localization workaround active restating minetest in new environment!" << std::endl;
std::string parameters = ""; std::string parameters;
for (unsigned int i = 1; i < argc; i++) {
if (!parameters.empty())
parameters += ' ';
for (unsigned int i=1;i < argc; i++) {
if (parameters != "") {
parameters += " ";
}
parameters += argv[i]; parameters += argv[i];
} }
const char* ptr_parameters = 0; const char *ptr_parameters = NULL;
if (parameters != "") { if (!parameters.empty())
ptr_parameters = parameters.c_str(); ptr_parameters = parameters.c_str();
}
/** users may start by short name in commandline without extention **/ // Allow calling without an extension
std::string appname = argv[0]; std::string app_name = argv[0];
if (appname.substr(appname.length() - 4) != ".exe") { if (app_name.compare(appname.size() - 4, 4, ".exe") != 0)
appname += ".exe"; app_name += ".exe";
}
if (!CreateProcess(appname.c_str(), STARTUPINFO startup_info = {0};
(char*) ptr_parameters, PROCESS_INFORMATION process_info = {0};
NULL,
NULL, bool success = CreateProcess(app_name.c_str(), (char *)ptr_parameters,
false, NULL, NULL, false, DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT, NULL, NULL, &startup_info, &process_info);
NULL,
NULL, if (success) {
&startupinfo, exit(0);
&processinfo)) { // NOTREACHED
} else {
char buffer[1024]; char buffer[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL, FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), buffer,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), sizeof(buffer) - 1, NULL);
buffer,
sizeof(buffer)-1,
NULL);
errorstream << "*******************************************************" << std::endl; errorstream << "*******************************************************" << std::endl;
errorstream << "CMD: " << appname << std::endl; errorstream << "CMD: " << app_name << std::endl;
errorstream << "Failed to restart with current locale: " << std::endl; errorstream << "Failed to restart with current locale: " << std::endl;
errorstream << buffer; errorstream << buffer;
errorstream << "Expect language to be broken!" << std::endl; errorstream << "Expect language to be broken!" << std::endl;
errorstream << "*******************************************************" << std::endl; errorstream << "*******************************************************" << std::endl;
} }
else {
exit(0);
}
} }
#else #else
errorstream << "*******************************************************" << std::endl; errorstream << "*******************************************************" << std::endl;
errorstream << "Can't apply locale workaround for server!" << std::endl; errorstream << "Can't apply locale workaround for server!" << std::endl;
errorstream << "Expect language to be broken!" << std::endl; errorstream << "Expect language to be broken!" << std::endl;
errorstream << "*******************************************************" << std::endl; errorstream << "*******************************************************" << std::endl;
#endif #endif
setlocale(LC_ALL,configured_language.c_str()); setlocale(LC_ALL, configured_language.c_str());
#else // Mingw #else // Mingw
char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char)); _putenv(("LANGUAGE=" + configured_language).c_str());
strcat(lang_str, "LANGUAGE=");
strcat(lang_str, configured_language.c_str());
putenv(lang_str);
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
#endif // ifndef _WIN32 #endif // ifndef _WIN32
} }

View File

@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifdef _MSC_VER #ifdef _MSC_VER
void init_gettext(const char *path, const std::string &configured_language, void init_gettext(const char *path, const std::string &configured_language,
int argc, char** argv); int argc, const char *argv[]);
#else #else
void init_gettext(const char *path, const std::string &configured_language); void init_gettext(const char *path, const std::string &configured_language);
#endif #endif