translation.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************/
  2. /* translation.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_H
  31. #define TRANSLATION_H
  32. #include "core/io/resource.h"
  33. #include "core/object/gdvirtual.gen.inc"
  34. class Translation : public Resource {
  35. GDCLASS(Translation, Resource);
  36. OBJ_SAVE_TYPE(Translation);
  37. RES_BASE_EXTENSION("translation");
  38. String locale = "en";
  39. HashMap<StringName, StringName> translation_map;
  40. virtual Vector<String> _get_message_list() const;
  41. virtual Dictionary _get_messages() const;
  42. virtual void _set_messages(const Dictionary &p_messages);
  43. void _notify_translation_changed_if_applies();
  44. protected:
  45. static void _bind_methods();
  46. #ifndef DISABLE_DEPRECATED
  47. static void _bind_compatibility_methods();
  48. #endif
  49. GDVIRTUAL2RC(StringName, _get_message, StringName, StringName);
  50. GDVIRTUAL4RC(StringName, _get_plural_message, StringName, StringName, int, StringName);
  51. public:
  52. void set_locale(const String &p_locale);
  53. _FORCE_INLINE_ String get_locale() const { return locale; }
  54. virtual void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "");
  55. virtual void add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context = "");
  56. virtual StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const; //overridable for other implementations
  57. virtual StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const;
  58. virtual void erase_message(const StringName &p_src_text, const StringName &p_context = "");
  59. virtual void get_message_list(List<StringName> *r_messages) const;
  60. virtual int get_message_count() const;
  61. virtual Vector<String> get_translated_message_list() const;
  62. Translation() {}
  63. };
  64. class TranslationServer : public Object {
  65. GDCLASS(TranslationServer, Object);
  66. String locale = "en";
  67. String fallback;
  68. HashSet<Ref<Translation>> translations;
  69. Ref<Translation> tool_translation;
  70. Ref<Translation> property_translation;
  71. Ref<Translation> doc_translation;
  72. Ref<Translation> extractable_translation;
  73. bool enabled = true;
  74. bool pseudolocalization_enabled = false;
  75. bool pseudolocalization_accents_enabled = false;
  76. bool pseudolocalization_double_vowels_enabled = false;
  77. bool pseudolocalization_fake_bidi_enabled = false;
  78. bool pseudolocalization_override_enabled = false;
  79. bool pseudolocalization_skip_placeholders_enabled = false;
  80. bool editor_pseudolocalization = false;
  81. float expansion_ratio = 0.0;
  82. String pseudolocalization_prefix;
  83. String pseudolocalization_suffix;
  84. StringName tool_pseudolocalize(const StringName &p_message) const;
  85. String get_override_string(String &p_message) const;
  86. String double_vowels(String &p_message) const;
  87. String replace_with_accented_string(String &p_message) const;
  88. String wrap_with_fakebidi_characters(String &p_message) const;
  89. String add_padding(const String &p_message, int p_length) const;
  90. const char32_t *get_accented_version(char32_t p_character) const;
  91. bool is_placeholder(String &p_message, int p_index) const;
  92. static TranslationServer *singleton;
  93. bool _load_translations(const String &p_from);
  94. String _standardize_locale(const String &p_locale, bool p_add_defaults) const;
  95. 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;
  96. static void _bind_methods();
  97. #ifndef DISABLE_DEPRECATED
  98. static void _bind_compatibility_methods();
  99. #endif
  100. struct LocaleScriptInfo {
  101. String name;
  102. String script;
  103. String default_country;
  104. HashSet<String> supported_countries;
  105. };
  106. static Vector<LocaleScriptInfo> locale_script_info;
  107. static HashMap<String, String> language_map;
  108. static HashMap<String, String> script_map;
  109. static HashMap<String, String> locale_rename_map;
  110. static HashMap<String, String> country_name_map;
  111. static HashMap<String, String> country_rename_map;
  112. static HashMap<String, String> variant_map;
  113. void init_locale_info();
  114. public:
  115. _FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
  116. void set_enabled(bool p_enabled) { enabled = p_enabled; }
  117. _FORCE_INLINE_ bool is_enabled() const { return enabled; }
  118. void set_locale(const String &p_locale);
  119. String get_locale() const;
  120. Ref<Translation> get_translation_object(const String &p_locale);
  121. Vector<String> get_all_languages() const;
  122. String get_language_name(const String &p_language) const;
  123. Vector<String> get_all_scripts() const;
  124. String get_script_name(const String &p_script) const;
  125. Vector<String> get_all_countries() const;
  126. String get_country_name(const String &p_country) const;
  127. String get_locale_name(const String &p_locale) const;
  128. PackedStringArray get_loaded_locales() const;
  129. void add_translation(const Ref<Translation> &p_translation);
  130. void remove_translation(const Ref<Translation> &p_translation);
  131. StringName translate(const StringName &p_message, const StringName &p_context = "") const;
  132. StringName translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  133. StringName pseudolocalize(const StringName &p_message) const;
  134. bool is_pseudolocalization_enabled() const;
  135. void set_pseudolocalization_enabled(bool p_enabled);
  136. void set_editor_pseudolocalization(bool p_enabled);
  137. void reload_pseudolocalization();
  138. String standardize_locale(const String &p_locale) const;
  139. int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
  140. String get_tool_locale();
  141. void set_tool_translation(const Ref<Translation> &p_translation);
  142. Ref<Translation> get_tool_translation() const;
  143. StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
  144. StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  145. void set_property_translation(const Ref<Translation> &p_translation);
  146. StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
  147. void set_doc_translation(const Ref<Translation> &p_translation);
  148. StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
  149. StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  150. void set_extractable_translation(const Ref<Translation> &p_translation);
  151. StringName extractable_translate(const StringName &p_message, const StringName &p_context = "") const;
  152. StringName extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  153. void setup();
  154. void clear();
  155. void load_translations();
  156. #ifdef TOOLS_ENABLED
  157. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  158. #endif // TOOLS_ENABLED
  159. TranslationServer();
  160. };
  161. #endif // TRANSLATION_H