| 1 | /**************************************************************************/ |
| 2 | /* packed_scene_translation_parser_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "packed_scene_translation_parser_plugin.h" |
| 32 | |
| 33 | #include "core/io/resource_loader.h" |
| 34 | #include "core/object/script_language.h" |
| 35 | #include "scene/gui/option_button.h" |
| 36 | #include "scene/resources/packed_scene.h" |
| 37 | |
| 38 | void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const { |
| 39 | ResourceLoader::get_recognized_extensions_for_type("PackedScene" , r_extensions); |
| 40 | } |
| 41 | |
| 42 | Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) { |
| 43 | // Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property. |
| 44 | // These properties are translated with the tr() function in the C++ code when being set or updated. |
| 45 | |
| 46 | Error err; |
| 47 | Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene" , ResourceFormatLoader::CACHE_MODE_REUSE, &err); |
| 48 | if (err) { |
| 49 | ERR_PRINT("Failed to load " + p_path); |
| 50 | return err; |
| 51 | } |
| 52 | Ref<SceneState> state = Ref<PackedScene>(loaded_res)->get_state(); |
| 53 | |
| 54 | Vector<String> parsed_strings; |
| 55 | for (int i = 0; i < state->get_node_count(); i++) { |
| 56 | String node_type = state->get_node_type(i); |
| 57 | if (!ClassDB::is_parent_class(node_type, "Control" ) && !ClassDB::is_parent_class(node_type, "Window" )) { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | // Find the `auto_translate` property, and abort the string parsing of the node if disabled. |
| 62 | bool auto_translating = true; |
| 63 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 64 | if (state->get_node_property_name(i, j) == "auto_translate" && (bool)state->get_node_property_value(i, j) == false) { |
| 65 | auto_translating = false; |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | if (!auto_translating) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 74 | String property_name = state->get_node_property_name(i, j); |
| 75 | if (!lookup_properties.has(property_name) || (exception_list.has(node_type) && exception_list[node_type].has(property_name))) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | Variant property_value = state->get_node_property_value(i, j); |
| 80 | if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) { |
| 81 | // Parse built-in script. |
| 82 | Ref<Script> s = Object::cast_to<Script>(property_value); |
| 83 | String extension = s->get_language()->get_extension(); |
| 84 | if (EditorTranslationParser::get_singleton()->can_parse(extension)) { |
| 85 | Vector<String> temp; |
| 86 | Vector<Vector<String>> ids_context_plural; |
| 87 | EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp, &ids_context_plural); |
| 88 | parsed_strings.append_array(temp); |
| 89 | r_ids_ctx_plural->append_array(ids_context_plural); |
| 90 | } |
| 91 | } else if ((node_type == "MenuButton" || node_type == "OptionButton" ) && property_name == "items" ) { |
| 92 | Vector<String> str_values = property_value; |
| 93 | int incr_value = node_type == "MenuButton" ? PopupMenu::ITEM_PROPERTY_SIZE : OptionButton::ITEM_PROPERTY_SIZE; |
| 94 | for (int k = 0; k < str_values.size(); k += incr_value) { |
| 95 | String desc = str_values[k].get_slice(";" , 1).strip_edges(); |
| 96 | if (!desc.is_empty()) { |
| 97 | parsed_strings.push_back(desc); |
| 98 | } |
| 99 | } |
| 100 | } else if (node_type == "FileDialog" && property_name == "filters" ) { |
| 101 | // Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files". |
| 102 | Vector<String> str_values = property_value; |
| 103 | for (int k = 0; k < str_values.size(); k++) { |
| 104 | String desc = str_values[k].get_slice(";" , 1).strip_edges(); |
| 105 | if (!desc.is_empty()) { |
| 106 | parsed_strings.push_back(desc); |
| 107 | } |
| 108 | } |
| 109 | } else if (property_value.get_type() == Variant::STRING) { |
| 110 | String str_value = String(property_value); |
| 111 | // Prevent reading text containing only spaces. |
| 112 | if (!str_value.strip_edges().is_empty()) { |
| 113 | parsed_strings.push_back(str_value); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | r_ids->append_array(parsed_strings); |
| 120 | |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() { |
| 125 | // Scene Node's properties containing strings that will be fetched for translation. |
| 126 | lookup_properties.insert("text" ); |
| 127 | lookup_properties.insert("tooltip_text" ); |
| 128 | lookup_properties.insert("placeholder_text" ); |
| 129 | lookup_properties.insert("items" ); |
| 130 | lookup_properties.insert("title" ); |
| 131 | lookup_properties.insert("dialog_text" ); |
| 132 | lookup_properties.insert("filters" ); |
| 133 | lookup_properties.insert("script" ); |
| 134 | |
| 135 | // Exception list (to prevent false positives). |
| 136 | exception_list.insert("LineEdit" , { "text" }); |
| 137 | exception_list.insert("TextEdit" , { "text" }); |
| 138 | exception_list.insert("CodeEdit" , { "text" }); |
| 139 | } |
| 140 | |