editor_translation_parser.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }
  40. if (get_script_instance()->has_method("parse_file")) {
  41. Array ids;
  42. Array ids_ctx_plural;
  43. get_script_instance()->call("parse_file", p_path, ids, ids_ctx_plural);
  44. // Add user's extracted translatable messages.
  45. for (int i = 0; i < ids.size(); i++) {
  46. r_ids->append(ids[i]);
  47. }
  48. // Add user's collected translatable messages with context or plurals.
  49. for (int i = 0; i < ids_ctx_plural.size(); i++) {
  50. Array arr = ids_ctx_plural[i];
  51. 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\"]");
  52. Vector<String> id_ctx_plural;
  53. id_ctx_plural.push_back(arr[0]);
  54. id_ctx_plural.push_back(arr[1]);
  55. id_ctx_plural.push_back(arr[2]);
  56. r_ids_ctx_plural->append(id_ctx_plural);
  57. }
  58. return OK;
  59. } else {
  60. ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined.");
  61. return ERR_UNAVAILABLE;
  62. }
  63. }
  64. void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  65. if (!get_script_instance()) {
  66. return;
  67. }
  68. if (get_script_instance()->has_method("get_recognized_extensions")) {
  69. Array extensions = get_script_instance()->call("get_recognized_extensions");
  70. for (int i = 0; i < extensions.size(); i++) {
  71. r_extensions->push_back(extensions[i]);
  72. }
  73. } else {
  74. ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
  75. }
  76. }
  77. void EditorTranslationParserPlugin::_bind_methods() {
  78. 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")));
  79. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::ARRAY, "get_recognized_extensions"));
  80. }
  81. /////////////////////////
  82. void EditorTranslationParser::get_recognized_extensions(List<String> *r_extensions) const {
  83. Set<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 (int i = 0; i < temp.size(); i++) {
  93. extensions.insert(temp[i]);
  94. }
  95. for (auto E = extensions.front(); E; E = E->next()) {
  96. r_extensions->push_back(E->get());
  97. }
  98. }
  99. bool EditorTranslationParser::can_parse(const String &p_extension) const {
  100. List<String> extensions;
  101. get_recognized_extensions(&extensions);
  102. for (int i = 0; i < extensions.size(); i++) {
  103. if (p_extension == extensions[i]) {
  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 (int j = 0; j < temp.size(); j++) {
  115. if (temp[j] == 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 (int j = 0; j < temp.size(); j++) {
  124. if (temp[j] == 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. }
  158. EditorTranslationParser::~EditorTranslationParser() {
  159. memdelete(singleton);
  160. singleton = nullptr;
  161. }