diff options
Diffstat (limited to 'editor/pot_generator.cpp')
-rw-r--r-- | editor/pot_generator.cpp | 224 |
1 files changed, 22 insertions, 202 deletions
diff --git a/editor/pot_generator.cpp b/editor/pot_generator.cpp index 7ec5a221a4..f9b8722aad 100644 --- a/editor/pot_generator.cpp +++ b/editor/pot_generator.cpp @@ -30,14 +30,11 @@ #include "pot_generator.h" -#include "core/class_db.h" #include "core/error_macros.h" -#include "core/io/resource_loader.h" +#include "core/os/file_access.h" #include "core/project_settings.h" -#include "core/script_language.h" -#include "core/variant.h" -#include "modules/gdscript/gdscript.h" -#include "scene/resources/packed_scene.h" +#include "editor_translation_parser.h" +#include "plugins/packed_scene_translation_parser_plugin.h" POTGenerator *POTGenerator::singleton = nullptr; @@ -65,40 +62,18 @@ void POTGenerator::generate_pot(const String &p_file) { all_translation_strings.clear(); Vector<String> files = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); - Vector<String> translation_strings; - List<String> packed_scene_extensions; - ResourceLoader::get_recognized_extensions_for_type("PackedScene", &packed_scene_extensions); // Collect all translatable strings according to files order in "POT Generation" setting. for (int i = 0; i < files.size(); i++) { + Vector<String> translation_strings; String file_path = files[i]; String file_extension = file_path.get_extension(); - Error err; - RES loaded_res = ResourceLoader::load(file_path, "", false, &err); - if (err) { - ERR_PRINT("Failed to load " + file_path); - continue; - } - - bool is_scene_file = false; - for (List<String>::Element *E = packed_scene_extensions.front(); E; E = E->next()) { - if (file_extension == E->get()) { - is_scene_file = true; - break; - } - } - - if (is_scene_file) { - Ref<PackedScene> ps = loaded_res; - translation_strings = _parse_scene(ps->get_state()); - } else if (file_extension == "gd") { - // Currently we only support GDScript. - Ref<GDScript> s = loaded_res; - translation_strings = _parse_script(s->get_source_code()); + if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) { + EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translation_strings); } else { - ERR_PRINT("Unrecognized file extension in generate_pot()"); - continue; + ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()"); + return; } // Store translation strings parsed in this iteration along with their corresponding source file - to write into POT later on. @@ -114,118 +89,6 @@ void POTGenerator::generate_pot(const String &p_file) { _write_to_pot(p_file); } -Vector<String> POTGenerator::_parse_scene(const Ref<SceneState> &p_state) { - // Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property. - // These properties are translated with the tr() function in the C++ code when being set or updated. - - Vector<String> parsed_strings; - - String property_name; - Variant property_value; - for (int i = 0; i < p_state->get_node_count(); i++) { - if (!ClassDB::is_parent_class(p_state->get_node_type(i), "Control") && !ClassDB::is_parent_class(p_state->get_node_type(i), "Viewport")) { - continue; - } - - for (int j = 0; j < p_state->get_node_property_count(i); j++) { - property_name = p_state->get_node_property_name(i, j); - if (!lookup_properties.has(property_name)) { - continue; - } - - property_value = p_state->get_node_property_value(i, j); - - if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) { - // Parse built-in script. Currently we only support GDScript. - Ref<GDScript> s = Object::cast_to<GDScript>(property_value); - parsed_strings.append_array(_parse_script(s->get_source_code())); - } else if (property_name == "filters") { - // Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files". - Vector<String> str_values = property_value; - for (int k = 0; k < str_values.size(); k++) { - String desc = str_values[k].get_slice(";", 1).strip_edges(); - if (!desc.empty()) { - parsed_strings.push_back(desc); - } - } - } else if (property_value.get_type() == Variant::STRING) { - String str_value = String(property_value); - // Prevent reading text containing only spaces. - if (!str_value.strip_edges().empty()) { - parsed_strings.push_back(str_value); - } - } - } - } - - return parsed_strings; -} - -Vector<String> POTGenerator::_parse_script(const String &p_source_code) { - // Parse and match all GDScript function API that involves translation string. - // E.g get_node("Label").text = "something", var test = tr("something") - // "something" will be matched and collected - // The extra complication in the regex pattern is to ensure that the matching works when users write over multiple lines, use tabs etc. - - Vector<String> parsed_strings; - - regex.clear(); - regex.compile(String("|").join(patterns)); - Array results = regex.search_all(p_source_code); - _get_captured_strings(results, &parsed_strings); - - // Special handling for FileDialog - Vector<String> temp; - _parse_file_dialog(p_source_code, &temp); - parsed_strings.append_array(temp); - - // Filter out / and + - String filter = "(?:\\\\\\n|\"[\\s\\\\]*\\+\\s*\")"; - regex.clear(); - regex.compile(filter); - for (int i = 0; i < parsed_strings.size(); i++) { - parsed_strings.set(i, regex.sub(parsed_strings[i], "", true)); - } - - return parsed_strings; -} - -void POTGenerator::_parse_file_dialog(const String &p_source_code, Vector<String> *r_output) { - // FileDialog API has the form .filters = PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"]). - // First filter: Get "*.png ; PNG Images", "*.gd ; GDScript Files" from PackedStringArray. - regex.clear(); - regex.compile(String("|").join(file_dialog_patterns)); - Array results = regex.search_all(p_source_code); - - Vector<String> temp; - _get_captured_strings(results, &temp); - String captured_strings = String(",").join(temp); - - // Second filter: Get the texts after semicolon from "*.png ; PNG Images","*.gd ; GDScript Files". - String second_filter = "\"[^;]+;" + text + "\""; - regex.clear(); - regex.compile(second_filter); - results = regex.search_all(captured_strings); - _get_captured_strings(results, r_output); - for (int i = 0; i < r_output->size(); i++) { - r_output->set(i, r_output->get(i).strip_edges()); - } -} - -void POTGenerator::_get_captured_strings(const Array &p_results, Vector<String> *r_output) { - Ref<RegExMatch> result; - for (int i = 0; i < p_results.size(); i++) { - result = p_results[i]; - for (int j = 0; j < result->get_group_count(); j++) { - String s = result->get_string(j + 1); - // Prevent reading text with only spaces. - if (!s.strip_edges().empty()) { - r_output->push_back(s); - } - } - } -} - void POTGenerator::_write_to_pot(const String &p_file) { Error err; FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err); @@ -264,14 +127,22 @@ void POTGenerator::_write_to_pot(const String &p_file) { file->store_line("#: " + E->get().trim_prefix("res://")); } + // Split \\n and \n. + Vector<String> temp = msg.split("\\n"); + Vector<String> msg_lines; + for (int i = 0; i < temp.size(); i++) { + msg_lines.append_array(temp[i].split("\n")); + if (i < temp.size() - 1) { + // Add \n. + msg_lines.set(msg_lines.size() - 1, msg_lines[msg_lines.size() - 1] + "\\n"); + } + } + // Write msgid. - Vector<String> msg_lines = msg.split("\\n"); - file->store_string("msgid \"" + msg_lines[0]); - for (int i = 1; i < msg_lines.size(); i++) { - file->store_line("\\n\""); - file->store_string("\"" + msg_lines[i]); + file->store_string("msgid "); + for (int i = 0; i < msg_lines.size(); i++) { + file->store_line("\"" + msg_lines[i] + "\""); } - file->store_line("\""); file->store_line("msgstr \"\"\n"); } @@ -287,57 +158,6 @@ POTGenerator *POTGenerator::get_singleton() { } POTGenerator::POTGenerator() { - // Scene Node's properties containing strings that will be fetched for translation. - lookup_properties.insert("text"); - lookup_properties.insert("hint_tooltip"); - lookup_properties.insert("placeholder_text"); - lookup_properties.insert("dialog_text"); - lookup_properties.insert("filters"); - lookup_properties.insert("script"); - - //Add exception list (to prevent false positives) - //line edit, text edit, richtextlabel - //Set<String> exception_list; - //exception_list.insert("RichTextLabel"); - - // Regex search pattern templates. - const String dot = "\\.[\\s\\\\]*"; - const String str_assign_template = "[\\s\\\\]*=[\\s\\\\]*\"" + text + "\""; - const String first_arg_template = "[\\s\\\\]*\\([\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)"; - const String second_arg_template = "[\\s\\\\]*\\([\\s\\S]+?,[\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)"; - - // Common patterns. - patterns.push_back("tr" + first_arg_template); - patterns.push_back(dot + "text" + str_assign_template); - patterns.push_back(dot + "placeholder_text" + str_assign_template); - patterns.push_back(dot + "hint_tooltip" + str_assign_template); - patterns.push_back(dot + "set_text" + first_arg_template); - patterns.push_back(dot + "set_tooltip" + first_arg_template); - patterns.push_back(dot + "set_placeholder" + first_arg_template); - - // Tabs and TabContainer API. - patterns.push_back(dot + "set_tab_title" + second_arg_template); - patterns.push_back(dot + "add_tab" + first_arg_template); - - // PopupMenu API. - patterns.push_back(dot + "add_check_item" + first_arg_template); - patterns.push_back(dot + "add_icon_check_item" + second_arg_template); - patterns.push_back(dot + "add_icon_item" + second_arg_template); - patterns.push_back(dot + "add_icon_radio_check_item" + second_arg_template); - patterns.push_back(dot + "add_item" + first_arg_template); - patterns.push_back(dot + "add_multistate_item" + first_arg_template); - patterns.push_back(dot + "add_radio_check_item" + first_arg_template); - patterns.push_back(dot + "add_separator" + first_arg_template); - patterns.push_back(dot + "add_submenu_item" + first_arg_template); - patterns.push_back(dot + "set_item_text" + second_arg_template); - //patterns.push_back(dot + "set_item_tooltip" + second_arg_template); //no tr() behind this function. might be bug. - - // FileDialog API - special case. - const String fd_text = "((?:[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*(?:\"[\\s\\\\]*\\+[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*)*\"[\\s\\\\]*,?)*)"; - const String packed_string_array = "[\\s\\\\]*PackedStringArray[\\s\\\\]*\\([\\s\\\\]*\\[" + fd_text + "\\][\\s\\\\]*\\)"; - file_dialog_patterns.push_back(dot + "add_filter[\\s\\\\]*\\(" + fd_text + "[\\s\\\\]*\\)"); - file_dialog_patterns.push_back(dot + "filters[\\s\\\\]*=" + packed_string_array); - file_dialog_patterns.push_back(dot + "set_filters[\\s\\\\]*\\(" + packed_string_array + "[\\s\\\\]*\\)"); } POTGenerator::~POTGenerator() { |