Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp
10278 views
1
/**************************************************************************/
2
/* gdscript_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 "gdscript_translation_parser_plugin.h"
32
33
#include "../gdscript.h"
34
#include "../gdscript_analyzer.h"
35
36
#include "core/io/resource_loader.h"
37
38
void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
39
GDScriptLanguage::get_singleton()->get_recognized_extensions(r_extensions);
40
}
41
42
Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {
43
// Extract all translatable strings using the parsed tree from GDScriptParser.
44
// The strategy is to find all ExpressionNode and AssignmentNode from the tree and extract strings if relevant, i.e
45
// Search strings in ExpressionNode -> CallNode -> tr(), set_text(), set_placeholder() etc.
46
// Search strings in AssignmentNode -> text = "__", tooltip_text = "__" etc.
47
48
Error err;
49
Ref<Resource> loaded_res = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
50
ERR_FAIL_COND_V_MSG(err, err, "Failed to load " + p_path);
51
52
translations = r_translations;
53
54
Ref<GDScript> gdscript = loaded_res;
55
String source_code = gdscript->get_source_code();
56
57
GDScriptParser parser;
58
err = parser.parse(source_code, p_path, false);
59
ERR_FAIL_COND_V_MSG(err, err, "Failed to parse GDScript with GDScriptParser.");
60
61
GDScriptAnalyzer analyzer(&parser);
62
err = analyzer.analyze();
63
ERR_FAIL_COND_V_MSG(err, err, "Failed to analyze GDScript with GDScriptAnalyzer.");
64
65
comment_data = &parser.comment_data;
66
67
// Traverse through the parsed tree from GDScriptParser.
68
GDScriptParser::ClassNode *c = parser.get_tree();
69
_traverse_class(c);
70
71
comment_data = nullptr;
72
73
return OK;
74
}
75
76
bool GDScriptEditorTranslationParserPlugin::_is_constant_string(const GDScriptParser::ExpressionNode *p_expression) {
77
ERR_FAIL_NULL_V(p_expression, false);
78
return p_expression->is_constant && p_expression->reduced_value.is_string();
79
}
80
81
String GDScriptEditorTranslationParserPlugin::_parse_comment(int p_line, bool &r_skip) const {
82
// Parse inline comment.
83
if (comment_data->has(p_line)) {
84
const String stripped_comment = comment_data->get(p_line).comment.trim_prefix("#").strip_edges();
85
86
if (stripped_comment.begins_with("TRANSLATORS:")) {
87
return stripped_comment.trim_prefix("TRANSLATORS:").strip_edges(true, false);
88
}
89
if (stripped_comment == "NO_TRANSLATE" || stripped_comment.begins_with("NO_TRANSLATE:")) {
90
r_skip = true;
91
return String();
92
}
93
}
94
95
// Parse multiline comment.
96
String multiline_comment;
97
for (int line = p_line - 1; comment_data->has(line) && comment_data->get(line).new_line; line--) {
98
const String stripped_comment = comment_data->get(line).comment.trim_prefix("#").strip_edges();
99
100
if (stripped_comment.is_empty()) {
101
continue;
102
}
103
104
if (multiline_comment.is_empty()) {
105
multiline_comment = stripped_comment;
106
} else {
107
multiline_comment = stripped_comment + "\n" + multiline_comment;
108
}
109
110
if (stripped_comment.begins_with("TRANSLATORS:")) {
111
return multiline_comment.trim_prefix("TRANSLATORS:").strip_edges(true, false);
112
}
113
if (stripped_comment == "NO_TRANSLATE" || stripped_comment.begins_with("NO_TRANSLATE:")) {
114
r_skip = true;
115
return String();
116
}
117
}
118
119
return String();
120
}
121
122
void GDScriptEditorTranslationParserPlugin::_add_id(const String &p_id, int p_line) {
123
bool skip = false;
124
const String comment = _parse_comment(p_line, skip);
125
if (skip) {
126
return;
127
}
128
129
translations->push_back({ p_id, String(), String(), comment });
130
}
131
132
void GDScriptEditorTranslationParserPlugin::_add_id_ctx_plural(const Vector<String> &p_id_ctx_plural, int p_line) {
133
bool skip = false;
134
const String comment = _parse_comment(p_line, skip);
135
if (skip) {
136
return;
137
}
138
139
translations->push_back({ p_id_ctx_plural[0], p_id_ctx_plural[1], p_id_ctx_plural[2], comment });
140
}
141
142
void GDScriptEditorTranslationParserPlugin::_traverse_class(const GDScriptParser::ClassNode *p_class) {
143
for (int i = 0; i < p_class->members.size(); i++) {
144
const GDScriptParser::ClassNode::Member &m = p_class->members[i];
145
// Other member types can't contain translatable strings.
146
switch (m.type) {
147
case GDScriptParser::ClassNode::Member::CLASS:
148
_traverse_class(m.m_class);
149
break;
150
case GDScriptParser::ClassNode::Member::FUNCTION:
151
_traverse_function(m.function);
152
break;
153
case GDScriptParser::ClassNode::Member::VARIABLE:
154
_assess_expression(m.variable->initializer);
155
if (m.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
156
_traverse_function(m.variable->setter);
157
_traverse_function(m.variable->getter);
158
}
159
break;
160
default:
161
break;
162
}
163
}
164
}
165
166
void GDScriptEditorTranslationParserPlugin::_traverse_function(const GDScriptParser::FunctionNode *p_func) {
167
if (!p_func) {
168
return;
169
}
170
171
for (int i = 0; i < p_func->parameters.size(); i++) {
172
_assess_expression(p_func->parameters[i]->initializer);
173
}
174
_traverse_block(p_func->body);
175
}
176
177
void GDScriptEditorTranslationParserPlugin::_traverse_block(const GDScriptParser::SuiteNode *p_suite) {
178
if (!p_suite) {
179
return;
180
}
181
182
const Vector<GDScriptParser::Node *> &statements = p_suite->statements;
183
for (int i = 0; i < statements.size(); i++) {
184
const GDScriptParser::Node *statement = statements[i];
185
186
// BREAK, BREAKPOINT, CONSTANT, CONTINUE, and PASS are skipped because they can't contain translatable strings.
187
switch (statement->type) {
188
case GDScriptParser::Node::ASSERT: {
189
const GDScriptParser::AssertNode *assert_node = static_cast<const GDScriptParser::AssertNode *>(statement);
190
_assess_expression(assert_node->condition);
191
_assess_expression(assert_node->message);
192
} break;
193
case GDScriptParser::Node::ASSIGNMENT: {
194
_assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(statement));
195
} break;
196
case GDScriptParser::Node::FOR: {
197
const GDScriptParser::ForNode *for_node = static_cast<const GDScriptParser::ForNode *>(statement);
198
_assess_expression(for_node->list);
199
_traverse_block(for_node->loop);
200
} break;
201
case GDScriptParser::Node::IF: {
202
const GDScriptParser::IfNode *if_node = static_cast<const GDScriptParser::IfNode *>(statement);
203
_assess_expression(if_node->condition);
204
_traverse_block(if_node->true_block);
205
_traverse_block(if_node->false_block);
206
} break;
207
case GDScriptParser::Node::MATCH: {
208
const GDScriptParser::MatchNode *match_node = static_cast<const GDScriptParser::MatchNode *>(statement);
209
_assess_expression(match_node->test);
210
for (int j = 0; j < match_node->branches.size(); j++) {
211
_traverse_block(match_node->branches[j]->guard_body);
212
_traverse_block(match_node->branches[j]->block);
213
}
214
} break;
215
case GDScriptParser::Node::RETURN: {
216
_assess_expression(static_cast<const GDScriptParser::ReturnNode *>(statement)->return_value);
217
} break;
218
case GDScriptParser::Node::VARIABLE: {
219
_assess_expression(static_cast<const GDScriptParser::VariableNode *>(statement)->initializer);
220
} break;
221
case GDScriptParser::Node::WHILE: {
222
const GDScriptParser::WhileNode *while_node = static_cast<const GDScriptParser::WhileNode *>(statement);
223
_assess_expression(while_node->condition);
224
_traverse_block(while_node->loop);
225
} break;
226
default: {
227
if (statement->is_expression()) {
228
_assess_expression(static_cast<const GDScriptParser::ExpressionNode *>(statement));
229
}
230
} break;
231
}
232
}
233
}
234
235
void GDScriptEditorTranslationParserPlugin::_assess_expression(const GDScriptParser::ExpressionNode *p_expression) {
236
// Explore all ExpressionNodes to find CallNodes which contain translation strings, such as tr(), set_text() etc.
237
// tr() can be embedded quite deep within multiple ExpressionNodes so need to dig down to search through all ExpressionNodes.
238
if (!p_expression) {
239
return;
240
}
241
242
// GET_NODE, IDENTIFIER, LITERAL, PRELOAD, SELF, and TYPE are skipped because they can't contain translatable strings.
243
switch (p_expression->type) {
244
case GDScriptParser::Node::ARRAY: {
245
const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
246
for (int i = 0; i < array_node->elements.size(); i++) {
247
_assess_expression(array_node->elements[i]);
248
}
249
} break;
250
case GDScriptParser::Node::ASSIGNMENT: {
251
_assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(p_expression));
252
} break;
253
case GDScriptParser::Node::AWAIT: {
254
_assess_expression(static_cast<const GDScriptParser::AwaitNode *>(p_expression)->to_await);
255
} break;
256
case GDScriptParser::Node::BINARY_OPERATOR: {
257
const GDScriptParser::BinaryOpNode *binary_op_node = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
258
_assess_expression(binary_op_node->left_operand);
259
_assess_expression(binary_op_node->right_operand);
260
} break;
261
case GDScriptParser::Node::CALL: {
262
_assess_call(static_cast<const GDScriptParser::CallNode *>(p_expression));
263
} break;
264
case GDScriptParser::Node::CAST: {
265
_assess_expression(static_cast<const GDScriptParser::CastNode *>(p_expression)->operand);
266
} break;
267
case GDScriptParser::Node::DICTIONARY: {
268
const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
269
for (int i = 0; i < dict_node->elements.size(); i++) {
270
_assess_expression(dict_node->elements[i].key);
271
_assess_expression(dict_node->elements[i].value);
272
}
273
} break;
274
case GDScriptParser::Node::LAMBDA: {
275
_traverse_function(static_cast<const GDScriptParser::LambdaNode *>(p_expression)->function);
276
} break;
277
case GDScriptParser::Node::SUBSCRIPT: {
278
const GDScriptParser::SubscriptNode *subscript_node = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
279
_assess_expression(subscript_node->base);
280
if (!subscript_node->is_attribute) {
281
_assess_expression(subscript_node->index);
282
}
283
} break;
284
case GDScriptParser::Node::TERNARY_OPERATOR: {
285
const GDScriptParser::TernaryOpNode *ternary_op_node = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression);
286
_assess_expression(ternary_op_node->condition);
287
_assess_expression(ternary_op_node->true_expr);
288
_assess_expression(ternary_op_node->false_expr);
289
} break;
290
case GDScriptParser::Node::TYPE_TEST: {
291
_assess_expression(static_cast<const GDScriptParser::TypeTestNode *>(p_expression)->operand);
292
} break;
293
case GDScriptParser::Node::UNARY_OPERATOR: {
294
_assess_expression(static_cast<const GDScriptParser::UnaryOpNode *>(p_expression)->operand);
295
} break;
296
default: {
297
} break;
298
}
299
}
300
301
void GDScriptEditorTranslationParserPlugin::_assess_assignment(const GDScriptParser::AssignmentNode *p_assignment) {
302
_assess_expression(p_assignment->assignee);
303
_assess_expression(p_assignment->assigned_value);
304
305
// Extract the translatable strings coming from assignments. For example, get_node("Label").text = "____"
306
307
StringName assignee_name;
308
if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
309
assignee_name = static_cast<const GDScriptParser::IdentifierNode *>(p_assignment->assignee)->name;
310
} else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
311
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_assignment->assignee);
312
if (subscript->is_attribute && subscript->attribute) {
313
assignee_name = subscript->attribute->name;
314
} else if (subscript->index && _is_constant_string(subscript->index)) {
315
assignee_name = subscript->index->reduced_value;
316
}
317
}
318
319
if (assignee_name != StringName() && assignment_patterns.has(assignee_name) && _is_constant_string(p_assignment->assigned_value)) {
320
// If the assignment is towards one of the extract patterns (text, tooltip_text etc.), and the value is a constant string, we collect the string.
321
_add_id(p_assignment->assigned_value->reduced_value, p_assignment->assigned_value->start_line);
322
} else if (assignee_name == fd_filters) {
323
// Extract from `get_node("FileDialog").filters = <filter array>`.
324
_extract_fd_filter_array(p_assignment->assigned_value);
325
}
326
}
327
328
void GDScriptEditorTranslationParserPlugin::_assess_call(const GDScriptParser::CallNode *p_call) {
329
_assess_expression(p_call->callee);
330
for (int i = 0; i < p_call->arguments.size(); i++) {
331
_assess_expression(p_call->arguments[i]);
332
}
333
334
// Extract the translatable strings coming from function calls. For example:
335
// tr("___"), get_node("Label").set_text("____"), get_node("LineEdit").set_placeholder("____").
336
337
StringName function_name = p_call->function_name;
338
339
// Variables for extracting tr() and tr_n().
340
Vector<String> id_ctx_plural;
341
id_ctx_plural.resize(3);
342
bool extract_id_ctx_plural = true;
343
344
if (function_name == tr_func || function_name == atr_func) {
345
// Extract from `tr(id, ctx)` or `atr(id, ctx)`.
346
for (int i = 0; i < p_call->arguments.size(); i++) {
347
if (_is_constant_string(p_call->arguments[i])) {
348
id_ctx_plural.write[i] = p_call->arguments[i]->reduced_value;
349
} else {
350
// Avoid adding something like tr("Flying dragon", var_context_level_1). We want to extract both id and context together.
351
extract_id_ctx_plural = false;
352
}
353
}
354
if (extract_id_ctx_plural) {
355
_add_id_ctx_plural(id_ctx_plural, p_call->start_line);
356
}
357
} else if (function_name == trn_func || function_name == atrn_func) {
358
// Extract from `tr_n(id, plural, n, ctx)` or `atr_n(id, plural, n, ctx)`.
359
Vector<int> indices;
360
indices.push_back(0);
361
indices.push_back(3);
362
indices.push_back(1);
363
for (int i = 0; i < indices.size(); i++) {
364
if (indices[i] >= p_call->arguments.size()) {
365
continue;
366
}
367
368
if (_is_constant_string(p_call->arguments[indices[i]])) {
369
id_ctx_plural.write[i] = p_call->arguments[indices[i]]->reduced_value;
370
} else {
371
extract_id_ctx_plural = false;
372
}
373
}
374
if (extract_id_ctx_plural) {
375
_add_id_ctx_plural(id_ctx_plural, p_call->start_line);
376
}
377
} else if (first_arg_patterns.has(function_name)) {
378
if (!p_call->arguments.is_empty() && _is_constant_string(p_call->arguments[0])) {
379
_add_id(p_call->arguments[0]->reduced_value, p_call->arguments[0]->start_line);
380
}
381
} else if (second_arg_patterns.has(function_name)) {
382
if (p_call->arguments.size() > 1 && _is_constant_string(p_call->arguments[1])) {
383
_add_id(p_call->arguments[1]->reduced_value, p_call->arguments[1]->start_line);
384
}
385
} else if (function_name == fd_add_filter) {
386
// Extract the 'JPE Images' in this example - get_node("FileDialog").add_filter("*.jpg; JPE Images").
387
if (!p_call->arguments.is_empty()) {
388
_extract_fd_filter_string(p_call->arguments[0], p_call->arguments[0]->start_line);
389
}
390
} else if (function_name == fd_set_filter) {
391
// Extract from `get_node("FileDialog").set_filters(<filter array>)`.
392
if (!p_call->arguments.is_empty()) {
393
_extract_fd_filter_array(p_call->arguments[0]);
394
}
395
}
396
}
397
398
void GDScriptEditorTranslationParserPlugin::_extract_fd_filter_string(const GDScriptParser::ExpressionNode *p_expression, int p_line) {
399
// Extract the name in "extension ; name".
400
if (_is_constant_string(p_expression)) {
401
PackedStringArray arr = p_expression->reduced_value.operator String().split(";", true);
402
ERR_FAIL_COND_MSG(arr.size() != 2, "Argument for setting FileDialog has bad format.");
403
_add_id(arr[1].strip_edges(), p_line);
404
}
405
}
406
407
void GDScriptEditorTranslationParserPlugin::_extract_fd_filter_array(const GDScriptParser::ExpressionNode *p_expression) {
408
const GDScriptParser::ArrayNode *array_node = nullptr;
409
410
if (p_expression->type == GDScriptParser::Node::ARRAY) {
411
// Extract from `["*.png ; PNG Images","*.gd ; GDScript Files"]` (implicit cast to `PackedStringArray`).
412
array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
413
} else if (p_expression->type == GDScriptParser::Node::CALL) {
414
// Extract from `PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"])`.
415
const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(p_expression);
416
if (call_node->get_callee_type() == GDScriptParser::Node::IDENTIFIER && call_node->function_name == SNAME("PackedStringArray") && !call_node->arguments.is_empty() && call_node->arguments[0]->type == GDScriptParser::Node::ARRAY) {
417
array_node = static_cast<const GDScriptParser::ArrayNode *>(call_node->arguments[0]);
418
}
419
}
420
421
if (array_node) {
422
for (int i = 0; i < array_node->elements.size(); i++) {
423
_extract_fd_filter_string(array_node->elements[i], array_node->elements[i]->start_line);
424
}
425
}
426
}
427
428
GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
429
assignment_patterns.insert("text");
430
assignment_patterns.insert("placeholder_text");
431
assignment_patterns.insert("tooltip_text");
432
433
first_arg_patterns.insert("set_text");
434
first_arg_patterns.insert("set_tooltip_text");
435
first_arg_patterns.insert("set_placeholder");
436
first_arg_patterns.insert("add_tab");
437
first_arg_patterns.insert("add_check_item");
438
first_arg_patterns.insert("add_item");
439
first_arg_patterns.insert("add_multistate_item");
440
first_arg_patterns.insert("add_radio_check_item");
441
first_arg_patterns.insert("add_separator");
442
first_arg_patterns.insert("add_submenu_item");
443
444
second_arg_patterns.insert("set_tab_title");
445
second_arg_patterns.insert("add_icon_check_item");
446
second_arg_patterns.insert("add_icon_item");
447
second_arg_patterns.insert("add_icon_radio_check_item");
448
second_arg_patterns.insert("set_item_text");
449
}
450
451