diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-07-05 22:35:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-05 22:35:18 +0200 |
commit | c95fbd50fef4f648fef28e9435726a40ee7b7e2e (patch) | |
tree | cf5df9d12bf5e8e608c4413f47aee6005e624120 | |
parent | 7e38e8fdb0c05f0216e4322806315a025dc05b6f (diff) | |
parent | cae6f0bda282c8e831cbae5d45cfaa4e841ff3bf (diff) |
Merge pull request #40102 from SkyLucilfer/PotGenerationFix
Change translation parser plugin API to parse_file()
-rw-r--r-- | doc/classes/EditorTranslationParserPlugin.xml | 25 | ||||
-rw-r--r-- | editor/editor_translation_parser.cpp | 29 | ||||
-rw-r--r-- | editor/editor_translation_parser.h | 1 | ||||
-rw-r--r-- | editor/plugins/packed_scene_translation_parser_plugin.cpp | 2 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_translation_parser_plugin.cpp | 37 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_translation_parser_plugin.h | 1 |
6 files changed, 35 insertions, 60 deletions
diff --git a/doc/classes/EditorTranslationParserPlugin.xml b/doc/classes/EditorTranslationParserPlugin.xml index 73098efd99..a40ef45916 100644 --- a/doc/classes/EditorTranslationParserPlugin.xml +++ b/doc/classes/EditorTranslationParserPlugin.xml @@ -4,22 +4,39 @@ Plugin for adding custom parsers to extract strings that are to be translated from custom files (.csv, .json etc.). </brief_description> <description> - Plugins are registered via [method EditorPlugin.add_translation_parser_plugin] method. To define the parsing and string extraction logic, override the [method parse_text] method in script. + Plugins are registered via [method EditorPlugin.add_translation_parser_plugin] method. To define the parsing and string extraction logic, override the [method parse_file] method in script. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu. Below shows an example of a custom parser that extracts strings in a CSV file to write into a POT. [codeblock] tool extends EditorTranslationParserPlugin - func parse_text(text, extracted_strings): + + func parse_file(path, extracted_strings): + var file = File.new() + file.open(path, File.READ) + var text = file.get_as_text() var split_strs = text.split(",", false, 0) for s in split_strs: extracted_strings.append(s) #print("Extracted string: " + s) + func get_recognized_extensions(): return ["csv"] [/codeblock] + [b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [File] type. + For example: + [codeblock] + func parse_file(path, extracted_strings): + var res = ResourceLoader.load(path, "Script") + var text = res.get_source_code() + # Parsing logic. + + + func get_recognized_extensions(): + return ["gd"] + [/codeblock] </description> <tutorials> </tutorials> @@ -31,10 +48,10 @@ Gets the list of file extensions to associate with this parser, e.g. [code]["csv"][/code]. </description> </method> - <method name="parse_text" qualifiers="virtual"> + <method name="parse_file" qualifiers="virtual"> <return type="void"> </return> - <argument index="0" name="text" type="String"> + <argument index="0" name="path" type="String"> </argument> <argument index="1" name="extracted_strings" type="Array"> </argument> diff --git a/editor/editor_translation_parser.cpp b/editor/editor_translation_parser.cpp index 1f08a985f1..3f4864ad1e 100644 --- a/editor/editor_translation_parser.cpp +++ b/editor/editor_translation_parser.cpp @@ -41,33 +41,16 @@ Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Str if (!get_script_instance()) return ERR_UNAVAILABLE; - if (get_script_instance()->has_method("parse_text")) { - Error err; - FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err); - if (err != OK) { - ERR_PRINT("Failed to open " + p_path); - return err; - } - parse_text(file->get_as_utf8_string(), r_extracted_strings); - return OK; - } else { - ERR_PRINT("Custom translation parser plugin's \"func parse_text(text, extracted_strings)\" is undefined."); - return ERR_UNAVAILABLE; - } -} - -void EditorTranslationParserPlugin::parse_text(const String &p_text, Vector<String> *r_extracted_strings) { - if (!get_script_instance()) - return; - - if (get_script_instance()->has_method("parse_text")) { + if (get_script_instance()->has_method("parse_file")) { Array extracted_strings; - get_script_instance()->call("parse_text", p_text, extracted_strings); + get_script_instance()->call("parse_file", p_path, extracted_strings); for (int i = 0; i < extracted_strings.size(); i++) { r_extracted_strings->append(extracted_strings[i]); } + return OK; } else { - ERR_PRINT("Custom translation parser plugin's \"func parse_text(text, extracted_strings)\" is undefined."); + ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined."); + return ERR_UNAVAILABLE; } } @@ -86,7 +69,7 @@ void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_ex } void EditorTranslationParserPlugin::_bind_methods() { - ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::NIL, "parse_text", PropertyInfo(Variant::STRING, "text"), PropertyInfo(Variant::ARRAY, "extracted_strings"))); + ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::NIL, "parse_file", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::ARRAY, "extracted_strings"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::ARRAY, "get_recognized_extensions")); } diff --git a/editor/editor_translation_parser.h b/editor/editor_translation_parser.h index 518e3616eb..6d00bedfa4 100644 --- a/editor/editor_translation_parser.h +++ b/editor/editor_translation_parser.h @@ -42,7 +42,6 @@ protected: public: virtual Error parse_file(const String &p_path, Vector<String> *r_extracted_strings); - virtual void parse_text(const String &p_text, Vector<String> *r_extracted_strings); virtual void get_recognized_extensions(List<String> *r_extensions) const; }; diff --git a/editor/plugins/packed_scene_translation_parser_plugin.cpp b/editor/plugins/packed_scene_translation_parser_plugin.cpp index f9aaa93614..52af0008b7 100644 --- a/editor/plugins/packed_scene_translation_parser_plugin.cpp +++ b/editor/plugins/packed_scene_translation_parser_plugin.cpp @@ -71,7 +71,7 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, String extension = s->get_language()->get_extension(); if (EditorTranslationParser::get_singleton()->can_parse(extension)) { Vector<String> temp; - EditorTranslationParser::get_singleton()->get_parser(extension)->parse_text(s->get_source_code(), &temp); + EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp); parsed_strings.append_array(temp); } } else if (property_name == "filters") { diff --git a/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp b/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp index a1b18978fc..6d454e43f2 100644 --- a/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp +++ b/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp @@ -38,25 +38,8 @@ void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<Strin } Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_extracted_strings) { - List<String> extensions; - get_recognized_extensions(&extensions); - bool extension_valid = false; - for (auto E = extensions.front(); E; E = E->next()) { - if (p_path.get_extension() == E->get()) { - extension_valid = true; - break; - } - } - - if (!extension_valid) { - Vector<String> temp; - for (auto E = extensions.front(); E; E = E->next()) { - temp.push_back(E->get()); - } - String valid_extensions = String(", ").join(temp); - ERR_PRINT("Argument p_path \"" + p_path + "\" has wrong extension. List of valid extensions: " + valid_extensions); - return ERR_INVALID_PARAMETER; - } + // 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. Error err; RES loaded_res = ResourceLoader::load(p_path, "", false, &err); @@ -66,26 +49,18 @@ Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Ve } Ref<GDScript> gdscript = loaded_res; - parse_text(gdscript->get_source_code(), r_extracted_strings); - - return OK; -} - -void GDScriptEditorTranslationParserPlugin::parse_text(const String &p_text, Vector<String> *r_extracted_strings) { - // 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. - + String source_code = gdscript->get_source_code(); Vector<String> parsed_strings; // Search translation strings with RegEx. regex.clear(); regex.compile(String("|").join(patterns)); - Array results = regex.search_all(p_text); + Array results = regex.search_all(source_code); _get_captured_strings(results, &parsed_strings); // Special handling for FileDialog. Vector<String> temp; - _parse_file_dialog(p_text, &temp); + _parse_file_dialog(source_code, &temp); parsed_strings.append_array(temp); // Filter out / and + @@ -97,6 +72,8 @@ void GDScriptEditorTranslationParserPlugin::parse_text(const String &p_text, Vec } r_extracted_strings->append_array(parsed_strings); + + return OK; } void GDScriptEditorTranslationParserPlugin::_parse_file_dialog(const String &p_source_code, Vector<String> *r_output) { diff --git a/modules/gdscript/editor/gdscript_translation_parser_plugin.h b/modules/gdscript/editor/gdscript_translation_parser_plugin.h index ef967845b9..efef8f8249 100644 --- a/modules/gdscript/editor/gdscript_translation_parser_plugin.h +++ b/modules/gdscript/editor/gdscript_translation_parser_plugin.h @@ -48,7 +48,6 @@ class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlug public: virtual Error parse_file(const String &p_path, Vector<String> *r_extracted_strings); - virtual void parse_text(const String &p_text, Vector<String> *r_extracted_strings); virtual void get_recognized_extensions(List<String> *r_extensions) const; GDScriptEditorTranslationParserPlugin(); |