gettext.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <string.h>
  18. #include <iostream>
  19. #include <stdlib.h>
  20. #include "gettext.h"
  21. #include "util/string.h"
  22. #include "log.h"
  23. #if USE_GETTEXT && defined(_MSC_VER)
  24. #include <WinNls.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(narrow_to_wide(shortname)) != glb_supported_locales.end()) {
  83. last_full_name = wide_to_narrow(glb_supported_locales[narrow_to_wide(shortname)]);
  84. return last_full_name.c_str();
  85. }
  86. /* empty string is system default */
  87. errorstream << "MSVC_LocaleLookup: unsupported locale: \"" << shortname
  88. << "\" switching to system default!" << std::endl;
  89. return "";
  90. }
  91. #endif
  92. /******************************************************************************/
  93. #ifdef _MSC_VER
  94. void init_gettext(const char *path, const std::string &configured_language, int argc, char** argv) {
  95. #else
  96. void init_gettext(const char *path, const std::string &configured_language) {
  97. #endif
  98. #if USE_GETTEXT
  99. /** first try to set user override environment **/
  100. if (configured_language.length() != 0) {
  101. #ifndef _WIN32
  102. /* add user specified locale to environment */
  103. setenv("LANGUAGE", configured_language.c_str(), 1);
  104. /* reload locale with changed environment */
  105. setlocale(LC_ALL, "");
  106. #elif defined(_MSC_VER)
  107. std::string current_language_var("");
  108. if (getenv("LANGUAGE") != 0) {
  109. current_language_var = std::string(getenv("LANGUAGE"));
  110. }
  111. char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
  112. strcat(lang_str, "LANGUAGE=");
  113. strcat(lang_str, configured_language.c_str());
  114. putenv(lang_str);
  115. SetEnvironmentVariableA("LANGUAGE",configured_language.c_str());
  116. #ifndef SERVER
  117. //very very dirty workaround to force gettext to see the right environment
  118. if (current_language_var != configured_language) {
  119. STARTUPINFO startupinfo;
  120. PROCESS_INFORMATION processinfo;
  121. memset(&startupinfo,0,sizeof(startupinfo));
  122. memset(&processinfo,0,sizeof(processinfo));
  123. errorstream << "MSVC localization workaround active restating minetest in new environment!" << std::endl;
  124. std::string parameters = "";
  125. for (unsigned int i=1;i < argc; i++) {
  126. if (parameters != "") {
  127. parameters += " ";
  128. }
  129. parameters += argv[i];
  130. }
  131. const char* ptr_parameters = 0;
  132. if (parameters != "") {
  133. ptr_parameters = parameters.c_str();
  134. }
  135. /** users may start by short name in commandline without extention **/
  136. std::string appname = argv[0];
  137. if (appname.substr(appname.length() -4) != ".exe") {
  138. appname += ".exe";
  139. }
  140. if (!CreateProcess(appname.c_str(),
  141. (char*) ptr_parameters,
  142. NULL,
  143. NULL,
  144. false,
  145. DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
  146. NULL,
  147. NULL,
  148. &startupinfo,
  149. &processinfo)) {
  150. char buffer[1024];
  151. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  152. NULL,
  153. GetLastError(),
  154. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  155. buffer,
  156. sizeof(buffer)-1,
  157. NULL);
  158. errorstream << "*******************************************************" << std::endl;
  159. errorstream << "CMD: " << appname << std::endl;
  160. errorstream << "Failed to restart with current locale: " << std::endl;
  161. errorstream << buffer;
  162. errorstream << "Expect language to be broken!" << std::endl;
  163. errorstream << "*******************************************************" << std::endl;
  164. }
  165. else {
  166. exit(0);
  167. }
  168. #else
  169. errorstream << "*******************************************************" << std::endl;
  170. errorstream << "Can't apply locale workaround for server!" << std::endl;
  171. errorstream << "Expect language to be broken!" << std::endl;
  172. errorstream << "*******************************************************" << std::endl;
  173. #endif
  174. }
  175. setlocale(LC_ALL,configured_language.c_str());
  176. #else // Mingw
  177. char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
  178. strcat(lang_str, "LANGUAGE=");
  179. strcat(lang_str, configured_language.c_str());
  180. putenv(lang_str);
  181. setlocale(LC_ALL, "");
  182. #endif // ifndef _WIN32
  183. }
  184. else {
  185. /* set current system default locale */
  186. setlocale(LC_ALL, "");
  187. }
  188. #if defined(_WIN32)
  189. if (getenv("LANGUAGE") != 0) {
  190. setlocale(LC_ALL, getenv("LANGUAGE"));
  191. }
  192. #ifdef _MSC_VER
  193. else if (getenv("LANG") != 0) {
  194. setlocale(LC_ALL, getenv("LANG"));
  195. }
  196. #endif
  197. #endif
  198. bindtextdomain(PROJECT_NAME, path);
  199. textdomain(PROJECT_NAME);
  200. #if defined(_WIN32)
  201. // Set character encoding for Win32
  202. char *tdomain = textdomain( (char *) NULL );
  203. if( tdomain == NULL )
  204. {
  205. errorstream << "Warning: domainname parameter is the null pointer" <<
  206. ", default domain is not set" << std::endl;
  207. tdomain = (char *) "messages";
  208. }
  209. /* char *codeset = */bind_textdomain_codeset( tdomain, "UTF-8" );
  210. //errorstream << "Gettext debug: domainname = " << tdomain << "; codeset = "<< codeset << std::endl;
  211. #endif // defined(_WIN32)
  212. #else
  213. /* set current system default locale */
  214. setlocale(LC_ALL, "");
  215. #endif // if USE_GETTEXT
  216. /* no matter what locale is used we need number format to be "C" */
  217. /* to ensure formspec parameters are evaluated correct! */
  218. setlocale(LC_NUMERIC,"C");
  219. infostream << "Message locale is now set to: "
  220. << setlocale(LC_ALL, 0) << std::endl;
  221. }