gettext.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef GETTEXT_HEADER
  17. #define GETTEXT_HEADER
  18. #include "config.h" // for USE_GETTEXT
  19. #include "log.h"
  20. #if USE_GETTEXT
  21. #include <libintl.h>
  22. #else
  23. #define gettext(String) String
  24. #endif
  25. #define _(String) gettext(String)
  26. #define gettext_noop(String) String
  27. #define N_(String) gettext_noop (String)
  28. #if defined(_WIN32)
  29. #define WIN32_LEAN_AND_MEAN
  30. #ifndef _WIN32_WINNT
  31. #define _WIN32_WINNT 0x0501
  32. #endif
  33. #include <windows.h>
  34. #endif // #if defined(_WIN32)
  35. #ifdef _MSC_VER
  36. void init_gettext(const char *path,std::string configured_language,int argc, char** argv);
  37. #else
  38. void init_gettext(const char *path,std::string configured_language);
  39. #endif
  40. extern std::wstring narrow_to_wide(const std::string& mbs);
  41. #include "util/numeric.h"
  42. /******************************************************************************/
  43. inline wchar_t* chartowchar_t(const char *str)
  44. {
  45. wchar_t* nstr = 0;
  46. #if defined(_WIN32)
  47. int nResult = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, 0, 0 );
  48. if( nResult == 0 )
  49. {
  50. errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
  51. }
  52. else
  53. {
  54. nstr = new wchar_t[nResult];
  55. MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult );
  56. }
  57. #else
  58. size_t l = strlen(str);
  59. nstr = new wchar_t[l+1];
  60. std::wstring intermediate = narrow_to_wide(str);
  61. memset(nstr, 0, (l+1)*sizeof(wchar_t));
  62. memcpy(nstr, intermediate.c_str(), l*sizeof(wchar_t));
  63. #endif
  64. return nstr;
  65. }
  66. /******************************************************************************/
  67. inline wchar_t* wgettext(const char *str)
  68. {
  69. return chartowchar_t(gettext(str));
  70. }
  71. /******************************************************************************/
  72. inline std::wstring wstrgettext(std::string text) {
  73. wchar_t* wlabel = wgettext(text.c_str());
  74. std::wstring out = (std::wstring)wlabel;
  75. delete[] wlabel;
  76. return out;
  77. }
  78. #endif