diff options
Diffstat (limited to 'editor')
-rw-r--r-- | editor/editor_node.cpp | 7 | ||||
-rw-r--r-- | editor/editor_plugin.cpp | 10 | ||||
-rw-r--r-- | editor/editor_plugin.h | 4 | ||||
-rw-r--r-- | editor/editor_translation_parser.cpp | 180 | ||||
-rw-r--r-- | editor/editor_translation_parser.h | 73 | ||||
-rw-r--r-- | editor/plugins/packed_scene_translation_parser_plugin.cpp | 114 | ||||
-rw-r--r-- | editor/plugins/packed_scene_translation_parser_plugin.h | 49 | ||||
-rw-r--r-- | editor/pot_generator.cpp | 166 | ||||
-rw-r--r-- | editor/pot_generator.h | 52 | ||||
-rw-r--r-- | editor/project_settings_editor.cpp | 124 | ||||
-rw-r--r-- | editor/project_settings_editor.h | 11 |
11 files changed, 788 insertions, 2 deletions
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 745cdd59c0..6e65103748 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<EditorPlugin>(); + ClassDB::register_class<EditorTranslationParserPlugin>(); ClassDB::register_class<EditorImportPlugin>(); ClassDB::register_class<EditorScript>(); ClassDB::register_class<EditorSelection>(); @@ -6656,6 +6659,10 @@ EditorNode::EditorNode() { EditorExport::get_singleton()->add_export_plugin(export_text_to_binary_plugin); + Ref<PackedSceneEditorTranslationParserPlugin> 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<Resource>(); diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index 32b799cd61..af1b426327 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -663,6 +663,14 @@ bool EditorPlugin::get_remove_list(List<Node *> *p_list) { void EditorPlugin::restore_global_state() {} void EditorPlugin::save_global_state() {} +void EditorPlugin::add_translation_parser_plugin(const Ref<EditorTranslationParserPlugin> &p_parser) { + EditorTranslationParser::get_singleton()->add_parser(p_parser, EditorTranslationParser::CUSTOM); +} + +void EditorPlugin::remove_translation_parser_plugin(const Ref<EditorTranslationParserPlugin> &p_parser) { + EditorTranslationParser::get_singleton()->remove_parser(p_parser, EditorTranslationParser::CUSTOM); +} + void EditorPlugin::add_import_plugin(const Ref<EditorImportPlugin> &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 e84984d57a..52ff7f04f8 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<EditorTranslationParserPlugin> &p_parser); + void remove_translation_parser_plugin(const Ref<EditorTranslationParserPlugin> &p_parser); + void add_import_plugin(const Ref<EditorImportPlugin> &p_importer); void remove_import_plugin(const Ref<EditorImportPlugin> &p_importer); diff --git a/editor/editor_translation_parser.cpp b/editor/editor_translation_parser.cpp new file mode 100644 index 0000000000..1f08a985f1 --- /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<String> *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<String> *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<String> *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<String> *r_extensions) const { + Set<String> extensions; + List<String> 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<String> extensions; + get_recognized_extensions(&extensions); + for (int i = 0; i < extensions.size(); i++) { + if (p_extension == extensions[i]) { + return true; + } + } + return false; +} + +Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const String &p_extension) const { + // Consider user-defined parsers first. + for (int i = 0; i < custom_parsers.size(); i++) { + List<String> 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<String> 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<EditorTranslationParserPlugin> &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<EditorTranslationParserPlugin> &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 0000000000..518e3616eb --- /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<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; +}; + +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<Ref<EditorTranslationParserPlugin>> standard_parsers; + Vector<Ref<EditorTranslationParserPlugin>> custom_parsers; + + void get_recognized_extensions(List<String> *r_extensions) const; + bool can_parse(const String &p_extension) const; + Ref<EditorTranslationParserPlugin> get_parser(const String &p_extension) const; + void add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type); + void remove_parser(const Ref<EditorTranslationParserPlugin> &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 0000000000..f9aaa93614 --- /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<String> *r_extensions) const { + ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions); +} + +Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *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<SceneState> state = Ref<PackedScene>(loaded_res)->get_state(); + + Vector<String> 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<Script> s = Object::cast_to<Script>(property_value); + 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); + parsed_strings.append_array(temp); + } + } 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); + } + } + } + } + + r_extracted_strings->append_array(parsed_strings); + + return OK; +} + +PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() { + // 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"); +} diff --git a/editor/plugins/packed_scene_translation_parser_plugin.h b/editor/plugins/packed_scene_translation_parser_plugin.h new file mode 100644 index 0000000000..9fead84c3f --- /dev/null +++ b/editor/plugins/packed_scene_translation_parser_plugin.h @@ -0,0 +1,49 @@ +/*************************************************************************/ +/* packed_scene_translation_parser_plugin.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 PACKED_SCENE_TRANSLATION_PARSER_PLUGIN_H +#define PACKED_SCENE_TRANSLATION_PARSER_PLUGIN_H + +#include "editor/editor_translation_parser.h" + +class PackedSceneEditorTranslationParserPlugin : public EditorTranslationParserPlugin { + GDCLASS(PackedSceneEditorTranslationParserPlugin, EditorTranslationParserPlugin); + + // Scene Node's properties that contain translation strings. + Set<String> lookup_properties; + +public: + virtual Error parse_file(const String &p_path, Vector<String> *r_extracted_strings); + virtual void get_recognized_extensions(List<String> *r_extensions) const; + + PackedSceneEditorTranslationParserPlugin(); +}; + +#endif // PACKED_SCENE_TRANSLATION_PARSER_PLUGIN_H diff --git a/editor/pot_generator.cpp b/editor/pot_generator.cpp new file mode 100644 index 0000000000..f9b8722aad --- /dev/null +++ b/editor/pot_generator.cpp @@ -0,0 +1,166 @@ +/*************************************************************************/ +/* pot_generator.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 "pot_generator.h" + +#include "core/error_macros.h" +#include "core/os/file_access.h" +#include "core/project_settings.h" +#include "editor_translation_parser.h" +#include "plugins/packed_scene_translation_parser_plugin.h" + +POTGenerator *POTGenerator::singleton = nullptr; + +//#define DEBUG_POT + +#ifdef DEBUG_POT +void _print_all_translation_strings(const OrderedHashMap<String, Set<String>> &p_all_translation_strings) { + for (auto E_pair = p_all_translation_strings.front(); E_pair; E_pair = E_pair.next()) { + String msg = static_cast<String>(E_pair.key()) + " : "; + for (Set<String>::Element *E = E_pair.value().front(); E; E = E->next()) { + msg += E->get() + " "; + } + print_line(msg); + } +} +#endif + +void POTGenerator::generate_pot(const String &p_file) { + if (!ProjectSettings::get_singleton()->has_setting("locale/translations_pot_files")) { + WARN_PRINT("No files selected for POT generation."); + return; + } + + // Clear all_translation_strings of the previous round. + all_translation_strings.clear(); + + Vector<String> files = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); + + // 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(); + + 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 " + 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. + for (int j = 0; j < translation_strings.size(); j++) { + all_translation_strings[translation_strings[j]].insert(file_path); + } + } + +#ifdef DEBUG_POT + _print_all_translation_strings(all_translation_strings); +#endif + + _write_to_pot(p_file); +} + +void POTGenerator::_write_to_pot(const String &p_file) { + Error err; + FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err); + if (err != OK) { + ERR_PRINT("Failed to open " + p_file); + return; + } + + String project_name = ProjectSettings::get_singleton()->get("application/config/name"); + Vector<String> files = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); + String extracted_files = ""; + for (int i = 0; i < files.size(); i++) { + extracted_files += "# " + files[i] + "\n"; + } + const String header = + "# LANGUAGE translation for " + project_name + " for the following files:\n" + extracted_files + + "#\n" + "#\n" + "# FIRST AUTHOR < EMAIL @ADDRESS>, YEAR.\n" + "#\n" + "#, fuzzy\n" + "msgid \"\"\n" + "msgstr \"\"\n" + "\"Project-Id-Version: " + + project_name + "\\n\"\n" + "\"Content-Type: text/plain; charset=UTF-8\\n\"\n" + "\"Content-Transfer-Encoding: 8-bit\\n\"\n\n"; + + file->store_string(header); + + for (OrderedHashMap<String, Set<String>>::Element E_pair = all_translation_strings.front(); E_pair; E_pair = E_pair.next()) { + String msg = E_pair.key(); + + // Write file locations. + for (Set<String>::Element *E = E_pair.value().front(); E; E = E->next()) { + 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. + file->store_string("msgid "); + for (int i = 0; i < msg_lines.size(); i++) { + file->store_line("\"" + msg_lines[i] + "\""); + } + + file->store_line("msgstr \"\"\n"); + } + + file->close(); +} + +POTGenerator *POTGenerator::get_singleton() { + if (!singleton) { + singleton = memnew(POTGenerator); + } + return singleton; +} + +POTGenerator::POTGenerator() { +} + +POTGenerator::~POTGenerator() { + memdelete(singleton); + singleton = nullptr; +} diff --git a/editor/pot_generator.h b/editor/pot_generator.h new file mode 100644 index 0000000000..abe1a21d41 --- /dev/null +++ b/editor/pot_generator.h @@ -0,0 +1,52 @@ +/*************************************************************************/ +/* pot_generator.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 POT_GENERATOR_H +#define POT_GENERATOR_H + +#include "core/ordered_hash_map.h" +#include "core/set.h" + +class POTGenerator { + static POTGenerator *singleton; + // Stores all translatable strings and the source files containing them. + OrderedHashMap<String, Set<String>> all_translation_strings; + + void _write_to_pot(const String &p_file); + +public: + static POTGenerator *get_singleton(); + void generate_pot(const String &p_file); + + POTGenerator(); + ~POTGenerator(); +}; + +#endif // POT_GENERATOR_H diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index a8029e1e2b..389b53133e 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -38,6 +38,8 @@ #include "editor/editor_export.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" +#include "editor/editor_translation_parser.h" +#include "editor/pot_generator.h" #include "scene/gui/margin_container.h" #include "scene/gui/tab_container.h" @@ -120,6 +122,7 @@ void ProjectSettingsEditor::_notification(int p_what) { action_add_error->add_theme_color_override("font_color", input_editor->get_theme_color("error_color", "Editor")); translation_list->connect("button_pressed", callable_mp(this, &ProjectSettingsEditor::_translation_delete)); + translation_pot_list->connect("button_pressed", callable_mp(this, &ProjectSettingsEditor::_translation_pot_delete)); _update_actions(); popup_add->add_icon_item(input_editor->get_theme_icon("Keyboard", "EditorIcons"), TTR("Key"), INPUT_KEY); popup_add->add_icon_item(input_editor->get_theme_icon("KeyboardPhysical", "EditorIcons"), TTR("Physical Key"), INPUT_KEY_PHYSICAL); @@ -140,6 +143,9 @@ void ProjectSettingsEditor::_notification(int p_what) { translation_res_option_file_open->add_filter("*." + E->get()); } + _update_translation_pot_file_extensions(); + translation_pot_generate->add_filter("*.pot"); + restart_close_button->set_icon(input_editor->get_theme_icon("Close", "EditorIcons")); restart_container->add_theme_style_override("panel", input_editor->get_theme_stylebox("bg", "Tree")); restart_icon->set_texture(input_editor->get_theme_icon("StatusWarning", "EditorIcons")); @@ -865,6 +871,8 @@ void ProjectSettingsEditor::popup_project_settings() { _update_translations(); autoload_settings->update_autoload(); plugin_settings->update_plugins(); + // New translation parser plugin might extend possible file extensions in POT generation. + _update_translation_pot_file_extensions(); set_process_unhandled_input(true); } @@ -1519,8 +1527,71 @@ void ProjectSettingsEditor::_translation_filter_mode_changed(int p_mode) { undo_redo->commit_action(); } +void ProjectSettingsEditor::_translation_pot_add(const String &p_path) { + PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); + + for (int i = 0; i < pot_translations.size(); i++) { + if (pot_translations[i] == p_path) { + return; //exists + } + } + + pot_translations.push_back(p_path); + undo_redo->create_action(TTR("Add files for POT generation")); + undo_redo->add_do_property(ProjectSettings::get_singleton(), "locale/translations_pot_files", pot_translations); + undo_redo->add_undo_property(ProjectSettings::get_singleton(), "locale/translations_pot_files", ProjectSettings::get_singleton()->get("locale/translations_pot_files")); + undo_redo->add_do_method(this, "_update_translations"); + undo_redo->add_undo_method(this, "_update_translations"); + undo_redo->add_do_method(this, "_settings_changed"); + undo_redo->add_undo_method(this, "_settings_changed"); + undo_redo->commit_action(); +} + +void ProjectSettingsEditor::_translation_pot_delete(Object *p_item, int p_column, int p_button) { + TreeItem *ti = Object::cast_to<TreeItem>(p_item); + ERR_FAIL_COND(!ti); + + int idx = ti->get_metadata(0); + + PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); + + ERR_FAIL_INDEX(idx, pot_translations.size()); + + pot_translations.remove(idx); + + undo_redo->create_action(TTR("Remove file from POT generation")); + undo_redo->add_do_property(ProjectSettings::get_singleton(), "locale/translations_pot_files", pot_translations); + undo_redo->add_undo_property(ProjectSettings::get_singleton(), "locale/translations_pot_files", ProjectSettings::get_singleton()->get("locale/translations_pot_files")); + undo_redo->add_do_method(this, "_update_translations"); + undo_redo->add_undo_method(this, "_update_translations"); + undo_redo->add_do_method(this, "_settings_changed"); + undo_redo->add_undo_method(this, "_settings_changed"); + undo_redo->commit_action(); +} + +void ProjectSettingsEditor::_translation_pot_file_open() { + translation_pot_file_open->popup_centered_ratio(); +} + +void ProjectSettingsEditor::_translation_pot_generate_open() { + translation_pot_generate->popup_centered_ratio(); +} + +void ProjectSettingsEditor::_translation_pot_generate(const String &p_file) { + POTGenerator::get_singleton()->generate_pot(p_file); +} + +void ProjectSettingsEditor::_update_translation_pot_file_extensions() { + translation_pot_file_open->clear_filters(); + List<String> translation_parse_file_extensions; + EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions); + for (List<String>::Element *E = translation_parse_file_extensions.front(); E; E = E->next()) { + translation_pot_file_open->add_filter("*." + E->get()); + } +} + void ProjectSettingsEditor::_update_translations() { - //update translations + // Update translations. if (updating_translations) { return; @@ -1601,7 +1672,7 @@ void ProjectSettingsEditor::_update_translations() { } } - //update translation remaps + // Update translation remaps. String remap_selected; if (translation_remap->get_selected()) { @@ -1696,6 +1767,23 @@ void ProjectSettingsEditor::_update_translations() { } } + // Update translation POT files. + + translation_pot_list->clear(); + root = translation_pot_list->create_item(nullptr); + translation_pot_list->set_hide_root(true); + if (ProjectSettings::get_singleton()->has_setting("locale/translations_pot_files")) { + PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("locale/translations_pot_files"); + for (int i = 0; i < pot_translations.size(); i++) { + TreeItem *t = translation_pot_list->create_item(root); + t->set_editable(0, false); + t->set_text(0, pot_translations[i].replace_first("res://", "")); + t->set_tooltip(0, pot_translations[i]); + t->set_metadata(0, i); + t->add_button(0, input_editor->get_theme_icon("Remove", "EditorIcons"), 0, false, TTR("Remove")); + } + } + updating_translations = false; } @@ -2108,6 +2196,38 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) { translation_filter->connect("item_edited", callable_mp(this, &ProjectSettingsEditor::_translation_filter_option_changed)); } + { + VBoxContainer *tvb = memnew(VBoxContainer); + translations->add_child(tvb); + tvb->set_name(TTR("POT Generation")); + HBoxContainer *thb = memnew(HBoxContainer); + tvb->add_child(thb); + thb->add_child(memnew(Label(TTR("Files with translation strings:")))); + thb->add_spacer(); + Button *addtr = memnew(Button(TTR("Add..."))); + addtr->connect("pressed", callable_mp(this, &ProjectSettingsEditor::_translation_pot_file_open)); + thb->add_child(addtr); + Button *generate = memnew(Button(TTR("Generate POT"))); + generate->connect("pressed", callable_mp(this, &ProjectSettingsEditor::_translation_pot_generate_open)); + thb->add_child(generate); + VBoxContainer *tmc = memnew(VBoxContainer); + tvb->add_child(tmc); + tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL); + translation_pot_list = memnew(Tree); + translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); + tmc->add_child(translation_pot_list); + + translation_pot_generate = memnew(EditorFileDialog); + add_child(translation_pot_generate); + translation_pot_generate->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); + translation_pot_generate->connect("file_selected", callable_mp(this, &ProjectSettingsEditor::_translation_pot_generate)); + + translation_pot_file_open = memnew(EditorFileDialog); + add_child(translation_pot_file_open); + translation_pot_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); + translation_pot_file_open->connect("file_selected", callable_mp(this, &ProjectSettingsEditor::_translation_pot_add)); + } + autoload_settings = memnew(EditorAutoloadSettings); autoload_settings->set_name(TTR("AutoLoad")); tab_container->add_child(autoload_settings); diff --git a/editor/project_settings_editor.h b/editor/project_settings_editor.h index 728f31efa8..cf47b1df4a 100644 --- a/editor/project_settings_editor.h +++ b/editor/project_settings_editor.h @@ -110,6 +110,10 @@ class ProjectSettingsEditor : public AcceptDialog { Vector<TreeItem *> translation_filter_treeitems; Vector<int> translation_locales_idxs_remap; + Tree *translation_pot_list; + EditorFileDialog *translation_pot_file_open; + EditorFileDialog *translation_pot_generate; + EditorAutoloadSettings *autoload_settings; EditorPluginSettings *plugin_settings; @@ -159,6 +163,13 @@ class ProjectSettingsEditor : public AcceptDialog { void _translation_filter_option_changed(); void _translation_filter_mode_changed(int p_mode); + void _translation_pot_add(const String &p_path); + void _translation_pot_delete(Object *p_item, int p_column, int p_button); + void _translation_pot_file_open(); + void _translation_pot_generate_open(); + void _translation_pot_generate(const String &p_file); + void _update_translation_pot_file_extensions(); + void _toggle_search_bar(bool p_pressed); Variant get_drag_data_fw(const Point2 &p_point, Control *p_from); |