gettext.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "gettext.h"
  20. #include "util/string.h"
  21. #include "porting.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. static 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. static const char* MSVC_LocaleLookup(const char* raw_shortname)
  66. {
  67. /* NULL is used to read locale only so we need to return it too */
  68. if (raw_shortname == NULL) return NULL;
  69. std::string shortname(raw_shortname);
  70. if (shortname == "C") return "C";
  71. if (shortname == "") return "";
  72. static std::string last_raw_value = "";
  73. static std::string last_full_name = "";
  74. static bool first_use = true;
  75. if (last_raw_value == shortname) {
  76. return last_full_name.c_str();
  77. }
  78. if (first_use) {
  79. EnumSystemLocalesA(UpdateLocaleCallback, LCID_SUPPORTED | LCID_ALTERNATE_SORTS);
  80. first_use = false;
  81. }
  82. last_raw_value = shortname;
  83. auto key = utf8_to_wide(shortname);
  84. if (glb_supported_locales.find(key) != glb_supported_locales.end()) {
  85. last_full_name = wide_to_utf8(glb_supported_locales[key]);
  86. return last_full_name.c_str();
  87. }
  88. /* empty string is system default */
  89. errorstream << "MSVC_LocaleLookup: unsupported locale: \"" << shortname
  90. << "\" switching to system default!" << std::endl;
  91. return "";
  92. }
  93. static void MSVC_LocaleWorkaround(int argc, char* argv[])
  94. {
  95. errorstream << "MSVC localization workaround active. "
  96. "Restarting " PROJECT_NAME_C " in a new environment!" << std::endl;
  97. std::string parameters;
  98. for (int i = 1; i < argc; i++) {
  99. if (i > 1)
  100. parameters += ' ';
  101. parameters += porting::QuoteArgv(argv[i]);
  102. }
  103. char *ptr_parameters = nullptr;
  104. if (!parameters.empty())
  105. ptr_parameters = &parameters[0];
  106. // Allow calling without an extension
  107. std::string app_name = argv[0];
  108. if (app_name.compare(app_name.size() - 4, 4, ".exe") != 0)
  109. app_name += ".exe";
  110. STARTUPINFO startup_info = {};
  111. PROCESS_INFORMATION process_info = {};
  112. bool success = CreateProcess(app_name.c_str(), ptr_parameters,
  113. NULL, NULL, false, DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
  114. NULL, NULL, &startup_info, &process_info);
  115. if (success) {
  116. exit(0);
  117. // NOTREACHED
  118. } else {
  119. char buffer[1024];
  120. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
  121. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), buffer,
  122. sizeof(buffer) - 1, NULL);
  123. errorstream << "*******************************************************" << std::endl;
  124. errorstream << "CMD: " << app_name << std::endl;
  125. errorstream << "Failed to restart with current locale: " << std::endl;
  126. errorstream << buffer;
  127. errorstream << "Expect language to be broken!" << std::endl;
  128. errorstream << "*******************************************************" << std::endl;
  129. }
  130. }
  131. #endif
  132. /******************************************************************************/
  133. void init_gettext(const char *path, const std::string &configured_language,
  134. int argc, char *argv[])
  135. {
  136. #if USE_GETTEXT
  137. // First, try to set user override environment
  138. if (!configured_language.empty()) {
  139. // Set LANGUAGE which overrides all others, see
  140. // <https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html>
  141. #ifndef _MSC_VER
  142. setenv("LANGUAGE", configured_language.c_str(), 1);
  143. // Reload locale with changed environment
  144. setlocale(LC_ALL, "");
  145. #else
  146. std::string current_language;
  147. const char *env_lang = getenv("LANGUAGE");
  148. if (env_lang)
  149. current_language = env_lang;
  150. setenv("LANGUAGE", configured_language.c_str(), 1);
  151. SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
  152. #ifndef SERVER
  153. // Hack to force gettext to see the right environment
  154. if (current_language != configured_language)
  155. MSVC_LocaleWorkaround(argc, argv);
  156. #else
  157. errorstream << "*******************************************************" << std::endl;
  158. errorstream << "Can't apply locale workaround for server!" << std::endl;
  159. errorstream << "Expect language to be broken!" << std::endl;
  160. errorstream << "*******************************************************" << std::endl;
  161. #endif
  162. setlocale(LC_ALL, configured_language.c_str());
  163. #endif // ifdef _MSC_VER
  164. } else {
  165. /* set current system default locale */
  166. setlocale(LC_ALL, "");
  167. }
  168. #if defined(_WIN32)
  169. if (getenv("LANGUAGE") != 0) {
  170. setlocale(LC_ALL, getenv("LANGUAGE"));
  171. }
  172. #ifdef _MSC_VER
  173. else if (getenv("LANG") != 0) {
  174. setlocale(LC_ALL, getenv("LANG"));
  175. }
  176. #endif
  177. #endif
  178. std::string name = lowercase(PROJECT_NAME);
  179. infostream << "Gettext: domainname=\"" << name
  180. << "\" path=\"" << path << "\"" << std::endl;
  181. bindtextdomain(name.c_str(), path);
  182. textdomain(name.c_str());
  183. #ifdef _WIN32
  184. // set character encoding
  185. char *tdomain = textdomain(nullptr);
  186. assert(tdomain);
  187. if (tdomain)
  188. bind_textdomain_codeset(tdomain, "UTF-8");
  189. #endif
  190. #else
  191. /* set current system default locale */
  192. setlocale(LC_ALL, "");
  193. #endif // if USE_GETTEXT
  194. /* no matter what locale is used we need number format to be "C" */
  195. /* to ensure formspec parameters are evaluated correctly! */
  196. setlocale(LC_NUMERIC, "C");
  197. infostream << "Message locale is now set to: "
  198. << setlocale(LC_ALL, 0) << std::endl;
  199. }