gdscript_translation_parser_plugin.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*************************************************************************/
  2. /* gdscript_translation_parser_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "gdscript_translation_parser_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "modules/gdscript/gdscript.h"
  33. void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  34. GDScriptLanguage::get_singleton()->get_recognized_extensions(r_extensions);
  35. }
  36. Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_extracted_strings) {
  37. // Parse and match all GDScript function API that involves translation string.
  38. // E.g get_node("Label").text = "something", var test = tr("something"), "something" will be matched and collected.
  39. Error err;
  40. RES loaded_res = ResourceLoader::load(p_path, "", false, &err);
  41. if (err) {
  42. ERR_PRINT("Failed to load " + p_path);
  43. return err;
  44. }
  45. Ref<GDScript> gdscript = loaded_res;
  46. String source_code = gdscript->get_source_code();
  47. Vector<String> parsed_strings;
  48. // Search translation strings with RegEx.
  49. regex.clear();
  50. regex.compile(String("|").join(patterns));
  51. Array results = regex.search_all(source_code);
  52. _get_captured_strings(results, &parsed_strings);
  53. // Special handling for FileDialog.
  54. Vector<String> temp;
  55. _parse_file_dialog(source_code, &temp);
  56. parsed_strings.append_array(temp);
  57. // Filter out / and +
  58. String filter = "(?:\\\\\\n|\"[\\s\\\\]*\\+\\s*\")";
  59. regex.clear();
  60. regex.compile(filter);
  61. for (int i = 0; i < parsed_strings.size(); i++) {
  62. parsed_strings.set(i, regex.sub(parsed_strings[i], "", true));
  63. }
  64. r_extracted_strings->append_array(parsed_strings);
  65. return OK;
  66. }
  67. void GDScriptEditorTranslationParserPlugin::_parse_file_dialog(const String &p_source_code, Vector<String> *r_output) {
  68. // FileDialog API has the form .filters = PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"]).
  69. // First filter: Get "*.png ; PNG Images", "*.gd ; GDScript Files" from PackedStringArray.
  70. regex.clear();
  71. regex.compile(String("|").join(file_dialog_patterns));
  72. Array results = regex.search_all(p_source_code);
  73. Vector<String> temp;
  74. _get_captured_strings(results, &temp);
  75. String captured_strings = String(",").join(temp);
  76. // Second filter: Get the texts after semicolon from "*.png ; PNG Images","*.gd ; GDScript Files".
  77. String second_filter = "\"[^;]+;" + text + "\"";
  78. regex.clear();
  79. regex.compile(second_filter);
  80. results = regex.search_all(captured_strings);
  81. _get_captured_strings(results, r_output);
  82. for (int i = 0; i < r_output->size(); i++) {
  83. r_output->set(i, r_output->get(i).strip_edges());
  84. }
  85. }
  86. void GDScriptEditorTranslationParserPlugin::_get_captured_strings(const Array &p_results, Vector<String> *r_output) {
  87. Ref<RegExMatch> result;
  88. for (int i = 0; i < p_results.size(); i++) {
  89. result = p_results[i];
  90. for (int j = 0; j < result->get_group_count(); j++) {
  91. String s = result->get_string(j + 1);
  92. // Prevent reading text with only spaces.
  93. if (!s.strip_edges().empty()) {
  94. r_output->push_back(s);
  95. }
  96. }
  97. }
  98. }
  99. GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
  100. // Regex search pattern templates.
  101. // The extra complication in the regex pattern is to ensure that the matching works when users write over multiple lines, use tabs etc.
  102. const String dot = "\\.[\\s\\\\]*";
  103. const String str_assign_template = "[\\s\\\\]*=[\\s\\\\]*\"" + text + "\"";
  104. const String first_arg_template = "[\\s\\\\]*\\([\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
  105. const String second_arg_template = "[\\s\\\\]*\\([\\s\\S]+?,[\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
  106. // Common patterns.
  107. patterns.push_back("tr" + first_arg_template);
  108. patterns.push_back(dot + "text" + str_assign_template);
  109. patterns.push_back(dot + "placeholder_text" + str_assign_template);
  110. patterns.push_back(dot + "hint_tooltip" + str_assign_template);
  111. patterns.push_back(dot + "set_text" + first_arg_template);
  112. patterns.push_back(dot + "set_tooltip" + first_arg_template);
  113. patterns.push_back(dot + "set_placeholder" + first_arg_template);
  114. // Tabs and TabContainer API.
  115. patterns.push_back(dot + "set_tab_title" + second_arg_template);
  116. patterns.push_back(dot + "add_tab" + first_arg_template);
  117. // PopupMenu API.
  118. patterns.push_back(dot + "add_check_item" + first_arg_template);
  119. patterns.push_back(dot + "add_icon_check_item" + second_arg_template);
  120. patterns.push_back(dot + "add_icon_item" + second_arg_template);
  121. patterns.push_back(dot + "add_icon_radio_check_item" + second_arg_template);
  122. patterns.push_back(dot + "add_item" + first_arg_template);
  123. patterns.push_back(dot + "add_multistate_item" + first_arg_template);
  124. patterns.push_back(dot + "add_radio_check_item" + first_arg_template);
  125. patterns.push_back(dot + "add_separator" + first_arg_template);
  126. patterns.push_back(dot + "add_submenu_item" + first_arg_template);
  127. patterns.push_back(dot + "set_item_text" + second_arg_template);
  128. //patterns.push_back(dot + "set_item_tooltip" + second_arg_template); //no tr() behind this function. might be bug.
  129. // FileDialog API - special case.
  130. const String fd_text = "((?:[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*(?:\"[\\s\\\\]*\\+[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*)*\"[\\s\\\\]*,?)*)";
  131. const String packed_string_array = "[\\s\\\\]*PackedStringArray[\\s\\\\]*\\([\\s\\\\]*\\[" + fd_text + "\\][\\s\\\\]*\\)";
  132. file_dialog_patterns.push_back(dot + "add_filter[\\s\\\\]*\\(" + fd_text + "[\\s\\\\]*\\)");
  133. file_dialog_patterns.push_back(dot + "filters[\\s\\\\]*=" + packed_string_array);
  134. file_dialog_patterns.push_back(dot + "set_filters[\\s\\\\]*\\(" + packed_string_array + "[\\s\\\\]*\\)");
  135. }