editor_translation_parser.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**************************************************************************/
  2. /* editor_translation_parser.cpp */
  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. #include "editor_translation_parser.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/object/script_language.h"
  33. #include "core/templates/hash_set.h"
  34. EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
  35. Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {
  36. TypedArray<PackedStringArray> ret;
  37. if (GDVIRTUAL_CALL(_parse_file, p_path, ret)) {
  38. // Copy over entries directly.
  39. for (const PackedStringArray translation : ret) {
  40. r_translations->push_back(translation);
  41. }
  42. return OK;
  43. }
  44. #ifndef DISABLE_DEPRECATED
  45. TypedArray<String> ids;
  46. TypedArray<Array> ids_ctx_plural;
  47. if (GDVIRTUAL_CALL(_parse_file_bind_compat_99297, p_path, ids, ids_ctx_plural)) {
  48. // Add user's extracted translatable messages.
  49. for (int i = 0; i < ids.size(); i++) {
  50. r_translations->push_back({ ids[i] });
  51. }
  52. // Add user's collected translatable messages with context or plurals.
  53. for (int i = 0; i < ids_ctx_plural.size(); i++) {
  54. Array arr = ids_ctx_plural[i];
  55. ERR_FAIL_COND_V_MSG(arr.size() != 3, ERR_INVALID_DATA, "Array entries written into `msgids_context_plural` in `_parse_file` method should have the form [\"message\", \"context\", \"plural message\"]");
  56. r_translations->push_back({ arr[0], arr[1], arr[2] });
  57. }
  58. return OK;
  59. }
  60. #endif // DISABLE_DEPRECATED
  61. ERR_PRINT("Custom translation parser plugin's \"_parse_file\" is undefined.");
  62. return ERR_UNAVAILABLE;
  63. }
  64. void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  65. Vector<String> extensions;
  66. if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
  67. for (int i = 0; i < extensions.size(); i++) {
  68. r_extensions->push_back(extensions[i]);
  69. }
  70. } else {
  71. ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
  72. }
  73. }
  74. void EditorTranslationParserPlugin::_bind_methods() {
  75. GDVIRTUAL_BIND(_parse_file, "path");
  76. GDVIRTUAL_BIND(_get_recognized_extensions);
  77. #ifndef DISABLE_DEPRECATED
  78. GDVIRTUAL_BIND_COMPAT(_parse_file_bind_compat_99297, "path", "msgids", "msgids_context_plural");
  79. #endif
  80. }
  81. /////////////////////////
  82. void EditorTranslationParser::get_recognized_extensions(List<String> *r_extensions) const {
  83. HashSet<String> extensions;
  84. List<String> temp;
  85. for (int i = 0; i < standard_parsers.size(); i++) {
  86. standard_parsers[i]->get_recognized_extensions(&temp);
  87. }
  88. for (int i = 0; i < custom_parsers.size(); i++) {
  89. custom_parsers[i]->get_recognized_extensions(&temp);
  90. }
  91. // Remove duplicates.
  92. for (const String &E : temp) {
  93. extensions.insert(E);
  94. }
  95. for (const String &E : extensions) {
  96. r_extensions->push_back(E);
  97. }
  98. }
  99. bool EditorTranslationParser::can_parse(const String &p_extension) const {
  100. List<String> extensions;
  101. get_recognized_extensions(&extensions);
  102. for (const String &extension : extensions) {
  103. if (p_extension == extension) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const String &p_extension) const {
  110. // Consider user-defined parsers first.
  111. for (int i = 0; i < custom_parsers.size(); i++) {
  112. List<String> temp;
  113. custom_parsers[i]->get_recognized_extensions(&temp);
  114. for (const String &E : temp) {
  115. if (E == p_extension) {
  116. return custom_parsers[i];
  117. }
  118. }
  119. }
  120. for (int i = 0; i < standard_parsers.size(); i++) {
  121. List<String> temp;
  122. standard_parsers[i]->get_recognized_extensions(&temp);
  123. for (const String &E : temp) {
  124. if (E == p_extension) {
  125. return standard_parsers[i];
  126. }
  127. }
  128. }
  129. WARN_PRINT("No translation parser available for \"" + p_extension + "\" extension.");
  130. return nullptr;
  131. }
  132. void EditorTranslationParser::add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  133. if (p_type == ParserType::STANDARD) {
  134. standard_parsers.push_back(p_parser);
  135. } else if (p_type == ParserType::CUSTOM) {
  136. custom_parsers.push_back(p_parser);
  137. }
  138. }
  139. void EditorTranslationParser::remove_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  140. if (p_type == ParserType::STANDARD) {
  141. standard_parsers.erase(p_parser);
  142. } else if (p_type == ParserType::CUSTOM) {
  143. custom_parsers.erase(p_parser);
  144. }
  145. }
  146. void EditorTranslationParser::clean_parsers() {
  147. standard_parsers.clear();
  148. custom_parsers.clear();
  149. }
  150. EditorTranslationParser *EditorTranslationParser::get_singleton() {
  151. if (!singleton) {
  152. singleton = memnew(EditorTranslationParser);
  153. }
  154. return singleton;
  155. }
  156. EditorTranslationParser::~EditorTranslationParser() {
  157. memdelete(singleton);
  158. singleton = nullptr;
  159. }