gettext.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SuperTux
  2. // Copyright (C) 2006 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_UTIL_GETTEXT_HPP
  17. #define HEADER_SUPERTUX_UTIL_GETTEXT_HPP
  18. #include <tinygettext/tinygettext.hpp>
  19. #include <memory>
  20. extern std::unique_ptr<tinygettext::DictionaryManager> g_dictionary_manager;
  21. /*
  22. * If you need to do a nontrivial substitution of values into a pattern, use
  23. * fmt::format rather than an ad-hoc concatenation. That way, translators can
  24. * translate the format string as a whole (and even rearrange the values if
  25. * necessary with "%1$s"-style codes) instead of multiple pieces. Patterns like
  26. * "Label: {}" with only one string piece are a borderline case where
  27. * fmt::format is not really necessary.
  28. *
  29. * http://www.mihai-nita.net/article.php?artID=20060430a
  30. * https://fmt.dev/latest/syntax.html
  31. *
  32. * Bad:
  33. * std::string greeting = _("Hello ") + name + _("!");
  34. * std::cout << _("Hello ") << name << _("!");
  35. * Good:
  36. * #include <fmt/format.h>
  37. * std::string greeting = fmt::format(fmt::runtime(_("Hello {}!")), name);
  38. * std::cout << fmt::format(fmt::runtime(_("Hello {}!")), name);
  39. *
  40. * If you need singular and plural forms use __ instead of _ and fmt::format
  41. * if necessary.
  42. *
  43. * https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
  44. *
  45. * Bad:
  46. * std::cout << _("You collected ") << num << _(" coins");
  47. * Good:
  48. * #include <fmt/format.h>
  49. * std::cout << fmt::format(fmt::runtime(__("You collected {} coin",
  50. * "You collected {} coins", num)),
  51. * num));
  52. */
  53. static inline std::string _(const std::string& message)
  54. {
  55. if (g_dictionary_manager)
  56. {
  57. return g_dictionary_manager->get_dictionary().translate(message);
  58. }
  59. else
  60. {
  61. return message;
  62. }
  63. }
  64. static inline std::string __(const std::string& message,
  65. const std::string& message_plural, int num)
  66. {
  67. if (g_dictionary_manager)
  68. {
  69. return g_dictionary_manager->get_dictionary().translate_plural(message,
  70. message_plural, num);
  71. }
  72. else if (num == 1)
  73. {
  74. return message;
  75. }
  76. else
  77. {
  78. return message_plural;
  79. }
  80. }
  81. #endif
  82. /* EOF */