gettext.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #if USE_GETTEXT && defined(_MSC_VER)
  23. #include <WinNls.h>
  24. #include <map>
  25. #include <direct.h>
  26. #include "filesys.h"
  27. #define setlocale(category,localename) \
  28. setlocale(category,MSVC_LocaleLookup(localename))
  29. static std::map<std::wstring,std::wstring> glb_supported_locales;
  30. /******************************************************************************/
  31. BOOL CALLBACK UpdateLocaleCallback(LPTSTR pStr)
  32. {
  33. char* endptr = 0;
  34. int LOCALEID = strtol(pStr,&endptr,16);
  35. wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
  36. memset(buffer,0,sizeof(buffer));
  37. if (GetLocaleInfoW(
  38. LOCALEID,
  39. LOCALE_SISO639LANGNAME,
  40. buffer,
  41. LOCALE_NAME_MAX_LENGTH)) {
  42. std::wstring name = buffer;
  43. memset(buffer,0,sizeof(buffer));
  44. GetLocaleInfoW(
  45. LOCALEID,
  46. LOCALE_SISO3166CTRYNAME,
  47. buffer,
  48. LOCALE_NAME_MAX_LENGTH);
  49. std::wstring country = buffer;
  50. memset(buffer,0,sizeof(buffer));
  51. GetLocaleInfoW(
  52. LOCALEID,
  53. LOCALE_SENGLISHLANGUAGENAME,
  54. buffer,
  55. LOCALE_NAME_MAX_LENGTH);
  56. std::wstring languagename = buffer;
  57. /* set both short and long variant */
  58. glb_supported_locales[name] = languagename;
  59. glb_supported_locales[name + L"_" + country] = languagename;
  60. }
  61. return true;
  62. }
  63. /******************************************************************************/
  64. const char* MSVC_LocaleLookup(const char* raw_shortname) {
  65. /* NULL is used to read locale only so we need to return it too */
  66. if (raw_shortname == NULL) return NULL;
  67. std::string shortname(raw_shortname);
  68. if (shortname == "C") return "C";
  69. if (shortname == "") return "";
  70. static std::string last_raw_value = "";
  71. static std::string last_full_name = "";
  72. static bool first_use = true;
  73. if (last_raw_value == shortname) {
  74. return last_full_name.c_str();
  75. }
  76. if (first_use) {
  77. EnumSystemLocalesA(UpdateLocaleCallback,LCID_SUPPORTED | LCID_ALTERNATE_SORTS);
  78. first_use = false;
  79. }
  80. last_raw_value = shortname;
  81. if (glb_supported_locales.find(narrow_to_wide(shortname)) != glb_supported_locales.end()) {
  82. last_full_name = wide_to_narrow(glb_supported_locales[narrow_to_wide(shortname)]);
  83. return last_full_name.c_str();
  84. }
  85. /* empty string is system default */
  86. errorstream << "MSVC_LocaleLookup: unsupported locale: \"" << shortname
  87. << "\" switching to system default!" << std::endl;
  88. return "";
  89. }
  90. #endif
  91. /******************************************************************************/
  92. #ifdef _MSC_VER
  93. void init_gettext(const char *path,std::string configured_language,int argc, char** argv) {
  94. #else
  95. void init_gettext(const char *path,std::string configured_language) {
  96. #endif
  97. #if USE_GETTEXT
  98. /** first try to set user override environment **/
  99. if (configured_language.length() != 0) {
  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_var("");
  107. if (getenv("LANGUAGE") != 0) {
  108. current_language_var = std::string(getenv("LANGUAGE"));
  109. }
  110. char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
  111. strcat(lang_str, "LANGUAGE=");
  112. strcat(lang_str, configured_language.c_str());
  113. putenv(lang_str);
  114. SetEnvironmentVariableA("LANGUAGE",configured_language.c_str());
  115. #ifndef SERVER
  116. //very very dirty workaround to force gettext to see the right environment
  117. if (current_language_var != configured_language) {
  118. STARTUPINFO startupinfo;
  119. PROCESS_INFORMATION processinfo;
  120. memset(&startupinfo,0,sizeof(startupinfo));
  121. memset(&processinfo,0,sizeof(processinfo));
  122. errorstream << "MSVC localization workaround active restating minetest in new environment!" << std::endl;
  123. std::string parameters = "";
  124. for (unsigned int i=1;i < argc; i++) {
  125. if (parameters != "") {
  126. parameters += " ";
  127. }
  128. parameters += argv[i];
  129. }
  130. const char* ptr_parameters = 0;
  131. if (parameters != "") {
  132. ptr_parameters = parameters.c_str();
  133. }
  134. /** users may start by short name in commandline without extention **/
  135. std::string appname = argv[0];
  136. if (appname.substr(appname.length() -4) != ".exe") {
  137. appname += ".exe";
  138. }
  139. if (!CreateProcess(appname.c_str(),
  140. (char*) ptr_parameters,
  141. NULL,
  142. NULL,
  143. false,
  144. DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT,
  145. NULL,
  146. NULL,
  147. &startupinfo,
  148. &processinfo)) {
  149. char buffer[1024];
  150. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  151. NULL,
  152. GetLastError(),
  153. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  154. buffer,
  155. sizeof(buffer)-1,
  156. NULL);
  157. errorstream << "*******************************************************" << std::endl;
  158. errorstream << "CMD: " << appname << std::endl;
  159. errorstream << "Failed to restart with current locale: " << std::endl;
  160. errorstream << buffer;
  161. errorstream << "Expect language to be broken!" << std::endl;
  162. errorstream << "*******************************************************" << std::endl;
  163. }
  164. else {
  165. exit(0);
  166. }
  167. #else
  168. errorstream << "*******************************************************" << std::endl;
  169. errorstream << "Can't apply locale workaround for server!" << std::endl;
  170. errorstream << "Expect language to be broken!" << std::endl;
  171. errorstream << "*******************************************************" << std::endl;
  172. #endif
  173. }
  174. setlocale(LC_ALL,configured_language.c_str());
  175. #else // Mingw
  176. char *lang_str = (char*)calloc(10 + configured_language.length(), sizeof(char));
  177. strcat(lang_str, "LANGUAGE=");
  178. strcat(lang_str, configured_language.c_str());
  179. putenv(lang_str);
  180. setlocale(LC_ALL, "");
  181. #endif // ifndef _WIN32
  182. }
  183. else {
  184. /* set current system default locale */
  185. setlocale(LC_ALL, "");
  186. }
  187. #if defined(_WIN32)
  188. if (getenv("LANGUAGE") != 0) {
  189. setlocale(LC_ALL, getenv("LANGUAGE"));
  190. }
  191. #ifdef _MSC_VER
  192. else if (getenv("LANG") != 0) {
  193. setlocale(LC_ALL, getenv("LANG"));
  194. }
  195. #endif
  196. #endif
  197. bindtextdomain(PROJECT_NAME, path);
  198. textdomain(PROJECT_NAME);
  199. #if defined(_WIN32)
  200. // Set character encoding for Win32
  201. char *tdomain = textdomain( (char *) NULL );
  202. if( tdomain == NULL )
  203. {
  204. errorstream << "Warning: domainname parameter is the null pointer" <<
  205. ", default domain is not set" << std::endl;
  206. tdomain = (char *) "messages";
  207. }
  208. /* char *codeset = */bind_textdomain_codeset( tdomain, "UTF-8" );
  209. //errorstream << "Gettext debug: domainname = " << tdomain << "; codeset = "<< codeset << std::endl;
  210. #endif // defined(_WIN32)
  211. #else
  212. /* set current system default locale */
  213. setlocale(LC_ALL, "");
  214. #endif // if USE_GETTEXT
  215. /* no matter what locale is used we need number format to be "C" */
  216. /* to ensure formspec parameters are evaluated correct! */
  217. setlocale(LC_NUMERIC,"C");
  218. infostream << "Message locale is now set to: "
  219. << setlocale(LC_ALL,0) << std::endl;
  220. }