Path: blob/master/editor/translations/packed_scene_translation_parser_plugin.cpp
9900 views
/**************************************************************************/1/* packed_scene_translation_parser_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "packed_scene_translation_parser_plugin.h"3132#include "core/io/resource_loader.h"33#include "core/object/script_language.h"34#include "scene/resources/packed_scene.h"3536void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {37ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions);38}3940Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {41// Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property.42// These properties are translated with the tr() function in the C++ code when being set or updated.4344Error err;45Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err);46if (err) {47ERR_PRINT("Failed to load " + p_path);48return err;49}50Ref<PackedScene> packed_scene = loaded_res;51ERR_FAIL_COND_V_MSG(packed_scene.is_null(), ERR_FILE_UNRECOGNIZED, vformat("'%s' is not a valid PackedScene resource.", p_path));52Ref<SceneState> state = packed_scene->get_state();5354Vector<Pair<NodePath, bool>> atr_owners;55Vector<String> tabcontainer_paths;5657for (int i = 0; i < state->get_node_count(); i++) {58String node_type = state->get_node_type(i);59String parent_path = String(state->get_node_path(i, true));6061// Handle instanced scenes.62if (node_type.is_empty()) {63Ref<PackedScene> instance = state->get_node_instance(i);64if (instance.is_valid()) {65Ref<SceneState> _state = instance->get_state();66node_type = _state->get_node_type(0);67}68}6970// Find the `auto_translate_mode` property.71bool auto_translating = true;72bool auto_translate_mode_found = false;73for (int j = 0; j < state->get_node_property_count(i); j++) {74String property = state->get_node_property_name(i, j);75// If an old scene wasn't saved in the new version, the `auto_translate` property won't be converted into `auto_translate_mode`,76// so the deprecated property still needs to be checked as well.77// TODO: Remove the check for "auto_translate" once the property if fully removed.78if (property != "auto_translate_mode" && property != "auto_translate") {79continue;80}8182auto_translate_mode_found = true;8384int idx_last = atr_owners.size() - 1;85if (idx_last > 0 && !parent_path.begins_with(String(atr_owners[idx_last].first))) {86// Exit from the current owner nesting into the previous one.87atr_owners.remove_at(idx_last);88}8990if (property == "auto_translate_mode") {91int auto_translate_mode = (int)state->get_node_property_value(i, j);92if (auto_translate_mode == Node::AUTO_TRANSLATE_MODE_DISABLED) {93auto_translating = false;94}95} else {96// TODO: Remove this once `auto_translate` is fully removed.97auto_translating = (bool)state->get_node_property_value(i, j);98}99100atr_owners.push_back(Pair(state->get_node_path(i), auto_translating));101102break;103}104105// If `auto_translate_mode` wasn't found, that means it is set to its default value (`AUTO_TRANSLATE_MODE_INHERIT`).106if (!auto_translate_mode_found) {107int idx_last = atr_owners.size() - 1;108if (idx_last >= 0 && parent_path.begins_with(String(atr_owners[idx_last].first))) {109auto_translating = atr_owners[idx_last].second;110} else {111atr_owners.push_back(Pair(state->get_node_path(i), true));112}113}114115// Parse the names of children of `TabContainer`s, as they are used for tab titles.116if (!tabcontainer_paths.is_empty()) {117if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) {118// Switch to the previous `TabContainer` this was nested in, if that was the case.119tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1);120}121122if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") &&123parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) {124r_translations->push_back({ state->get_node_name(i) });125}126}127if (!auto_translating) {128continue;129}130131if (node_type == "TabContainer") {132tabcontainer_paths.push_back(String(state->get_node_path(i)));133}134135for (int j = 0; j < state->get_node_property_count(i); j++) {136String property_name = state->get_node_property_name(i, j);137138if (!match_property(property_name, node_type)) {139continue;140}141142Variant property_value = state->get_node_property_value(i, j);143if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) {144// Parse built-in script.145Ref<Script> s = Object::cast_to<Script>(property_value);146if (!s->is_built_in()) {147continue;148}149150String extension = s->get_language()->get_extension();151if (EditorTranslationParser::get_singleton()->can_parse(extension)) {152EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), r_translations);153}154} else if (node_type == "FileDialog" && property_name == "filters") {155// Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".156Vector<String> str_values = property_value;157for (int k = 0; k < str_values.size(); k++) {158String desc = str_values[k].get_slicec(';', 1).strip_edges();159if (!desc.is_empty()) {160r_translations->push_back({ desc });161}162}163} else if (property_value.get_type() == Variant::STRING) {164String str_value = String(property_value);165// Prevent reading text containing only spaces.166if (!str_value.strip_edges().is_empty()) {167r_translations->push_back({ str_value });168}169}170}171}172173return OK;174}175176bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) {177for (const KeyValue<String, Vector<String>> &exception : exception_list) {178const String &exception_node_type = exception.key;179if (ClassDB::is_parent_class(p_node_type, exception_node_type)) {180const Vector<String> &exception_properties = exception.value;181for (const String &exception_property : exception_properties) {182if (p_property_name.match(exception_property)) {183return false;184}185}186}187}188for (const String &lookup_property : lookup_properties) {189if (p_property_name.match(lookup_property)) {190return true;191}192}193return false;194}195196PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() {197// Scene Node's properties containing strings that will be fetched for translation.198lookup_properties.insert("text");199lookup_properties.insert("*_text");200lookup_properties.insert("popup/*/text");201lookup_properties.insert("title");202lookup_properties.insert("filters");203lookup_properties.insert("script");204lookup_properties.insert("item_*/text");205206// Exception list (to prevent false positives).207exception_list.insert("LineEdit", { "text" });208exception_list.insert("TextEdit", { "text" });209exception_list.insert("CodeEdit", { "text" });210}211212213