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