gdscript_translation_parser_plugin.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
  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. // Special handling for tr and tr_n.
  58. _handle_tr_pattern(source_code, tr_pattern, r_ids_ctx_plural);
  59. _handle_tr_pattern(source_code, trn_pattern, r_ids_ctx_plural);
  60. // Filter out / and + used when user writes over multiple lines in GDScript
  61. String filter = "(?:\\\\\\n|\"[\\s\\\\]*\\+\\s*\")";
  62. regex.clear();
  63. regex.compile(filter);
  64. for (int i = 0; i < parsed_strings.size(); i++) {
  65. parsed_strings.set(i, regex.sub(parsed_strings[i], "", true));
  66. }
  67. r_ids->append_array(parsed_strings);
  68. return OK;
  69. }
  70. void GDScriptEditorTranslationParserPlugin::_parse_file_dialog(const String &p_source_code, Vector<String> *r_output) {
  71. // FileDialog API has the form .filters = PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"]).
  72. // First filter: Get "*.png ; PNG Images", "*.gd ; GDScript Files" from PackedStringArray.
  73. regex.clear();
  74. regex.compile(String("|").join(file_dialog_patterns));
  75. Array results = regex.search_all(p_source_code);
  76. Vector<String> temp;
  77. _get_captured_strings(results, &temp);
  78. String captured_strings = String(",").join(temp);
  79. // Second filter: Get the texts after semicolon from "*.png ; PNG Images","*.gd ; GDScript Files".
  80. String second_filter = "\"[^;]+;" + text + "\"";
  81. regex.clear();
  82. regex.compile(second_filter);
  83. results = regex.search_all(captured_strings);
  84. _get_captured_strings(results, r_output);
  85. for (int i = 0; i < r_output->size(); i++) {
  86. r_output->set(i, r_output->get(i).strip_edges());
  87. }
  88. }
  89. void GDScriptEditorTranslationParserPlugin::_get_captured_strings(const Array &p_results, Vector<String> *r_output) {
  90. Ref<RegExMatch> result;
  91. for (int i = 0; i < p_results.size(); i++) {
  92. result = p_results[i];
  93. for (int j = 0; j < result->get_group_count(); j++) {
  94. String s = result->get_string(j + 1);
  95. // Prevent reading text with only spaces.
  96. if (!s.strip_edges().empty()) {
  97. r_output->push_back(s);
  98. }
  99. }
  100. }
  101. }
  102. void GDScriptEditorTranslationParserPlugin::_handle_tr_pattern(const String &p_source_code, const String &p_pattern, Vector<Vector<String>> *r_ids_ctx_plural) {
  103. regex.clear();
  104. regex.compile(p_pattern);
  105. Array results = regex.search_all(p_source_code);
  106. Ref<RegExMatch> reg_match;
  107. int valid_group_count = 0;
  108. if (p_pattern == tr_pattern) {
  109. valid_group_count = 2;
  110. } else if (p_pattern == trn_pattern) {
  111. valid_group_count = 3;
  112. } else {
  113. ERR_FAIL_MSG("Invalid p_pattern parameter. Unrecognized tr pattern.");
  114. return;
  115. }
  116. // Get msgid, context and plural from RegEx search result.
  117. for (int i = 0; i < results.size(); i++) {
  118. reg_match = results[i];
  119. ERR_FAIL_COND_MSG(reg_match->get_group_count() != valid_group_count, "Number of captured groups from RegEx pattern doesn't match.");
  120. String msgid = reg_match->get_string(1);
  121. String context, plural;
  122. if (valid_group_count == 2) {
  123. context = reg_match->get_string(2);
  124. } else if (valid_group_count == 3) {
  125. plural = reg_match->get_string(2);
  126. context = reg_match->get_string(3);
  127. }
  128. if (!msgid.strip_edges().empty()) {
  129. Vector<String> id_ctx_plural;
  130. id_ctx_plural.push_back(msgid);
  131. id_ctx_plural.push_back(context);
  132. id_ctx_plural.push_back(plural.strip_edges().empty() ? "" : plural);
  133. r_ids_ctx_plural->push_back(id_ctx_plural);
  134. }
  135. }
  136. }
  137. GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
  138. // Regex search pattern templates.
  139. // The extra complication in the regex pattern is to ensure that the matching works when users write over multiple lines, use tabs etc.
  140. const String dot = "\\.[\\s\\\\]*";
  141. const String str_assign_template = "[\\s\\\\]*=[\\s\\\\]*\"" + text + "\"";
  142. const String first_arg_template = "[\\s\\\\]*\\([\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
  143. const String second_arg_template = "[\\s\\\\]*\\([\\s\\S]+?,[\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
  144. // Common patterns.
  145. patterns.push_back(dot + "text" + str_assign_template);
  146. patterns.push_back(dot + "placeholder_text" + str_assign_template);
  147. patterns.push_back(dot + "hint_tooltip" + str_assign_template);
  148. patterns.push_back(dot + "set_text" + first_arg_template);
  149. patterns.push_back(dot + "set_tooltip" + first_arg_template);
  150. patterns.push_back(dot + "set_placeholder" + first_arg_template);
  151. // Tabs and TabContainer API.
  152. patterns.push_back(dot + "set_tab_title" + second_arg_template);
  153. patterns.push_back(dot + "add_tab" + first_arg_template);
  154. // PopupMenu API.
  155. patterns.push_back(dot + "add_check_item" + first_arg_template);
  156. patterns.push_back(dot + "add_icon_check_item" + second_arg_template);
  157. patterns.push_back(dot + "add_icon_item" + second_arg_template);
  158. patterns.push_back(dot + "add_icon_radio_check_item" + second_arg_template);
  159. patterns.push_back(dot + "add_item" + first_arg_template);
  160. patterns.push_back(dot + "add_multistate_item" + first_arg_template);
  161. patterns.push_back(dot + "add_radio_check_item" + first_arg_template);
  162. patterns.push_back(dot + "add_separator" + first_arg_template);
  163. patterns.push_back(dot + "add_submenu_item" + first_arg_template);
  164. patterns.push_back(dot + "set_item_text" + second_arg_template);
  165. //patterns.push_back(dot + "set_item_tooltip" + second_arg_template); //no tr() behind this function. might be bug.
  166. // FileDialog API - special case.
  167. const String fd_text = "((?:[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*(?:\"[\\s\\\\]*\\+[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*)*\"[\\s\\\\]*,?)*)";
  168. const String packed_string_array = "[\\s\\\\]*PackedStringArray[\\s\\\\]*\\([\\s\\\\]*\\[" + fd_text + "\\][\\s\\\\]*\\)";
  169. file_dialog_patterns.push_back(dot + "add_filter[\\s\\\\]*\\(" + fd_text + "[\\s\\\\]*\\)");
  170. file_dialog_patterns.push_back(dot + "filters[\\s\\\\]*=" + packed_string_array);
  171. file_dialog_patterns.push_back(dot + "set_filters[\\s\\\\]*\\(" + packed_string_array + "[\\s\\\\]*\\)");
  172. }