translation_server.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* translation_server.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_SERVER_H
  31. #define TRANSLATION_SERVER_H
  32. #include "core/string/translation.h"
  33. #include "core/string/translation_domain.h"
  34. class TranslationServer : public Object {
  35. GDCLASS(TranslationServer, Object);
  36. String locale = "en";
  37. String fallback;
  38. Ref<TranslationDomain> main_domain;
  39. Ref<TranslationDomain> editor_domain;
  40. Ref<TranslationDomain> property_domain;
  41. Ref<TranslationDomain> doc_domain;
  42. HashMap<StringName, Ref<TranslationDomain>> custom_domains;
  43. mutable HashMap<String, int> locale_compare_cache;
  44. bool enabled = true;
  45. static inline TranslationServer *singleton = nullptr;
  46. bool _load_translations(const String &p_from);
  47. static void _bind_methods();
  48. #ifndef DISABLE_DEPRECATED
  49. String _standardize_locale_bind_compat_98972(const String &p_locale) const;
  50. static void _bind_compatibility_methods();
  51. #endif
  52. struct LocaleScriptInfo {
  53. String name;
  54. String script;
  55. String default_country;
  56. HashSet<String> supported_countries;
  57. };
  58. static Vector<LocaleScriptInfo> locale_script_info;
  59. struct Locale {
  60. String language;
  61. String script;
  62. String country;
  63. String variant;
  64. bool operator==(const Locale &p_locale) const {
  65. return (p_locale.language == language) &&
  66. (p_locale.script == script) &&
  67. (p_locale.country == country) &&
  68. (p_locale.variant == variant);
  69. }
  70. operator String() const;
  71. Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults);
  72. };
  73. static HashMap<String, String> language_map;
  74. static HashMap<String, String> script_map;
  75. static HashMap<String, String> locale_rename_map;
  76. static HashMap<String, String> country_name_map;
  77. static HashMap<String, String> country_rename_map;
  78. static HashMap<String, String> variant_map;
  79. void init_locale_info();
  80. public:
  81. _FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
  82. Ref<TranslationDomain> get_editor_domain() const { return editor_domain; }
  83. void set_enabled(bool p_enabled) { enabled = p_enabled; }
  84. _FORCE_INLINE_ bool is_enabled() const { return enabled; }
  85. void set_locale(const String &p_locale);
  86. String get_locale() const;
  87. String get_fallback_locale() const;
  88. Ref<Translation> get_translation_object(const String &p_locale);
  89. Vector<String> get_all_languages() const;
  90. String get_language_name(const String &p_language) const;
  91. Vector<String> get_all_scripts() const;
  92. String get_script_name(const String &p_script) const;
  93. Vector<String> get_all_countries() const;
  94. String get_country_name(const String &p_country) const;
  95. String get_locale_name(const String &p_locale) const;
  96. PackedStringArray get_loaded_locales() const;
  97. void add_translation(const Ref<Translation> &p_translation);
  98. void remove_translation(const Ref<Translation> &p_translation);
  99. StringName translate(const StringName &p_message, const StringName &p_context = "") const;
  100. StringName translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  101. StringName pseudolocalize(const StringName &p_message) const;
  102. bool is_pseudolocalization_enabled() const;
  103. void set_pseudolocalization_enabled(bool p_enabled);
  104. void reload_pseudolocalization();
  105. String standardize_locale(const String &p_locale, bool p_add_defaults = false) const;
  106. int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
  107. String get_tool_locale();
  108. StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
  109. StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  110. StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
  111. StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
  112. StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  113. bool has_domain(const StringName &p_domain) const;
  114. Ref<TranslationDomain> get_or_add_domain(const StringName &p_domain);
  115. void remove_domain(const StringName &p_domain);
  116. void setup();
  117. void clear();
  118. void load_translations();
  119. #ifdef TOOLS_ENABLED
  120. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  121. #endif // TOOLS_ENABLED
  122. TranslationServer();
  123. };
  124. #endif // TRANSLATION_SERVER_H