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<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) { |
43 | // Extract all translatable strings using the parsed tree from GDSriptParser. |
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 | if (err) { |
51 | ERR_PRINT("Failed to load " + p_path); |
52 | return err; |
53 | } |
54 | |
55 | ids = r_ids; |
56 | ids_ctx_plural = r_ids_ctx_plural; |
57 | Ref<GDScript> gdscript = loaded_res; |
58 | String source_code = gdscript->get_source_code(); |
59 | |
60 | GDScriptParser parser; |
61 | err = parser.parse(source_code, p_path, false); |
62 | ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to parse GDScript with GDScriptParser." ); |
63 | |
64 | GDScriptAnalyzer analyzer(&parser); |
65 | err = analyzer.analyze(); |
66 | ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to analyze GDScript with GDScriptAnalyzer." ); |
67 | |
68 | // Traverse through the parsed tree from GDScriptParser. |
69 | GDScriptParser::ClassNode *c = parser.get_tree(); |
70 | _traverse_class(c); |
71 | |
72 | return OK; |
73 | } |
74 | |
75 | bool GDScriptEditorTranslationParserPlugin::_is_constant_string(const GDScriptParser::ExpressionNode *p_expression) { |
76 | ERR_FAIL_NULL_V(p_expression, false); |
77 | return p_expression->is_constant && (p_expression->reduced_value.get_type() == Variant::STRING || p_expression->reduced_value.get_type() == Variant::STRING_NAME); |
78 | } |
79 | |
80 | void GDScriptEditorTranslationParserPlugin::_traverse_class(const GDScriptParser::ClassNode *p_class) { |
81 | for (int i = 0; i < p_class->members.size(); i++) { |
82 | const GDScriptParser::ClassNode::Member &m = p_class->members[i]; |
83 | // There are 7 types of Member, but only class, function and variable can contain translatable strings. |
84 | switch (m.type) { |
85 | case GDScriptParser::ClassNode::Member::CLASS: |
86 | _traverse_class(m.m_class); |
87 | break; |
88 | case GDScriptParser::ClassNode::Member::FUNCTION: |
89 | _traverse_function(m.function); |
90 | break; |
91 | case GDScriptParser::ClassNode::Member::VARIABLE: |
92 | _read_variable(m.variable); |
93 | break; |
94 | default: |
95 | break; |
96 | } |
97 | } |
98 | } |
99 | |
100 | void GDScriptEditorTranslationParserPlugin::_traverse_function(const GDScriptParser::FunctionNode *p_func) { |
101 | _traverse_block(p_func->body); |
102 | } |
103 | |
104 | void GDScriptEditorTranslationParserPlugin::_read_variable(const GDScriptParser::VariableNode *p_var) { |
105 | _assess_expression(p_var->initializer); |
106 | } |
107 | |
108 | void GDScriptEditorTranslationParserPlugin::_traverse_block(const GDScriptParser::SuiteNode *p_suite) { |
109 | if (!p_suite) { |
110 | return; |
111 | } |
112 | |
113 | const Vector<GDScriptParser::Node *> &statements = p_suite->statements; |
114 | for (int i = 0; i < statements.size(); i++) { |
115 | const GDScriptParser::Node *statement = statements[i]; |
116 | |
117 | // Statements with Node type constant, break, continue, pass, breakpoint are skipped because they can't contain translatable strings. |
118 | switch (statement->type) { |
119 | case GDScriptParser::Node::VARIABLE: |
120 | _assess_expression(static_cast<const GDScriptParser::VariableNode *>(statement)->initializer); |
121 | break; |
122 | case GDScriptParser::Node::IF: { |
123 | const GDScriptParser::IfNode *if_node = static_cast<const GDScriptParser::IfNode *>(statement); |
124 | _assess_expression(if_node->condition); |
125 | //FIXME : if the elif logic is changed in GDScriptParser, then this probably will have to change as well. See GDScriptParser::TreePrinter::print_if(). |
126 | _traverse_block(if_node->true_block); |
127 | _traverse_block(if_node->false_block); |
128 | break; |
129 | } |
130 | case GDScriptParser::Node::FOR: { |
131 | const GDScriptParser::ForNode *for_node = static_cast<const GDScriptParser::ForNode *>(statement); |
132 | _assess_expression(for_node->list); |
133 | _traverse_block(for_node->loop); |
134 | break; |
135 | } |
136 | case GDScriptParser::Node::WHILE: { |
137 | const GDScriptParser::WhileNode *while_node = static_cast<const GDScriptParser::WhileNode *>(statement); |
138 | _assess_expression(while_node->condition); |
139 | _traverse_block(while_node->loop); |
140 | break; |
141 | } |
142 | case GDScriptParser::Node::MATCH: { |
143 | const GDScriptParser::MatchNode *match_node = static_cast<const GDScriptParser::MatchNode *>(statement); |
144 | _assess_expression(match_node->test); |
145 | for (int j = 0; j < match_node->branches.size(); j++) { |
146 | _traverse_block(match_node->branches[j]->block); |
147 | } |
148 | break; |
149 | } |
150 | case GDScriptParser::Node::RETURN: |
151 | _assess_expression(static_cast<const GDScriptParser::ReturnNode *>(statement)->return_value); |
152 | break; |
153 | case GDScriptParser::Node::ASSERT: |
154 | _assess_expression((static_cast<const GDScriptParser::AssertNode *>(statement))->condition); |
155 | break; |
156 | case GDScriptParser::Node::ASSIGNMENT: |
157 | _assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(statement)); |
158 | break; |
159 | default: |
160 | if (statement->is_expression()) { |
161 | _assess_expression(static_cast<const GDScriptParser::ExpressionNode *>(statement)); |
162 | } |
163 | break; |
164 | } |
165 | } |
166 | } |
167 | |
168 | void GDScriptEditorTranslationParserPlugin::_assess_expression(const GDScriptParser::ExpressionNode *p_expression) { |
169 | // Explore all ExpressionNodes to find CallNodes which contain translation strings, such as tr(), set_text() etc. |
170 | // tr() can be embedded quite deep within multiple ExpressionNodes so need to dig down to search through all ExpressionNodes. |
171 | if (!p_expression) { |
172 | return; |
173 | } |
174 | |
175 | // ExpressionNode of type await, cast, get_node, identifier, literal, preload, self, subscript, unary are ignored as they can't be CallNode |
176 | // containing translation strings. |
177 | switch (p_expression->type) { |
178 | case GDScriptParser::Node::ARRAY: { |
179 | const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression); |
180 | for (int i = 0; i < array_node->elements.size(); i++) { |
181 | _assess_expression(array_node->elements[i]); |
182 | } |
183 | break; |
184 | } |
185 | case GDScriptParser::Node::ASSIGNMENT: |
186 | _assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(p_expression)); |
187 | break; |
188 | case GDScriptParser::Node::BINARY_OPERATOR: { |
189 | const GDScriptParser::BinaryOpNode *binary_op_node = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression); |
190 | _assess_expression(binary_op_node->left_operand); |
191 | _assess_expression(binary_op_node->right_operand); |
192 | break; |
193 | } |
194 | case GDScriptParser::Node::CALL: { |
195 | const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(p_expression); |
196 | _extract_from_call(call_node); |
197 | for (int i = 0; i < call_node->arguments.size(); i++) { |
198 | _assess_expression(call_node->arguments[i]); |
199 | } |
200 | } break; |
201 | case GDScriptParser::Node::DICTIONARY: { |
202 | const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expression); |
203 | for (int i = 0; i < dict_node->elements.size(); i++) { |
204 | _assess_expression(dict_node->elements[i].key); |
205 | _assess_expression(dict_node->elements[i].value); |
206 | } |
207 | break; |
208 | } |
209 | case GDScriptParser::Node::TERNARY_OPERATOR: { |
210 | const GDScriptParser::TernaryOpNode *ternary_op_node = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression); |
211 | _assess_expression(ternary_op_node->condition); |
212 | _assess_expression(ternary_op_node->true_expr); |
213 | _assess_expression(ternary_op_node->false_expr); |
214 | break; |
215 | } |
216 | default: |
217 | break; |
218 | } |
219 | } |
220 | |
221 | void GDScriptEditorTranslationParserPlugin::_assess_assignment(const GDScriptParser::AssignmentNode *p_assignment) { |
222 | // Extract the translatable strings coming from assignments. For example, get_node("Label").text = "____" |
223 | |
224 | StringName assignee_name; |
225 | if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) { |
226 | assignee_name = static_cast<const GDScriptParser::IdentifierNode *>(p_assignment->assignee)->name; |
227 | } else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) { |
228 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_assignment->assignee); |
229 | if (subscript->is_attribute && subscript->attribute) { |
230 | assignee_name = subscript->attribute->name; |
231 | } else if (subscript->index && _is_constant_string(subscript->index)) { |
232 | assignee_name = subscript->index->reduced_value; |
233 | } |
234 | } |
235 | |
236 | if (assignee_name != StringName() && assignment_patterns.has(assignee_name) && _is_constant_string(p_assignment->assigned_value)) { |
237 | // 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. |
238 | ids->push_back(p_assignment->assigned_value->reduced_value); |
239 | } else if (assignee_name == fd_filters && p_assignment->assigned_value->type == GDScriptParser::Node::CALL) { |
240 | // FileDialog.filters accepts assignment in the form of PackedStringArray. For example, |
241 | // get_node("FileDialog").filters = PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"]). |
242 | |
243 | const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(p_assignment->assigned_value); |
244 | if (!call_node->arguments.is_empty() && call_node->arguments[0]->type == GDScriptParser::Node::ARRAY) { |
245 | const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(call_node->arguments[0]); |
246 | |
247 | // Extract the name in "extension ; name" of PackedStringArray. |
248 | for (int i = 0; i < array_node->elements.size(); i++) { |
249 | _extract_fd_constant_strings(array_node->elements[i]); |
250 | } |
251 | } |
252 | } else { |
253 | // If the assignee is not in extract patterns or the assigned_value is not a constant string, try to see if the assigned_value contains tr(). |
254 | _assess_expression(p_assignment->assigned_value); |
255 | } |
256 | } |
257 | |
258 | void GDScriptEditorTranslationParserPlugin::(const GDScriptParser::CallNode *p_call) { |
259 | // Extract the translatable strings coming from function calls. For example: |
260 | // tr("___"), get_node("Label").set_text("____"), get_node("LineEdit").set_placeholder("____"). |
261 | |
262 | StringName function_name = p_call->function_name; |
263 | |
264 | // Variables for extracting tr() and tr_n(). |
265 | Vector<String> id_ctx_plural; |
266 | id_ctx_plural.resize(3); |
267 | bool = true; |
268 | |
269 | if (function_name == tr_func) { |
270 | // Extract from tr(id, ctx). |
271 | for (int i = 0; i < p_call->arguments.size(); i++) { |
272 | if (_is_constant_string(p_call->arguments[i])) { |
273 | id_ctx_plural.write[i] = p_call->arguments[i]->reduced_value; |
274 | } else { |
275 | // Avoid adding something like tr("Flying dragon", var_context_level_1). We want to extract both id and context together. |
276 | extract_id_ctx_plural = false; |
277 | } |
278 | } |
279 | if (extract_id_ctx_plural) { |
280 | ids_ctx_plural->push_back(id_ctx_plural); |
281 | } |
282 | } else if (function_name == trn_func) { |
283 | // Extract from tr_n(id, plural, n, ctx). |
284 | Vector<int> indices; |
285 | indices.push_back(0); |
286 | indices.push_back(3); |
287 | indices.push_back(1); |
288 | for (int i = 0; i < indices.size(); i++) { |
289 | if (indices[i] >= p_call->arguments.size()) { |
290 | continue; |
291 | } |
292 | |
293 | if (_is_constant_string(p_call->arguments[indices[i]])) { |
294 | id_ctx_plural.write[i] = p_call->arguments[indices[i]]->reduced_value; |
295 | } else { |
296 | extract_id_ctx_plural = false; |
297 | } |
298 | } |
299 | if (extract_id_ctx_plural) { |
300 | ids_ctx_plural->push_back(id_ctx_plural); |
301 | } |
302 | } else if (first_arg_patterns.has(function_name)) { |
303 | if (_is_constant_string(p_call->arguments[0])) { |
304 | ids->push_back(p_call->arguments[0]->reduced_value); |
305 | } |
306 | } else if (second_arg_patterns.has(function_name)) { |
307 | if (_is_constant_string(p_call->arguments[1])) { |
308 | ids->push_back(p_call->arguments[1]->reduced_value); |
309 | } |
310 | } else if (function_name == fd_add_filter) { |
311 | // Extract the 'JPE Images' in this example - get_node("FileDialog").add_filter("*.jpg; JPE Images"). |
312 | _extract_fd_constant_strings(p_call->arguments[0]); |
313 | } else if (function_name == fd_set_filter && p_call->arguments[0]->type == GDScriptParser::Node::CALL) { |
314 | // FileDialog.set_filters() accepts assignment in the form of PackedStringArray. For example, |
315 | // get_node("FileDialog").set_filters( PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"])). |
316 | |
317 | const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(p_call->arguments[0]); |
318 | if (call_node->arguments[0]->type == GDScriptParser::Node::ARRAY) { |
319 | const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(call_node->arguments[0]); |
320 | for (int i = 0; i < array_node->elements.size(); i++) { |
321 | _extract_fd_constant_strings(array_node->elements[i]); |
322 | } |
323 | } |
324 | } |
325 | |
326 | if (p_call->callee && p_call->callee->type == GDScriptParser::Node::SUBSCRIPT) { |
327 | const GDScriptParser::SubscriptNode *subscript_node = static_cast<const GDScriptParser::SubscriptNode *>(p_call->callee); |
328 | if (subscript_node->base && subscript_node->base->type == GDScriptParser::Node::CALL) { |
329 | const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(subscript_node->base); |
330 | _extract_from_call(call_node); |
331 | } |
332 | } |
333 | } |
334 | |
335 | void GDScriptEditorTranslationParserPlugin::(const GDScriptParser::ExpressionNode *p_expression) { |
336 | // Extract the name in "extension ; name". |
337 | |
338 | if (_is_constant_string(p_expression)) { |
339 | String arg_val = p_expression->reduced_value; |
340 | PackedStringArray arr = arg_val.split(";" , true); |
341 | if (arr.size() != 2) { |
342 | ERR_PRINT("Argument for setting FileDialog has bad format." ); |
343 | return; |
344 | } |
345 | ids->push_back(arr[1].strip_edges()); |
346 | } |
347 | } |
348 | |
349 | GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() { |
350 | assignment_patterns.insert("text" ); |
351 | assignment_patterns.insert("placeholder_text" ); |
352 | assignment_patterns.insert("tooltip_text" ); |
353 | |
354 | first_arg_patterns.insert("set_text" ); |
355 | first_arg_patterns.insert("set_tooltip_text" ); |
356 | first_arg_patterns.insert("set_placeholder" ); |
357 | first_arg_patterns.insert("add_tab" ); |
358 | first_arg_patterns.insert("add_check_item" ); |
359 | first_arg_patterns.insert("add_item" ); |
360 | first_arg_patterns.insert("add_multistate_item" ); |
361 | first_arg_patterns.insert("add_radio_check_item" ); |
362 | first_arg_patterns.insert("add_separator" ); |
363 | first_arg_patterns.insert("add_submenu_item" ); |
364 | |
365 | second_arg_patterns.insert("set_tab_title" ); |
366 | second_arg_patterns.insert("add_icon_check_item" ); |
367 | second_arg_patterns.insert("add_icon_item" ); |
368 | second_arg_patterns.insert("add_icon_radio_check_item" ); |
369 | second_arg_patterns.insert("set_item_text" ); |
370 | } |
371 | |