Skip to content

Commit e067cea

Browse files
ShadowNinjakwolekr
authored andcommittedOct 15, 2015
Clean up gettext initialization
1 parent 7b8d372 commit e067cea

File tree

2 files changed

+51
-67
lines changed

2 files changed

+51
-67
lines changed
 

Diff for: ‎src/gettext.cpp

+50-66
Original file line numberDiff line numberDiff line change
@@ -118,106 +118,90 @@ const char* MSVC_LocaleLookup(const char* raw_shortname) {
118118

119119
/******************************************************************************/
120120
#ifdef _MSC_VER
121-
void init_gettext(const char *path, const std::string &configured_language, int argc, char** argv) {
121+
void init_gettext(const char *path, const std::string &configured_language,
122+
int argc, const char *argv[])
122123
#else
123-
void init_gettext(const char *path, const std::string &configured_language) {
124+
void init_gettext(const char *path, const std::string &configured_language)
124125
#endif
126+
{
125127
#if USE_GETTEXT
126-
/** first try to set user override environment **/
127-
if (configured_language.length() != 0) {
128+
// First, try to set user override environment
129+
if (!configured_language.empty()) {
128130
#ifndef _WIN32
129-
/* add user specified locale to environment */
131+
// Add user specified locale to environment
130132
setenv("LANGUAGE", configured_language.c_str(), 1);
131133

132-
/* reload locale with changed environment */
134+
// Reload locale with changed environment
133135
setlocale(LC_ALL, "");
134136
#elif defined(_MSC_VER)
135-
std::string current_language_var("");
136-
if (getenv("LANGUAGE") != 0) {
137-
current_language_var = std::string(getenv("LANGUAGE"));
138-
}
139-
140-
char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
141-
strcat(lang_str, "LANGUAGE=");
142-
strcat(lang_str, configured_language.c_str());
143-
putenv(lang_str);
137+
std::string current_language;
138+
const char *env_lang = getenv("LANGUAGE");
139+
if (env_lang)
140+
current_language = env_lang;
144141

145-
SetEnvironmentVariableA("LANGUAGE",configured_language.c_str());
142+
_putenv(("LANGUAGE=" + configured_language).c_str());
143+
SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
146144

147145
#ifndef SERVER
148-
//very very dirty workaround to force gettext to see the right environment
149-
if (current_language_var != configured_language) {
150-
STARTUPINFO startupinfo;
151-
PROCESS_INFORMATION processinfo;
152-
memset(&startupinfo, 0, sizeof(startupinfo));
153-
memset(&processinfo, 0, sizeof(processinfo));
154-
errorstream << "MSVC localization workaround active restating minetest in new environment!" << std::endl;
155-
156-
std::string parameters = "";
157-
158-
for (unsigned int i=1;i < argc; i++) {
159-
if (parameters != "") {
160-
parameters += " ";
161-
}
146+
// Hack to force gettext to see the right environment
147+
if (current_language != configured_language) {
148+
errorstream << "MSVC localization workaround active. "
149+
"Restarting " PROJECT_NAME_C " in a new environment!" << std::endl;
150+
151+
std::string parameters;
152+
153+
for (unsigned int i = 1; i < argc; i++) {
154+
if (!parameters.empty())
155+
parameters += ' ';
156+
162157
parameters += argv[i];
163158
}
164159

165-
const char* ptr_parameters = 0;
160+
const char *ptr_parameters = NULL;
166161

167-
if (parameters != "") {
162+
if (!parameters.empty())
168163
ptr_parameters = parameters.c_str();
169-
}
170164

171-
/** users may start by short name in commandline without extention **/
172-
std::string appname = argv[0];
173-
if (appname.substr(appname.length() - 4) != ".exe") {
174-
appname += ".exe";
175-
}
165+
// Allow calling without an extension
166+
std::string app_name = argv[0];
167+
if (app_name.compare(appname.size() - 4, 4, ".exe") != 0)
168+
app_name += ".exe";
169+
170+
STARTUPINFO startup_info = {0};
171+
PROCESS_INFORMATION process_info = {0};
176172

177-
if (!CreateProcess(appname.c_str(),
178-
(char*) ptr_parameters,
179-
NULL,
180-
NULL,
181-
false,
182-
DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
183-
NULL,
184-
NULL,
185-
&startupinfo,
186-
&processinfo)) {
173+
bool success = CreateProcess(app_name.c_str(), (char *)ptr_parameters,
174+
NULL, NULL, false, DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
175+
NULL, NULL, &startup_info, &process_info);
176+
177+
if (success) {
178+
exit(0);
179+
// NOTREACHED
180+
} else {
187181
char buffer[1024];
188-
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
189-
NULL,
190-
GetLastError(),
191-
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
192-
buffer,
193-
sizeof(buffer)-1,
194-
NULL);
182+
183+
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
184+
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), buffer,
185+
sizeof(buffer) - 1, NULL);
186+
195187
errorstream << "*******************************************************" << std::endl;
196-
errorstream << "CMD: " << appname << std::endl;
188+
errorstream << "CMD: " << app_name << std::endl;
197189
errorstream << "Failed to restart with current locale: " << std::endl;
198190
errorstream << buffer;
199191
errorstream << "Expect language to be broken!" << std::endl;
200192
errorstream << "*******************************************************" << std::endl;
201193
}
202-
else {
203-
exit(0);
204-
}
205194
}
206195
#else
207196
errorstream << "*******************************************************" << std::endl;
208197
errorstream << "Can't apply locale workaround for server!" << std::endl;
209198
errorstream << "Expect language to be broken!" << std::endl;
210199
errorstream << "*******************************************************" << std::endl;
211-
212200
#endif
213201

214-
setlocale(LC_ALL,configured_language.c_str());
202+
setlocale(LC_ALL, configured_language.c_str());
215203
#else // Mingw
216-
char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
217-
strcat(lang_str, "LANGUAGE=");
218-
strcat(lang_str, configured_language.c_str());
219-
putenv(lang_str);
220-
204+
_putenv(("LANGUAGE=" + configured_language).c_str());
221205
setlocale(LC_ALL, "");
222206
#endif // ifndef _WIN32
223207
}

Diff for: ‎src/gettext.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3434

3535
#ifdef _MSC_VER
3636
void init_gettext(const char *path, const std::string &configured_language,
37-
int argc, char** argv);
37+
int argc, const char *argv[]);
3838
#else
3939
void init_gettext(const char *path, const std::string &configured_language);
4040
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.