gettext.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #pragma once
  17. #include "config.h" // for USE_GETTEXT
  18. #include <string>
  19. #include "porting.h"
  20. #if USE_GETTEXT
  21. #include <libintl.h>
  22. #else
  23. // In certain environments, some standard headers like <iomanip>
  24. // and <locale> include libintl.h. If libintl.h is included after
  25. // we define our gettext macro below, this causes a syntax error
  26. // at the declaration of the gettext function in libintl.h.
  27. // Fix this by including such a header before defining the macro.
  28. // See issue #4446.
  29. // Note that we can't include libintl.h directly since we're in
  30. // the USE_GETTEXT=0 case and can't assume that gettext is installed.
  31. #include <locale>
  32. #define gettext(String) String
  33. #endif
  34. #define _(String) gettext(String)
  35. #define gettext_noop(String) (String)
  36. #define N_(String) gettext_noop((String))
  37. void init_gettext(const char *path, const std::string &configured_language,
  38. int argc, char *argv[]);
  39. extern wchar_t *utf8_to_wide_c(const char *str);
  40. // The returned string must be freed using delete[]
  41. inline const wchar_t *wgettext(const char *str)
  42. {
  43. // We must check here that is not an empty string to avoid trying to translate it
  44. return str[0] ? utf8_to_wide_c(gettext(str)) : utf8_to_wide_c("");
  45. }
  46. inline std::string strgettext(const std::string &text)
  47. {
  48. return text.empty() ? "" : gettext(text.c_str());
  49. }
  50. /**
  51. * Returns translated string with format args applied
  52. *
  53. * @tparam Args Template parameter for format args
  54. * @param src Translation source string
  55. * @param args Variable format args
  56. * @return translated string
  57. */
  58. template <typename ...Args>
  59. inline std::wstring fwgettext(const char *src, Args&&... args)
  60. {
  61. wchar_t buf[255];
  62. const wchar_t* str = wgettext(src);
  63. swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, std::forward<Args>(args)...);
  64. delete[] str;
  65. return std::wstring(buf);
  66. }
  67. /**
  68. * Returns translated string with format args applied
  69. *
  70. * @tparam Args Template parameter for format args
  71. * @param format Translation source string
  72. * @param args Variable format args
  73. * @return translated string.
  74. */
  75. template <typename ...Args>
  76. inline std::string fmtgettext(const char *format, Args&&... args)
  77. {
  78. std::string buf;
  79. std::size_t buf_size = 256;
  80. buf.resize(buf_size);
  81. format = gettext(format);
  82. int len = porting::mt_snprintf(&buf[0], buf_size, format, std::forward<Args>(args)...);
  83. if (len <= 0) throw std::runtime_error("gettext format error: " + std::string(format));
  84. if ((size_t)len >= buf.size()) {
  85. buf.resize(len+1); // extra null byte
  86. porting::mt_snprintf(&buf[0], buf.size(), format, std::forward<Args>(args)...);
  87. }
  88. buf.resize(len); // remove null bytes
  89. return buf;
  90. }