gettext.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <string>
  17. #include <cstring>
  18. #include <iostream>
  19. #include <cstdlib>
  20. #include "gettext.h"
  21. #include "util/string.h"
  22. #include "log.h"
  23. #if USE_GETTEXT && defined(_MSC_VER)
  24. #include <windows.h>
  25. #include <map>
  26. #include <direct.h>
  27. #include "filesys.h"
  28. #define setlocale(category, localename) \
  29. setlocale(category, MSVC_LocaleLookup(localename))
  30. static std::map<std::wstring, std::wstring> glb_supported_locales;
  31. /******************************************************************************/
  32. BOOL CALLBACK UpdateLocaleCallback(LPTSTR pStr)
  33. {
  34. char* endptr = 0;
  35. int LOCALEID = strtol(pStr, &endptr,16);
  36. wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
  37. memset(buffer, 0, sizeof(buffer));
  38. if (GetLocaleInfoW(
  39. LOCALEID,
  40. LOCALE_SISO639LANGNAME,
  41. buffer,
  42. LOCALE_NAME_MAX_LENGTH)) {
  43. std::wstring name = buffer;
  44. memset(buffer, 0, sizeof(buffer));
  45. GetLocaleInfoW(
  46. LOCALEID,
  47. LOCALE_SISO3166CTRYNAME,
  48. buffer,
  49. LOCALE_NAME_MAX_LENGTH);
  50. std::wstring country = buffer;
  51. memset(buffer, 0, sizeof(buffer));
  52. GetLocaleInfoW(
  53. LOCALEID,
  54. LOCALE_SENGLISHLANGUAGENAME,
  55. buffer,
  56. LOCALE_NAME_MAX_LENGTH);
  57. std::wstring languagename = buffer;
  58. /* set both short and long variant */
  59. glb_supported_locales[name] = languagename;
  60. glb_supported_locales[name + L"_" + country] = languagename;
  61. }
  62. return true;
  63. }
  64. /******************************************************************************/
  65. const char* MSVC_LocaleLookup(const char* raw_shortname) {
  66. /* NULL is used to read locale only so we need to return it too */
  67. if (raw_shortname == NULL) return NULL;
  68. std::string shortname(raw_shortname);
  69. if (shortname == "C") return "C";
  70. if (shortname == "") return "";
  71. static std::string last_raw_value = "";
  72. static std::string last_full_name = "";
  73. static bool first_use = true;
  74. if (last_raw_value == shortname) {
  75. return last_full_name.c_str();
  76. }
  77. if (first_use) {
  78. EnumSystemLocalesA(UpdateLocaleCallback, LCID_SUPPORTED | LCID_ALTERNATE_SORTS);
  79. first_use = false;
  80. }
  81. last_raw_value = shortname;
  82. if (glb_supported_locales.find(utf8_to_wide(shortname)) != glb_supported_locales.end()) {
  83. last_full_name = wide_to_utf8(
  84. glb_supported_locales[utf8_to_wide(shortname)]);
  85. return last_full_name.c_str();
  86. }
  87. /* empty string is system default */
  88. errorstream << "MSVC_LocaleLookup: unsupported locale: \"" << shortname
  89. << "\" switching to system default!" << std::endl;
  90. return "";
  91. }
  92. #endif
  93. /******************************************************************************/
  94. void init_gettext(const char *path, const std::string &configured_language,
  95. int argc, char *argv[])
  96. {
  97. #if USE_GETTEXT
  98. // First, try to set user override environment
  99. if (!configured_language.empty()) {
  100. #ifndef _WIN32
  101. // Add user specified locale to environment
  102. setenv("LANGUAGE", configured_language.c_str(), 1);
  103. #ifdef __ANDROID__
  104. setenv("LANG", configured_language.c_str(), 1);
  105. #endif
  106. // Reload locale with changed environment
  107. setlocale(LC_ALL, "");
  108. #elif defined(_MSC_VER)
  109. std::string current_language;
  110. const char *env_lang = getenv("LANGUAGE");
  111. if (env_lang)
  112. current_language = env_lang;
  113. _putenv(("LANGUAGE=" + configured_language).c_str());
  114. SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
  115. #ifndef SERVER
  116. // Hack to force gettext to see the right environment
  117. if (current_language != configured_language) {
  118. errorstream << "MSVC localization workaround active. "
  119. "Restarting " PROJECT_NAME_C " in a new environment!" << std::endl;
  120. std::string parameters;
  121. for (int i = 1; i < argc; i++) {
  122. if (i > 1)
  123. parameters += ' ';
  124. parameters += porting::QuoteArgv(argv[i]);
  125. }
  126. char *ptr_parameters = nullptr;
  127. if (!parameters.empty())
  128. ptr_parameters = &parameters[0];
  129. // Allow calling without an extension
  130. std::string app_name = argv[0];
  131. if (app_name.compare(app_name.size() - 4, 4, ".exe") != 0)
  132. app_name += ".exe";
  133. STARTUPINFO startup_info = {};
  134. PROCESS_INFORMATION process_info = {};
  135. bool success = CreateProcess(app_name.c_str(), ptr_parameters,
  136. NULL, NULL, false, DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
  137. NULL, NULL, &startup_info, &process_info);
  138. if (success) {
  139. exit(0);
  140. // NOTREACHED
  141. } else {
  142. char buffer[1024];
  143. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
  144. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), buffer,
  145. sizeof(buffer) - 1, NULL);
  146. errorstream << "*******************************************************" << std::endl;
  147. errorstream << "CMD: " << app_name << std::endl;
  148. errorstream << "Failed to restart with current locale: " << std::endl;
  149. errorstream << buffer;
  150. errorstream << "Expect language to be broken!" << std::endl;
  151. errorstream << "*******************************************************" << std::endl;
  152. }
  153. }
  154. #else
  155. errorstream << "*******************************************************" << std::endl;
  156. errorstream << "Can't apply locale workaround for server!" << std::endl;
  157. errorstream << "Expect language to be broken!" << std::endl;
  158. errorstream << "*******************************************************" << std::endl;
  159. #endif
  160. setlocale(LC_ALL, configured_language.c_str());
  161. #else // Mingw
  162. _putenv(("LANGUAGE=" + configured_language).c_str());
  163. setlocale(LC_ALL, "");
  164. #endif // ifndef _WIN32
  165. }
  166. else {
  167. /* set current system default locale */
  168. setlocale(LC_ALL, "");
  169. }
  170. #if defined(_WIN32)
  171. if (getenv("LANGUAGE") != 0) {
  172. setlocale(LC_ALL, getenv("LANGUAGE"));
  173. }
  174. #ifdef _MSC_VER
  175. else if (getenv("LANG") != 0) {
  176. setlocale(LC_ALL, getenv("LANG"));
  177. }
  178. #endif
  179. #endif
  180. std::string name = lowercase(PROJECT_NAME);
  181. infostream << "Gettext: domainname=\"" << name
  182. << "\" path=\"" << path << "\"" << std::endl;
  183. bindtextdomain(name.c_str(), path);
  184. textdomain(name.c_str());
  185. #if defined(_WIN32)
  186. // Set character encoding for Win32
  187. char *tdomain = textdomain( (char *) NULL );
  188. if( tdomain == NULL )
  189. {
  190. errorstream << "Warning: domainname parameter is the null pointer" <<
  191. ", default domain is not set" << std::endl;
  192. tdomain = (char *) "messages";
  193. }
  194. /* char *codeset = */bind_textdomain_codeset( tdomain, "UTF-8" );
  195. //errorstream << "Gettext debug: domainname = " << tdomain << "; codeset = "<< codeset << std::endl;
  196. #endif // defined(_WIN32)
  197. #else
  198. /* set current system default locale */
  199. setlocale(LC_ALL, "");
  200. #endif // if USE_GETTEXT
  201. /* no matter what locale is used we need number format to be "C" */
  202. /* to ensure formspec parameters are evaluated correct! */
  203. setlocale(LC_NUMERIC, "C");
  204. infostream << "Message locale is now set to: "
  205. << setlocale(LC_ALL, 0) << std::endl;
  206. }