translation.h 8.3 KB

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