editor_translation_parser.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*************************************************************************/
  2. /* editor_translation_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/file_access.h"
  34. #include "core/templates/set.h"
  35. EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
  36. Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
  37. if (!get_script_instance())
  38. return ERR_UNAVAILABLE;
  39. if (get_script_instance()->has_method("parse_file")) {
  40. Array ids;
  41. Array ids_ctx_plural;
  42. get_script_instance()->call("parse_file", p_path, ids, ids_ctx_plural);
  43. // Add user's extracted translatable messages.
  44. for (int i = 0; i < ids.size(); i++) {
  45. r_ids->append(ids[i]);
  46. }
  47. // Add user's collected translatable messages with context or plurals.
  48. for (int i = 0; i < ids_ctx_plural.size(); i++) {
  49. Array arr = ids_ctx_plural[i];
  50. 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\"]");
  51. Vector<String> id_ctx_plural;
  52. id_ctx_plural.push_back(arr[0]);
  53. id_ctx_plural.push_back(arr[1]);
  54. id_ctx_plural.push_back(arr[2]);
  55. r_ids_ctx_plural->append(id_ctx_plural);
  56. }
  57. return OK;
  58. } else {
  59. ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined.");
  60. return ERR_UNAVAILABLE;
  61. }
  62. }
  63. void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  64. if (!get_script_instance())
  65. return;
  66. if (get_script_instance()->has_method("get_recognized_extensions")) {
  67. Array extensions = get_script_instance()->call("get_recognized_extensions");
  68. for (int i = 0; i < extensions.size(); i++) {
  69. r_extensions->push_back(extensions[i]);
  70. }
  71. } else {
  72. ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
  73. }
  74. }
  75. void EditorTranslationParserPlugin::_bind_methods() {
  76. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::NIL, "parse_file", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::ARRAY, "msgids"), PropertyInfo(Variant::ARRAY, "msgids_context_plural")));
  77. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::ARRAY, "get_recognized_extensions"));
  78. }
  79. /////////////////////////
  80. void EditorTranslationParser::get_recognized_extensions(List<String> *r_extensions) const {
  81. Set<String> extensions;
  82. List<String> temp;
  83. for (int i = 0; i < standard_parsers.size(); i++) {
  84. standard_parsers[i]->get_recognized_extensions(&temp);
  85. }
  86. for (int i = 0; i < custom_parsers.size(); i++) {
  87. custom_parsers[i]->get_recognized_extensions(&temp);
  88. }
  89. // Remove duplicates.
  90. for (int i = 0; i < temp.size(); i++) {
  91. extensions.insert(temp[i]);
  92. }
  93. for (auto E = extensions.front(); E; E = E->next()) {
  94. r_extensions->push_back(E->get());
  95. }
  96. }
  97. bool EditorTranslationParser::can_parse(const String &p_extension) const {
  98. List<String> extensions;
  99. get_recognized_extensions(&extensions);
  100. for (int i = 0; i < extensions.size(); i++) {
  101. if (p_extension == extensions[i]) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const String &p_extension) const {
  108. // Consider user-defined parsers first.
  109. for (int i = 0; i < custom_parsers.size(); i++) {
  110. List<String> temp;
  111. custom_parsers[i]->get_recognized_extensions(&temp);
  112. for (int j = 0; j < temp.size(); j++) {
  113. if (temp[j] == p_extension) {
  114. return custom_parsers[i];
  115. }
  116. }
  117. }
  118. for (int i = 0; i < standard_parsers.size(); i++) {
  119. List<String> temp;
  120. standard_parsers[i]->get_recognized_extensions(&temp);
  121. for (int j = 0; j < temp.size(); j++) {
  122. if (temp[j] == p_extension) {
  123. return standard_parsers[i];
  124. }
  125. }
  126. }
  127. WARN_PRINT("No translation parser available for \"" + p_extension + "\" extension.");
  128. return nullptr;
  129. }
  130. void EditorTranslationParser::add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  131. if (p_type == ParserType::STANDARD) {
  132. standard_parsers.push_back(p_parser);
  133. } else if (p_type == ParserType::CUSTOM) {
  134. custom_parsers.push_back(p_parser);
  135. }
  136. }
  137. void EditorTranslationParser::remove_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  138. if (p_type == ParserType::STANDARD) {
  139. standard_parsers.erase(p_parser);
  140. } else if (p_type == ParserType::CUSTOM) {
  141. custom_parsers.erase(p_parser);
  142. }
  143. }
  144. void EditorTranslationParser::clean_parsers() {
  145. standard_parsers.clear();
  146. custom_parsers.clear();
  147. }
  148. EditorTranslationParser *EditorTranslationParser::get_singleton() {
  149. if (!singleton) {
  150. singleton = memnew(EditorTranslationParser);
  151. }
  152. return singleton;
  153. }
  154. EditorTranslationParser::EditorTranslationParser() {
  155. }
  156. EditorTranslationParser::~EditorTranslationParser() {
  157. memdelete(singleton);
  158. singleton = nullptr;
  159. }