gettext.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // Reload locale with changed environment
  104. setlocale(LC_ALL, "");
  105. #elif defined(_MSC_VER)
  106. std::string current_language;
  107. const char *env_lang = getenv("LANGUAGE");
  108. if (env_lang)
  109. current_language = env_lang;
  110. _putenv(("LANGUAGE=" + configured_language).c_str());
  111. SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
  112. #ifndef SERVER
  113. // Hack to force gettext to see the right environment
  114. if (current_language != configured_language) {
  115. errorstream << "MSVC localization workaround active. "
  116. "Restarting " PROJECT_NAME_C " in a new environment!" << std::endl;
  117. std::string parameters;
  118. for (unsigned int i = 1; i < argc; i++) {
  119. if (!parameters.empty())
  120. parameters += ' ';
  121. parameters += argv[i];
  122. }
  123. const char *ptr_parameters = NULL;
  124. if (!parameters.empty())
  125. ptr_parameters = parameters.c_str();
  126. // Allow calling without an extension
  127. std::string app_name = argv[0];
  128. if (app_name.compare(app_name.size() - 4, 4, ".exe") != 0)
  129. app_name += ".exe";
  130. STARTUPINFO startup_info = {0};
  131. PROCESS_INFORMATION process_info = {0};
  132. bool success = CreateProcess(app_name.c_str(), (char *)ptr_parameters,
  133. NULL, NULL, false, DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
  134. NULL, NULL, &startup_info, &process_info);
  135. if (success) {
  136. exit(0);
  137. // NOTREACHED
  138. } else {
  139. char buffer[1024];
  140. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
  141. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), buffer,
  142. sizeof(buffer) - 1, NULL);
  143. errorstream << "*******************************************************" << std::endl;
  144. errorstream << "CMD: " << app_name << std::endl;
  145. errorstream << "Failed to restart with current locale: " << std::endl;
  146. errorstream << buffer;
  147. errorstream << "Expect language to be broken!" << std::endl;
  148. errorstream << "*******************************************************" << std::endl;
  149. }
  150. }
  151. #else
  152. errorstream << "*******************************************************" << std::endl;
  153. errorstream << "Can't apply locale workaround for server!" << std::endl;
  154. errorstream << "Expect language to be broken!" << std::endl;
  155. errorstream << "*******************************************************" << std::endl;
  156. #endif
  157. setlocale(LC_ALL, configured_language.c_str());
  158. #else // Mingw
  159. _putenv(("LANGUAGE=" + configured_language).c_str());
  160. setlocale(LC_ALL, "");
  161. #endif // ifndef _WIN32
  162. }
  163. else {
  164. /* set current system default locale */
  165. setlocale(LC_ALL, "");
  166. }
  167. #if defined(_WIN32)
  168. if (getenv("LANGUAGE") != 0) {
  169. setlocale(LC_ALL, getenv("LANGUAGE"));
  170. }
  171. #ifdef _MSC_VER
  172. else if (getenv("LANG") != 0) {
  173. setlocale(LC_ALL, getenv("LANG"));
  174. }
  175. #endif
  176. #endif
  177. static std::string name = lowercase(PROJECT_NAME);
  178. bindtextdomain(name.c_str(), path);
  179. textdomain(name.c_str());
  180. #if defined(_WIN32)
  181. // Set character encoding for Win32
  182. char *tdomain = textdomain( (char *) NULL );
  183. if( tdomain == NULL )
  184. {
  185. errorstream << "Warning: domainname parameter is the null pointer" <<
  186. ", default domain is not set" << std::endl;
  187. tdomain = (char *) "messages";
  188. }
  189. /* char *codeset = */bind_textdomain_codeset( tdomain, "UTF-8" );
  190. //errorstream << "Gettext debug: domainname = " << tdomain << "; codeset = "<< codeset << std::endl;
  191. #endif // defined(_WIN32)
  192. #else
  193. /* set current system default locale */
  194. setlocale(LC_ALL, "");
  195. #endif // if USE_GETTEXT
  196. /* no matter what locale is used we need number format to be "C" */
  197. /* to ensure formspec parameters are evaluated correct! */
  198. setlocale(LC_NUMERIC, "C");
  199. infostream << "Message locale is now set to: "
  200. << setlocale(LC_ALL, 0) << std::endl;
  201. }