Path: blob/master/editor/translations/packed_scene_translation_parser_plugin.cpp
20951 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// Handle the `tooltip_auto_translate_mode` property separately.116String tooltip_text;117bool tooltip_auto_translating = auto_translating;118for (int j = 0; j < state->get_node_property_count(i); j++) {119String property = state->get_node_property_name(i, j);120if (property == "tooltip_text") {121tooltip_text = (String)state->get_node_property_value(i, j);122continue;123}124if (property == "tooltip_auto_translate_mode") {125int mode = (int)state->get_node_property_value(i, j);126switch (mode) {127case Node::AUTO_TRANSLATE_MODE_ALWAYS: {128tooltip_auto_translating = true;129} break;130case Node::AUTO_TRANSLATE_MODE_DISABLED: {131tooltip_auto_translating = false;132} break;133}134continue;135}136}137if (!tooltip_text.is_empty() && tooltip_auto_translating) {138r_translations->push_back({ tooltip_text });139}140141// Parse the names of children of `TabContainer`s, as they are used for tab titles.142if (!tabcontainer_paths.is_empty()) {143if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) {144// Switch to the previous `TabContainer` this was nested in, if that was the case.145tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1);146}147148if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") &&149parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) {150r_translations->push_back({ state->get_node_name(i) });151}152}153if (!auto_translating) {154continue;155}156157if (node_type == "TabContainer") {158tabcontainer_paths.push_back(String(state->get_node_path(i)));159}160161for (int j = 0; j < state->get_node_property_count(i); j++) {162String property_name = state->get_node_property_name(i, j);163164if (!match_property(property_name, node_type)) {165continue;166}167168Variant property_value = state->get_node_property_value(i, j);169if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) {170// Parse built-in script.171Ref<Script> s = Object::cast_to<Script>(property_value);172if (s.is_null() || !s->is_built_in()) {173continue;174}175176String extension = s->get_language()->get_extension();177if (EditorTranslationParser::get_singleton()->can_parse(extension)) {178EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), r_translations);179}180} else if ((node_type == "FileDialog" || node_type == "EditorFileDialog") && property_name == "filters") {181// Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".182Vector<String> str_values = property_value;183for (int k = 0; k < str_values.size(); k++) {184String desc = str_values[k].get_slicec(';', 1).strip_edges();185if (!desc.is_empty()) {186r_translations->push_back({ desc });187}188}189} else if (property_value.get_type() == Variant::STRING) {190String str_value = String(property_value);191// Prevent reading text containing only spaces.192if (!str_value.strip_edges().is_empty()) {193r_translations->push_back({ str_value });194}195}196}197}198199return OK;200}201202bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) {203for (const KeyValue<String, Vector<String>> &exception : exception_list) {204const String &exception_node_type = exception.key;205if (ClassDB::is_parent_class(p_node_type, exception_node_type)) {206const Vector<String> &exception_properties = exception.value;207for (const String &exception_property : exception_properties) {208if (p_property_name.match(exception_property)) {209return false;210}211}212}213}214for (const String &lookup_property : lookup_properties) {215if (p_property_name.match(lookup_property)) {216return true;217}218}219return false;220}221222PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() {223// Scene Node's properties containing strings that will be fetched for translation.224lookup_properties.insert("text");225lookup_properties.insert("*_text");226lookup_properties.insert("popup/*/text");227lookup_properties.insert("title");228lookup_properties.insert("filters");229lookup_properties.insert("script");230lookup_properties.insert("item_*/text");231232// Exception list (to prevent false positives).233exception_list.insert("LineEdit", { "text" });234exception_list.insert("TextEdit", { "text" });235exception_list.insert("CodeEdit", { "text" });236exception_list.insert("Control", { "tooltip_text" });237}238239240