translation_po.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**************************************************************************/
  2. /* translation_po.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TRANSLATION_PO_H
  31. #define TRANSLATION_PO_H
  32. //#define DEBUG_TRANSLATION_PO
  33. #include "core/math/expression.h"
  34. #include "core/string/translation.h"
  35. class TranslationPO : public Translation {
  36. GDCLASS(TranslationPO, Translation);
  37. // TLDR: Maps context to a list of source strings and translated strings. In PO terms, maps msgctxt to a list of msgid and msgstr.
  38. // The first key corresponds to context, and the second key (of the contained HashMap) corresponds to source string.
  39. // The value Vector<StringName> in the second map stores the translated strings. Index 0, 1, 2 matches msgstr[0], msgstr[1], msgstr[2]... in the case of plurals.
  40. // Otherwise index 0 matches to msgstr in a singular translation.
  41. // Strings without context have "" as first key.
  42. HashMap<StringName, HashMap<StringName, Vector<StringName>>> translation_map;
  43. int plural_forms = 0; // 0 means no "Plural-Forms" is given in the PO header file. The min for all languages is 1.
  44. String plural_rule;
  45. // Cache temporary variables related to _get_plural_index() to make it faster
  46. class EQNode : public RefCounted {
  47. public:
  48. String regex;
  49. Ref<EQNode> left;
  50. Ref<EQNode> right;
  51. };
  52. Ref<EQNode> equi_tests;
  53. int _find_unquoted(const String &p_src, char32_t p_chr) const;
  54. int _eq_test(const Ref<EQNode> &p_node, const Variant &p_result) const;
  55. Vector<String> input_name;
  56. mutable Ref<Expression> expr;
  57. mutable Array input_val;
  58. mutable StringName last_plural_key;
  59. mutable StringName last_plural_context;
  60. mutable int last_plural_n = -1; // Set it to an impossible value at the beginning.
  61. mutable int last_plural_mapped_index = 0;
  62. void _cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node);
  63. int _get_plural_index(int p_n) const;
  64. Vector<String> _get_message_list() const override;
  65. Dictionary _get_messages() const override;
  66. void _set_messages(const Dictionary &p_messages) override;
  67. protected:
  68. static void _bind_methods();
  69. public:
  70. Vector<String> get_translated_message_list() const override;
  71. void get_message_list(List<StringName> *r_messages) const override;
  72. int get_message_count() const override;
  73. void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "") override;
  74. void add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context = "") override;
  75. StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const override;
  76. StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const override;
  77. void erase_message(const StringName &p_src_text, const StringName &p_context = "") override;
  78. void set_plural_rule(const String &p_plural_rule);
  79. int get_plural_forms() const;
  80. String get_plural_rule() const;
  81. #ifdef DEBUG_TRANSLATION_PO
  82. void print_translation_map();
  83. #endif
  84. TranslationPO() {}
  85. };
  86. #endif // TRANSLATION_PO_H