| 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/resources/packed_scene.h" |
| 36 | |
| 37 | void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const { |
| 38 | ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions); |
| 39 | } |
| 40 | |
| 41 | Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) { |
| 42 | // Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property. |
| 43 | // These properties are translated with the tr() function in the C++ code when being set or updated. |
| 44 | |
| 45 | Error err; |
| 46 | Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err); |
| 47 | if (err) { |
| 48 | ERR_PRINT("Failed to load " + p_path); |
| 49 | return err; |
| 50 | } |
| 51 | Ref<PackedScene> packed_scene = loaded_res; |
| 52 | ERR_FAIL_COND_V_MSG(packed_scene.is_null(), ERR_FILE_UNRECOGNIZED, vformat("'%s' is not a valid PackedScene resource.", p_path)); |
| 53 | Ref<SceneState> state = packed_scene->get_state(); |
| 54 | |
| 55 | Vector<Pair<NodePath, bool>> atr_owners; |
| 56 | Vector<String> tabcontainer_paths; |
| 57 | |
| 58 | for (int i = 0; i < state->get_node_count(); i++) { |
| 59 | String node_type = state->get_node_type(i); |
| 60 | String parent_path = String(state->get_node_path(i, true)); |
| 61 | |
| 62 | // Handle instanced scenes. |
| 63 | if (node_type.is_empty()) { |
| 64 | Ref<PackedScene> instance = state->get_node_instance(i); |
| 65 | if (instance.is_valid()) { |
| 66 | Ref<SceneState> _state = instance->get_state(); |
| 67 | node_type = _state->get_node_type(0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Find the `auto_translate_mode` property. |
| 72 | bool auto_translating = true; |
| 73 | bool auto_translate_mode_found = false; |
| 74 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 75 | String property = state->get_node_property_name(i, j); |
| 76 | // If an old scene wasn't saved in the new version, the `auto_translate` property won't be converted into `auto_translate_mode`, |
| 77 | // so the deprecated property still needs to be checked as well. |
| 78 | // TODO: Remove the check for "auto_translate" once the property if fully removed. |
| 79 | if (property != "auto_translate_mode" && property != "auto_translate") { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | auto_translate_mode_found = true; |
| 84 | |
| 85 | int idx_last = atr_owners.size() - 1; |
| 86 | if (idx_last > 0 && !parent_path.begins_with(String(atr_owners[idx_last].first))) { |
| 87 | // Exit from the current owner nesting into the previous one. |
| 88 | atr_owners.remove_at(idx_last); |
| 89 | } |
| 90 | |
| 91 | if (property == "auto_translate_mode") { |
| 92 | int auto_translate_mode = (int)state->get_node_property_value(i, j); |
| 93 | if (auto_translate_mode == Node::AUTO_TRANSLATE_MODE_DISABLED) { |
| 94 | auto_translating = false; |
| 95 | } |
| 96 | } else { |
| 97 | // TODO: Remove this once `auto_translate` is fully removed. |
| 98 | auto_translating = (bool)state->get_node_property_value(i, j); |
| 99 | } |
| 100 | |
| 101 | atr_owners.push_back(Pair(state->get_node_path(i), auto_translating)); |
| 102 | |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | // If `auto_translate_mode` wasn't found, that means it is set to its default value (`AUTO_TRANSLATE_MODE_INHERIT`). |
| 107 | if (!auto_translate_mode_found) { |
| 108 | int idx_last = atr_owners.size() - 1; |
| 109 | if (idx_last >= 0 && parent_path.begins_with(String(atr_owners[idx_last].first))) { |
| 110 | auto_translating = atr_owners[idx_last].second; |
| 111 | } else { |
| 112 | atr_owners.push_back(Pair(state->get_node_path(i), true)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Handle the `tooltip_auto_translate_mode` property separately. |
| 117 | String tooltip_text; |
| 118 | bool tooltip_auto_translating = auto_translating; |
| 119 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 120 | String property = state->get_node_property_name(i, j); |
| 121 | if (property == "tooltip_text") { |
| 122 | tooltip_text = (String)state->get_node_property_value(i, j); |
| 123 | continue; |
| 124 | } |
| 125 | if (property == "tooltip_auto_translate_mode") { |
| 126 | int mode = (int)state->get_node_property_value(i, j); |
| 127 | switch (mode) { |
| 128 | case Node::AUTO_TRANSLATE_MODE_ALWAYS: { |
| 129 | tooltip_auto_translating = true; |
| 130 | } break; |
| 131 | case Node::AUTO_TRANSLATE_MODE_DISABLED: { |
| 132 | tooltip_auto_translating = false; |
| 133 | } break; |
| 134 | } |
| 135 | continue; |
| 136 | } |
| 137 | } |
| 138 | if (!tooltip_text.is_empty() && tooltip_auto_translating) { |
| 139 | r_translations->push_back({ tooltip_text }); |
| 140 | } |
| 141 | |
| 142 | // Parse the names of children of `TabContainer`s, as they are used for tab titles. |
| 143 | if (!tabcontainer_paths.is_empty()) { |
| 144 | if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) { |
| 145 | // Switch to the previous `TabContainer` this was nested in, if that was the case. |
| 146 | tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1); |
| 147 | } |
| 148 | |
| 149 | if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") && |
| 150 | parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) { |
| 151 | r_translations->push_back({ state->get_node_name(i) }); |
| 152 | } |
| 153 | } |
| 154 | if (!auto_translating) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | if (node_type == "TabContainer") { |
| 159 | tabcontainer_paths.push_back(String(state->get_node_path(i))); |
| 160 | } |
| 161 | |
| 162 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 163 | String property_name = state->get_node_property_name(i, j); |
| 164 | |
| 165 | if (!match_property(property_name, node_type)) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | Variant property_value = state->get_node_property_value(i, j); |
| 170 | if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) { |
| 171 | // Parse built-in script. |
| 172 | Ref<Script> s = Object::cast_to<Script>(property_value); |
| 173 | if (s.is_null() || !s->is_built_in()) { |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | String extension = s->get_language()->get_extension(); |
| 178 | if (EditorTranslationParser::get_singleton()->can_parse(extension)) { |
| 179 | EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), r_translations); |
| 180 | } |
| 181 | } else if ((node_type == "FileDialog" || node_type == "EditorFileDialog") && property_name == "filters") { |
| 182 | // Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files". |
| 183 | Vector<String> str_values = property_value; |
| 184 | for (int k = 0; k < str_values.size(); k++) { |
| 185 | String desc = str_values[k].get_slicec(';', 1).strip_edges(); |
| 186 | if (!desc.is_empty()) { |
| 187 | r_translations->push_back({ desc }); |
| 188 | } |
| 189 | } |
| 190 | } else if (property_value.get_type() == Variant::STRING) { |
| 191 | String str_value = String(property_value); |
| 192 | // Prevent reading text containing only spaces. |
| 193 | if (!str_value.strip_edges().is_empty()) { |
| 194 | r_translations->push_back({ str_value }); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) { |
| 204 | for (const KeyValue<String, Vector<String>> &exception : exception_list) { |
| 205 | const String &exception_node_type = exception.key; |
| 206 | if (ClassDB::is_parent_class(p_node_type, exception_node_type)) { |
| 207 | const Vector<String> &exception_properties = exception.value; |
| 208 | for (const String &exception_property : exception_properties) { |
| 209 | if (p_property_name.match(exception_property)) { |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | for (const String &lookup_property : lookup_properties) { |
| 216 | if (p_property_name.match(lookup_property)) { |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() { |
| 224 | // Scene Node's properties containing strings that will be fetched for translation. |
| 225 | lookup_properties.insert("text"); |
| 226 | lookup_properties.insert("*_text"); |
| 227 | lookup_properties.insert("popup/*/text"); |
| 228 | lookup_properties.insert("title"); |
| 229 | lookup_properties.insert("filters"); |
| 230 | lookup_properties.insert("script"); |
| 231 | lookup_properties.insert("item_*/text"); |
| 232 | |
| 233 | // Exception list (to prevent false positives). |
| 234 | exception_list.insert("LineEdit", { "text" }); |
| 235 | exception_list.insert("TextEdit", { "text" }); |
| 236 | exception_list.insert("CodeEdit", { "text" }); |
| 237 | exception_list.insert("Control", { "tooltip_text" }); |
| 238 | } |