@@ -118,106 +118,90 @@ const char* MSVC_LocaleLookup(const char* raw_shortname) {
118
118
119
119
/* *****************************************************************************/
120
120
#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[])
122
123
#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)
124
125
#endif
126
+ {
125
127
#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 () ) {
128
130
#ifndef _WIN32
129
- /* add user specified locale to environment */
131
+ // Add user specified locale to environment
130
132
setenv (" LANGUAGE" , configured_language.c_str (), 1 );
131
133
132
- /* reload locale with changed environment */
134
+ // Reload locale with changed environment
133
135
setlocale (LC_ALL, " " );
134
136
#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;
144
141
145
- SetEnvironmentVariableA (" LANGUAGE" ,configured_language.c_str ());
142
+ _putenv ((" LANGUAGE=" + configured_language).c_str ());
143
+ SetEnvironmentVariableA (" LANGUAGE" , configured_language.c_str ());
146
144
147
145
#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
+
162
157
parameters += argv[i];
163
158
}
164
159
165
- const char * ptr_parameters = 0 ;
160
+ const char * ptr_parameters = NULL ;
166
161
167
- if (parameters != " " ) {
162
+ if (!parameters. empty ())
168
163
ptr_parameters = parameters.c_str ();
169
- }
170
164
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 };
176
172
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 {
187
181
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
+
195
187
errorstream << " *******************************************************" << std::endl;
196
- errorstream << " CMD: " << appname << std::endl;
188
+ errorstream << " CMD: " << app_name << std::endl;
197
189
errorstream << " Failed to restart with current locale: " << std::endl;
198
190
errorstream << buffer;
199
191
errorstream << " Expect language to be broken!" << std::endl;
200
192
errorstream << " *******************************************************" << std::endl;
201
193
}
202
- else {
203
- exit (0 );
204
- }
205
194
}
206
195
#else
207
196
errorstream << " *******************************************************" << std::endl;
208
197
errorstream << " Can't apply locale workaround for server!" << std::endl;
209
198
errorstream << " Expect language to be broken!" << std::endl;
210
199
errorstream << " *******************************************************" << std::endl;
211
-
212
200
#endif
213
201
214
- setlocale (LC_ALL,configured_language.c_str ());
202
+ setlocale (LC_ALL, configured_language.c_str ());
215
203
#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 ());
221
205
setlocale (LC_ALL, " " );
222
206
#endif // ifndef _WIN32
223
207
}
0 commit comments