translation_server.h 6.4 KB

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