diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml
index 2fa791a9df0..99fe9b4bb5d 100644
--- a/doc/classes/EditorPlugin.xml
+++ b/doc/classes/EditorPlugin.xml
@@ -142,6 +142,15 @@
Adds a custom submenu under [b]Project > Tools >[/b] [code]name[/code]. [code]submenu[/code] should be an object of class [PopupMenu]. This submenu should be cleaned up using [code]remove_tool_menu_item(name)[/code].
+
+
+
+
+
+
+ Registers a custom translation parser plugin for extracting translatable strings from custom files.
+
+
@@ -464,6 +473,15 @@
Removes a menu [code]name[/code] from [b]Project > Tools[/b].
+
+
+
+
+
+
+ Removes a registered custom translation parser plugin.
+
+
diff --git a/doc/classes/EditorTranslationParserPlugin.xml b/doc/classes/EditorTranslationParserPlugin.xml
new file mode 100644
index 00000000000..c7d796ec30d
--- /dev/null
+++ b/doc/classes/EditorTranslationParserPlugin.xml
@@ -0,0 +1,48 @@
+
+
+
+ Plugin for adding custom parsers to extract strings that are to be translated from custom files (.csv, .json etc.).
+
+
+ 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.
+ 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):
+ 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]
+
+
+
+
+
+
+
+
+ Gets the list of file extensions to associate with this parser, e.g. [code]["csv"][/code].
+
+
+
+
+
+
+
+
+
+
+ Override this method to define a custom parsing logic to extract the translatable strings.
+
+
+
+
+
+
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp
index 745cdd59c0a..6e65103748a 100644
--- a/editor/editor_node.cpp
+++ b/editor/editor_node.cpp
@@ -85,6 +85,7 @@
#include "editor/editor_settings.h"
#include "editor/editor_spin_slider.h"
#include "editor/editor_themes.h"
+#include "editor/editor_translation_parser.h"
#include "editor/export_template_manager.h"
#include "editor/filesystem_dock.h"
#include "editor/import/editor_import_collada.h"
@@ -137,6 +138,7 @@
#include "editor/plugins/multimesh_editor_plugin.h"
#include "editor/plugins/navigation_polygon_editor_plugin.h"
#include "editor/plugins/node_3d_editor_plugin.h"
+#include "editor/plugins/packed_scene_translation_parser_plugin.h"
#include "editor/plugins/path_2d_editor_plugin.h"
#include "editor/plugins/path_3d_editor_plugin.h"
#include "editor/plugins/physical_bone_3d_editor_plugin.h"
@@ -3588,6 +3590,7 @@ void EditorNode::register_editor_types() {
ResourceSaver::set_timestamp_on_save(true);
ClassDB::register_class();
+ ClassDB::register_class();
ClassDB::register_class();
ClassDB::register_class();
ClassDB::register_class();
@@ -6656,6 +6659,10 @@ EditorNode::EditorNode() {
EditorExport::get_singleton()->add_export_plugin(export_text_to_binary_plugin);
+ Ref packed_scene_translation_parser_plugin;
+ packed_scene_translation_parser_plugin.instance();
+ EditorTranslationParser::get_singleton()->add_parser(packed_scene_translation_parser_plugin, EditorTranslationParser::STANDARD);
+
_edit_current();
current = nullptr;
saving_resource = Ref();
diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp
index 32b799cd61d..af1b426327a 100644
--- a/editor/editor_plugin.cpp
+++ b/editor/editor_plugin.cpp
@@ -663,6 +663,14 @@ bool EditorPlugin::get_remove_list(List *p_list) {
void EditorPlugin::restore_global_state() {}
void EditorPlugin::save_global_state() {}
+void EditorPlugin::add_translation_parser_plugin(const Ref &p_parser) {
+ EditorTranslationParser::get_singleton()->add_parser(p_parser, EditorTranslationParser::CUSTOM);
+}
+
+void EditorPlugin::remove_translation_parser_plugin(const Ref &p_parser) {
+ EditorTranslationParser::get_singleton()->remove_parser(p_parser, EditorTranslationParser::CUSTOM);
+}
+
void EditorPlugin::add_import_plugin(const Ref &p_importer) {
ResourceFormatImporter::get_singleton()->add_importer(p_importer);
EditorFileSystem::get_singleton()->call_deferred("scan");
@@ -796,6 +804,8 @@ void EditorPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_undo_redo"), &EditorPlugin::_get_undo_redo);
ClassDB::bind_method(D_METHOD("queue_save_layout"), &EditorPlugin::queue_save_layout);
+ ClassDB::bind_method(D_METHOD("add_translation_parser_plugin", "parser"), &EditorPlugin::add_translation_parser_plugin);
+ ClassDB::bind_method(D_METHOD("remove_translation_parser_plugin", "parser"), &EditorPlugin::remove_translation_parser_plugin);
ClassDB::bind_method(D_METHOD("add_import_plugin", "importer"), &EditorPlugin::add_import_plugin);
ClassDB::bind_method(D_METHOD("remove_import_plugin", "importer"), &EditorPlugin::remove_import_plugin);
ClassDB::bind_method(D_METHOD("add_scene_import_plugin", "scene_importer"), &EditorPlugin::add_scene_import_plugin);
diff --git a/editor/editor_plugin.h b/editor/editor_plugin.h
index e84984d57ae..52ff7f04f85 100644
--- a/editor/editor_plugin.h
+++ b/editor/editor_plugin.h
@@ -34,6 +34,7 @@
#include "core/io/config_file.h"
#include "core/undo_redo.h"
#include "editor/editor_inspector.h"
+#include "editor/editor_translation_parser.h"
#include "editor/import/editor_import_plugin.h"
#include "editor/import/resource_importer_scene.h"
#include "editor/script_create_dialog.h"
@@ -220,6 +221,9 @@ public:
virtual void restore_global_state();
virtual void save_global_state();
+ void add_translation_parser_plugin(const Ref &p_parser);
+ void remove_translation_parser_plugin(const Ref &p_parser);
+
void add_import_plugin(const Ref &p_importer);
void remove_import_plugin(const Ref &p_importer);
diff --git a/editor/editor_translation_parser.cpp b/editor/editor_translation_parser.cpp
new file mode 100644
index 00000000000..1f08a985f1d
--- /dev/null
+++ b/editor/editor_translation_parser.cpp
@@ -0,0 +1,180 @@
+/*************************************************************************/
+/* editor_translation_parser.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "editor_translation_parser.h"
+
+#include "core/error_macros.h"
+#include "core/os/file_access.h"
+#include "core/script_language.h"
+#include "core/set.h"
+
+EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
+
+Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector *r_extracted_strings) {
+ 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 *r_extracted_strings) {
+ if (!get_script_instance())
+ return;
+
+ if (get_script_instance()->has_method("parse_text")) {
+ Array extracted_strings;
+ get_script_instance()->call("parse_text", p_text, extracted_strings);
+ for (int i = 0; i < extracted_strings.size(); i++) {
+ r_extracted_strings->append(extracted_strings[i]);
+ }
+ } else {
+ ERR_PRINT("Custom translation parser plugin's \"func parse_text(text, extracted_strings)\" is undefined.");
+ }
+}
+
+void EditorTranslationParserPlugin::get_recognized_extensions(List *r_extensions) const {
+ if (!get_script_instance())
+ return;
+
+ if (get_script_instance()->has_method("get_recognized_extensions")) {
+ Array extensions = get_script_instance()->call("get_recognized_extensions");
+ for (int i = 0; i < extensions.size(); i++) {
+ r_extensions->push_back(extensions[i]);
+ }
+ } else {
+ ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
+ }
+}
+
+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::ARRAY, "get_recognized_extensions"));
+}
+
+/////////////////////////
+
+void EditorTranslationParser::get_recognized_extensions(List *r_extensions) const {
+ Set extensions;
+ List temp;
+ for (int i = 0; i < standard_parsers.size(); i++) {
+ standard_parsers[i]->get_recognized_extensions(&temp);
+ }
+ for (int i = 0; i < custom_parsers.size(); i++) {
+ custom_parsers[i]->get_recognized_extensions(&temp);
+ }
+ // Remove duplicates.
+ for (int i = 0; i < temp.size(); i++) {
+ extensions.insert(temp[i]);
+ }
+ for (auto E = extensions.front(); E; E = E->next()) {
+ r_extensions->push_back(E->get());
+ }
+}
+
+bool EditorTranslationParser::can_parse(const String &p_extension) const {
+ List extensions;
+ get_recognized_extensions(&extensions);
+ for (int i = 0; i < extensions.size(); i++) {
+ if (p_extension == extensions[i]) {
+ return true;
+ }
+ }
+ return false;
+}
+
+Ref EditorTranslationParser::get_parser(const String &p_extension) const {
+ // Consider user-defined parsers first.
+ for (int i = 0; i < custom_parsers.size(); i++) {
+ List temp;
+ custom_parsers[i]->get_recognized_extensions(&temp);
+ for (int j = 0; j < temp.size(); j++) {
+ if (temp[j] == p_extension) {
+ return custom_parsers[i];
+ }
+ }
+ }
+
+ for (int i = 0; i < standard_parsers.size(); i++) {
+ List temp;
+ standard_parsers[i]->get_recognized_extensions(&temp);
+ for (int j = 0; j < temp.size(); j++) {
+ if (temp[j] == p_extension) {
+ return standard_parsers[i];
+ }
+ }
+ }
+
+ WARN_PRINT("No translation parser available for \"" + p_extension + "\" extension.");
+
+ return nullptr;
+}
+
+void EditorTranslationParser::add_parser(const Ref &p_parser, ParserType p_type) {
+ if (p_type == ParserType::STANDARD) {
+ standard_parsers.push_back(p_parser);
+ } else if (p_type == ParserType::CUSTOM) {
+ custom_parsers.push_back(p_parser);
+ }
+}
+
+void EditorTranslationParser::remove_parser(const Ref &p_parser, ParserType p_type) {
+ if (p_type == ParserType::STANDARD) {
+ standard_parsers.erase(p_parser);
+ } else if (p_type == ParserType::CUSTOM) {
+ custom_parsers.erase(p_parser);
+ }
+}
+
+EditorTranslationParser *EditorTranslationParser::get_singleton() {
+ if (!singleton) {
+ singleton = memnew(EditorTranslationParser);
+ }
+ return singleton;
+}
+
+EditorTranslationParser::EditorTranslationParser() {
+}
+
+EditorTranslationParser::~EditorTranslationParser() {
+ memdelete(singleton);
+ singleton = nullptr;
+}
diff --git a/editor/editor_translation_parser.h b/editor/editor_translation_parser.h
new file mode 100644
index 00000000000..518e3616ebc
--- /dev/null
+++ b/editor/editor_translation_parser.h
@@ -0,0 +1,73 @@
+/*************************************************************************/
+/* editor_translation_parser.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef EDITOR_TRANSLATION_PARSER_H
+#define EDITOR_TRANSLATION_PARSER_H
+
+#include "core/error_list.h"
+#include "core/reference.h"
+
+class EditorTranslationParserPlugin : public Reference {
+ GDCLASS(EditorTranslationParserPlugin, Reference);
+
+protected:
+ static void _bind_methods();
+
+public:
+ virtual Error parse_file(const String &p_path, Vector *r_extracted_strings);
+ virtual void parse_text(const String &p_text, Vector *r_extracted_strings);
+ virtual void get_recognized_extensions(List *r_extensions) const;
+};
+
+class EditorTranslationParser {
+ static EditorTranslationParser *singleton;
+
+public:
+ enum ParserType {
+ STANDARD, // GDScript, CSharp, ...
+ CUSTOM // User-defined parser plugins. This will override standard parsers if the same extension type is defined.
+ };
+
+ static EditorTranslationParser *get_singleton();
+
+ Vector[> standard_parsers;
+ Vector][> custom_parsers;
+
+ void get_recognized_extensions(List *r_extensions) const;
+ bool can_parse(const String &p_extension) const;
+ Ref get_parser(const String &p_extension) const;
+ void add_parser(const Ref &p_parser, ParserType p_type);
+ void remove_parser(const Ref &p_parser, ParserType p_type);
+
+ EditorTranslationParser();
+ ~EditorTranslationParser();
+};
+
+#endif // EDITOR_TRANSLATION_PARSER_H
diff --git a/editor/plugins/packed_scene_translation_parser_plugin.cpp b/editor/plugins/packed_scene_translation_parser_plugin.cpp
new file mode 100644
index 00000000000..f9aaa936141
--- /dev/null
+++ b/editor/plugins/packed_scene_translation_parser_plugin.cpp
@@ -0,0 +1,114 @@
+/*************************************************************************/
+/* packed_scene_translation_parser_plugin.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "packed_scene_translation_parser_plugin.h"
+
+#include "core/io/resource_loader.h"
+#include "scene/resources/packed_scene.h"
+
+void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List *r_extensions) const {
+ ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions);
+}
+
+Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector *r_extracted_strings) {
+ // 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.
+
+ Error err;
+ RES loaded_res = ResourceLoader::load(p_path, "PackedScene", false, &err);
+ if (err) {
+ ERR_PRINT("Failed to load " + p_path);
+ return err;
+ }
+ Ref state = Ref(loaded_res)->get_state();
+
+ Vector parsed_strings;
+ String property_name;
+ Variant property_value;
+ for (int i = 0; i < state->get_node_count(); i++) {
+ if (!ClassDB::is_parent_class(state->get_node_type(i), "Control") && !ClassDB::is_parent_class(state->get_node_type(i), "Viewport")) {
+ continue;
+ }
+
+ for (int j = 0; j < state->get_node_property_count(i); j++) {
+ property_name = state->get_node_property_name(i, j);
+ if (!lookup_properties.has(property_name)) {
+ continue;
+ }
+
+ property_value = 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.
+ Ref]