/**************************************************************************/ /* packed_scene_translation_parser_plugin.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "core/object/script_language.h" #include "scene/gui/option_button.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_ids, Vector> *r_ids_ctx_plural) { // 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; Ref loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err); if (err) { ERR_PRINT("Failed to load " + p_path); return err; } Ref state = Ref(loaded_res)->get_state(); Vector parsed_strings; List tabcontainer_paths; for (int i = 0; i < state->get_node_count(); i++) { String node_type = state->get_node_type(i); bool is_control = ClassDB::is_parent_class(node_type, "Control"); if (!is_control && !ClassDB::is_parent_class(node_type, "Window")) { continue; } // Find the `auto_translate` property, and abort the string parsing of the node if disabled. bool auto_translating = true; for (int j = 0; j < state->get_node_property_count(i); j++) { if (state->get_node_property_name(i, j) == "auto_translate" && (bool)state->get_node_property_value(i, j) == false) { auto_translating = false; break; } } // Parse the names of children of `TabContainer`s, as they are used for tab titles. if (!tabcontainer_paths.is_empty()) { String parent_path = state->get_node_path(i, true); if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) { // Switch to the previous `TabContainer` this was nested in, if that was the case. tabcontainer_paths.pop_back(); } if (is_control && auto_translating && !tabcontainer_paths.is_empty() && parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) { parsed_strings.push_back(state->get_node_name(i)); } } if (!auto_translating) { continue; } if (node_type == "TabContainer") { tabcontainer_paths.push_back(state->get_node_path(i)); } for (int j = 0; j < state->get_node_property_count(i); j++) { String property_name = state->get_node_property_name(i, j); if (!lookup_properties.has(property_name) || (exception_list.has(node_type) && exception_list[node_type].has(property_name))) { continue; } Variant 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