Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/translations/packed_scene_translation_parser_plugin.cpp
9900 views
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
// Parse the names of children of `TabContainer`s, as they are used for tab titles.
117
if (!tabcontainer_paths.is_empty()) {
118
if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) {
119
// Switch to the previous `TabContainer` this was nested in, if that was the case.
120
tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1);
121
}
122
123
if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") &&
124
parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) {
125
r_translations->push_back({ state->get_node_name(i) });
126
}
127
}
128
if (!auto_translating) {
129
continue;
130
}
131
132
if (node_type == "TabContainer") {
133
tabcontainer_paths.push_back(String(state->get_node_path(i)));
134
}
135
136
for (int j = 0; j < state->get_node_property_count(i); j++) {
137
String property_name = state->get_node_property_name(i, j);
138
139
if (!match_property(property_name, node_type)) {
140
continue;
141
}
142
143
Variant property_value = state->get_node_property_value(i, j);
144
if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) {
145
// Parse built-in script.
146
Ref<Script> s = Object::cast_to<Script>(property_value);
147
if (!s->is_built_in()) {
148
continue;
149
}
150
151
String extension = s->get_language()->get_extension();
152
if (EditorTranslationParser::get_singleton()->can_parse(extension)) {
153
EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), r_translations);
154
}
155
} else if (node_type == "FileDialog" && property_name == "filters") {
156
// Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".
157
Vector<String> str_values = property_value;
158
for (int k = 0; k < str_values.size(); k++) {
159
String desc = str_values[k].get_slicec(';', 1).strip_edges();
160
if (!desc.is_empty()) {
161
r_translations->push_back({ desc });
162
}
163
}
164
} else if (property_value.get_type() == Variant::STRING) {
165
String str_value = String(property_value);
166
// Prevent reading text containing only spaces.
167
if (!str_value.strip_edges().is_empty()) {
168
r_translations->push_back({ str_value });
169
}
170
}
171
}
172
}
173
174
return OK;
175
}
176
177
bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) {
178
for (const KeyValue<String, Vector<String>> &exception : exception_list) {
179
const String &exception_node_type = exception.key;
180
if (ClassDB::is_parent_class(p_node_type, exception_node_type)) {
181
const Vector<String> &exception_properties = exception.value;
182
for (const String &exception_property : exception_properties) {
183
if (p_property_name.match(exception_property)) {
184
return false;
185
}
186
}
187
}
188
}
189
for (const String &lookup_property : lookup_properties) {
190
if (p_property_name.match(lookup_property)) {
191
return true;
192
}
193
}
194
return false;
195
}
196
197
PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() {
198
// Scene Node's properties containing strings that will be fetched for translation.
199
lookup_properties.insert("text");
200
lookup_properties.insert("*_text");
201
lookup_properties.insert("popup/*/text");
202
lookup_properties.insert("title");
203
lookup_properties.insert("filters");
204
lookup_properties.insert("script");
205
lookup_properties.insert("item_*/text");
206
207
// Exception list (to prevent false positives).
208
exception_list.insert("LineEdit", { "text" });
209
exception_list.insert("TextEdit", { "text" });
210
exception_list.insert("CodeEdit", { "text" });
211
}
212
213