gettext.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "porting.h"
  19. #include "util/string.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. inline std::string strgettext(const char *str)
  40. {
  41. // We must check here that is not an empty string to avoid trying to translate it
  42. return str[0] ? gettext(str) : "";
  43. }
  44. inline std::string strgettext(const std::string &str)
  45. {
  46. return strgettext(str.c_str());
  47. }
  48. inline std::wstring wstrgettext(const char *str)
  49. {
  50. return utf8_to_wide(strgettext(str));
  51. }
  52. inline std::wstring wstrgettext(const std::string &str)
  53. {
  54. return wstrgettext(str.c_str());
  55. }
  56. /**
  57. * Returns translated string with format args applied
  58. *
  59. * @tparam Args Template parameter for format args
  60. * @param src Translation source string
  61. * @param args Variable format args
  62. * @return translated string
  63. */
  64. template <typename ...Args>
  65. inline std::wstring fwgettext(const char *src, Args&&... args)
  66. {
  67. wchar_t buf[255];
  68. swprintf(buf, sizeof(buf) / sizeof(wchar_t), wstrgettext(src).c_str(),
  69. std::forward<Args>(args)...);
  70. return std::wstring(buf);
  71. }
  72. /**
  73. * Returns translated string with format args applied
  74. *
  75. * @tparam Args Template parameter for format args
  76. * @param format Translation source string
  77. * @param args Variable format args
  78. * @return translated string.
  79. */
  80. template <typename ...Args>
  81. inline std::string fmtgettext(const char *format, Args&&... args)
  82. {
  83. std::string buf;
  84. std::size_t buf_size = 256;
  85. buf.resize(buf_size);
  86. format = gettext(format);
  87. int len = porting::mt_snprintf(&buf[0], buf_size, format, std::forward<Args>(args)...);
  88. if (len <= 0) throw std::runtime_error("gettext format error: " + std::string(format));
  89. if ((size_t)len >= buf.size()) {
  90. buf.resize(len+1); // extra null byte
  91. porting::mt_snprintf(&buf[0], buf.size(), format, std::forward<Args>(args)...);
  92. }
  93. buf.resize(len); // remove null bytes
  94. return buf;
  95. }