translation.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*************************************************************************/
  2. /* translation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_H
  31. #define TRANSLATION_H
  32. #include "core/io/resource.h"
  33. class Translation : public Resource {
  34. GDCLASS(Translation, Resource);
  35. OBJ_SAVE_TYPE(Translation);
  36. RES_BASE_EXTENSION("translation");
  37. String locale = "en";
  38. Map<StringName, StringName> translation_map;
  39. virtual Vector<String> _get_message_list() const;
  40. virtual Dictionary _get_messages() const;
  41. virtual void _set_messages(const Dictionary &p_messages);
  42. protected:
  43. static void _bind_methods();
  44. public:
  45. void set_locale(const String &p_locale);
  46. _FORCE_INLINE_ String get_locale() const { return locale; }
  47. virtual void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "");
  48. virtual void add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context = "");
  49. virtual StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const; //overridable for other implementations
  50. virtual StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const;
  51. virtual void erase_message(const StringName &p_src_text, const StringName &p_context = "");
  52. virtual void get_message_list(List<StringName> *r_messages) const;
  53. virtual int get_message_count() const;
  54. Translation() {}
  55. };
  56. class TranslationServer : public Object {
  57. GDCLASS(TranslationServer, Object);
  58. String locale = "en";
  59. String fallback;
  60. Set<Ref<Translation>> translations;
  61. Ref<Translation> tool_translation;
  62. Ref<Translation> doc_translation;
  63. Map<String, String> locale_name_map;
  64. bool enabled = true;
  65. static TranslationServer *singleton;
  66. bool _load_translations(const String &p_from);
  67. StringName _get_message_from_translations(const StringName &p_message, const StringName &p_context, const String &p_locale, bool plural, const String &p_message_plural = "", int p_n = 0) const;
  68. static void _bind_methods();
  69. public:
  70. _FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
  71. void set_enabled(bool p_enabled) { enabled = p_enabled; }
  72. _FORCE_INLINE_ bool is_enabled() const { return enabled; }
  73. void set_locale(const String &p_locale);
  74. String get_locale() const;
  75. Ref<Translation> get_translation_object(const String &p_locale);
  76. String get_locale_name(const String &p_locale) const;
  77. Array get_loaded_locales() const;
  78. void add_translation(const Ref<Translation> &p_translation);
  79. void remove_translation(const Ref<Translation> &p_translation);
  80. StringName translate(const StringName &p_message, const StringName &p_context = "") const;
  81. StringName translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  82. static Vector<String> get_all_locales();
  83. static Vector<String> get_all_locale_names();
  84. static bool is_locale_valid(const String &p_locale);
  85. static String standardize_locale(const String &p_locale);
  86. static String get_language_code(const String &p_locale);
  87. String get_tool_locale();
  88. void set_tool_translation(const Ref<Translation> &p_translation);
  89. Ref<Translation> get_tool_translation() const;
  90. StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
  91. StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  92. void set_doc_translation(const Ref<Translation> &p_translation);
  93. StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
  94. StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  95. void setup();
  96. void clear();
  97. void load_translations();
  98. TranslationServer();
  99. };
  100. #endif // TRANSLATION_H