| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  packed_scene_translation_parser_plugin.cpp                            */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "packed_scene_translation_parser_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "core/io/resource_loader.h"
 | 
					
						
							| 
									
										
										
										
											2023-09-06 21:02:52 +02:00
										 |  |  | #include "core/object/script_language.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | #include "scene/resources/packed_scene.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const { | 
					
						
							|  |  |  | 	ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 00:07:35 +02:00
										 |  |  | Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) { | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 	// Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property.
 | 
					
						
							|  |  |  | 	// These properties are translated with the tr() function in the C++ code when being set or updated.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | 	Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 	if (err) { | 
					
						
							|  |  |  | 		ERR_PRINT("Failed to load " + p_path); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-22 00:22:33 +08:00
										 |  |  | 	Ref<PackedScene> packed_scene = loaded_res; | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V_MSG(packed_scene.is_null(), ERR_FILE_UNRECOGNIZED, vformat("'%s' is not a valid PackedScene resource.", p_path)); | 
					
						
							|  |  |  | 	Ref<SceneState> state = packed_scene->get_state(); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector<String> parsed_strings; | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 	Vector<Pair<NodePath, bool>> atr_owners; | 
					
						
							|  |  |  | 	Vector<String> tabcontainer_paths; | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 	for (int i = 0; i < state->get_node_count(); i++) { | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 		String node_type = state->get_node_type(i); | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 		String parent_path = state->get_node_path(i, true); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 17:40:35 +02:00
										 |  |  | 		// Handle instanced scenes.
 | 
					
						
							|  |  |  | 		if (node_type.is_empty()) { | 
					
						
							|  |  |  | 			Ref<PackedScene> instance = state->get_node_instance(i); | 
					
						
							|  |  |  | 			if (instance.is_valid()) { | 
					
						
							|  |  |  | 				Ref<SceneState> _state = instance->get_state(); | 
					
						
							|  |  |  | 				node_type = _state->get_node_type(0); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 		// Find the `auto_translate_mode` property.
 | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 		bool auto_translating = true; | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 		bool auto_translate_mode_found = false; | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 		for (int j = 0; j < state->get_node_property_count(i); j++) { | 
					
						
							| 
									
										
										
										
											2024-07-22 10:00:09 -03:00
										 |  |  | 			String property = state->get_node_property_name(i, j); | 
					
						
							|  |  |  | 			// If an old scene wasn't saved in the new version, the `auto_translate` property won't be converted into `auto_translate_mode`,
 | 
					
						
							|  |  |  | 			// so the deprecated property still needs to be checked as well.
 | 
					
						
							|  |  |  | 			// TODO: Remove the check for "auto_translate" once the property if fully removed.
 | 
					
						
							|  |  |  | 			if (property != "auto_translate_mode" && property != "auto_translate") { | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			auto_translate_mode_found = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			int idx_last = atr_owners.size() - 1; | 
					
						
							|  |  |  | 			if (idx_last > 0 && !parent_path.begins_with(atr_owners[idx_last].first)) { | 
					
						
							| 
									
										
										
										
											2024-08-12 18:05:58 -03:00
										 |  |  | 				// Exit from the current owner nesting into the previous one.
 | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 				atr_owners.remove_at(idx_last); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 10:00:09 -03:00
										 |  |  | 			if (property == "auto_translate_mode") { | 
					
						
							|  |  |  | 				int auto_translate_mode = (int)state->get_node_property_value(i, j); | 
					
						
							|  |  |  | 				if (auto_translate_mode == Node::AUTO_TRANSLATE_MODE_DISABLED) { | 
					
						
							|  |  |  | 					auto_translating = false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// TODO: Remove this once `auto_translate` is fully removed.
 | 
					
						
							|  |  |  | 				auto_translating = (bool)state->get_node_property_value(i, j); | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			atr_owners.push_back(Pair(state->get_node_path(i), auto_translating)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If `auto_translate_mode` wasn't found, that means it is set to its default value (`AUTO_TRANSLATE_MODE_INHERIT`).
 | 
					
						
							|  |  |  | 		if (!auto_translate_mode_found) { | 
					
						
							|  |  |  | 			int idx_last = atr_owners.size() - 1; | 
					
						
							| 
									
										
										
										
											2024-08-12 18:05:58 -03:00
										 |  |  | 			if (idx_last > 0 && parent_path.begins_with(atr_owners[idx_last].first)) { | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 				auto_translating = atr_owners[idx_last].second; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				atr_owners.push_back(Pair(state->get_node_path(i), true)); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-06 10:52:44 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parse the names of children of `TabContainer`s, as they are used for tab titles.
 | 
					
						
							|  |  |  | 		if (!tabcontainer_paths.is_empty()) { | 
					
						
							|  |  |  | 			if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) { | 
					
						
							|  |  |  | 				// Switch to the previous `TabContainer` this was nested in, if that was the case.
 | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 				tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1); | 
					
						
							| 
									
										
										
										
											2024-01-06 10:52:44 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 18:29:45 -03:00
										 |  |  | 			if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") && | 
					
						
							|  |  |  | 					parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) { | 
					
						
							| 
									
										
										
										
											2024-01-06 10:52:44 -03:00
										 |  |  | 				parsed_strings.push_back(state->get_node_name(i)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 		if (!auto_translating) { | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 10:52:44 -03:00
										 |  |  | 		if (node_type == "TabContainer") { | 
					
						
							|  |  |  | 			tabcontainer_paths.push_back(state->get_node_path(i)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 		for (int j = 0; j < state->get_node_property_count(i); j++) { | 
					
						
							|  |  |  | 			String property_name = state->get_node_property_name(i, j); | 
					
						
							| 
									
										
										
										
											2024-03-08 17:40:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (!match_property(property_name, node_type)) { | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 			Variant property_value = state->get_node_property_value(i, j); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 			if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) { | 
					
						
							|  |  |  | 				// Parse built-in script.
 | 
					
						
							|  |  |  | 				Ref<Script> s = Object::cast_to<Script>(property_value); | 
					
						
							| 
									
										
										
										
											2023-12-23 21:18:18 -03:00
										 |  |  | 				if (!s->is_built_in()) { | 
					
						
							|  |  |  | 					continue; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 				String extension = s->get_language()->get_extension(); | 
					
						
							|  |  |  | 				if (EditorTranslationParser::get_singleton()->can_parse(extension)) { | 
					
						
							|  |  |  | 					Vector<String> temp; | 
					
						
							| 
									
										
										
										
											2020-07-23 00:07:35 +02:00
										 |  |  | 					Vector<Vector<String>> ids_context_plural; | 
					
						
							|  |  |  | 					EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp, &ids_context_plural); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 					parsed_strings.append_array(temp); | 
					
						
							| 
									
										
										
										
											2020-07-23 00:07:35 +02:00
										 |  |  | 					r_ids_ctx_plural->append_array(ids_context_plural); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 			} else if (node_type == "FileDialog" && property_name == "filters") { | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 				// Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".
 | 
					
						
							|  |  |  | 				Vector<String> str_values = property_value; | 
					
						
							|  |  |  | 				for (int k = 0; k < str_values.size(); k++) { | 
					
						
							|  |  |  | 					String desc = str_values[k].get_slice(";", 1).strip_edges(); | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 					if (!desc.is_empty()) { | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 						parsed_strings.push_back(desc); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else if (property_value.get_type() == Variant::STRING) { | 
					
						
							|  |  |  | 				String str_value = String(property_value); | 
					
						
							|  |  |  | 				// Prevent reading text containing only spaces.
 | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 				if (!str_value.strip_edges().is_empty()) { | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 					parsed_strings.push_back(str_value); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 00:07:35 +02:00
										 |  |  | 	r_ids->append_array(parsed_strings); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 17:40:35 +02:00
										 |  |  | bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) { | 
					
						
							|  |  |  | 	for (const KeyValue<String, Vector<String>> &exception : exception_list) { | 
					
						
							|  |  |  | 		const String &exception_node_type = exception.key; | 
					
						
							|  |  |  | 		if (ClassDB::is_parent_class(p_node_type, exception_node_type)) { | 
					
						
							|  |  |  | 			const Vector<String> &exception_properties = exception.value; | 
					
						
							|  |  |  | 			for (const String &exception_property : exception_properties) { | 
					
						
							|  |  |  | 				if (p_property_name.match(exception_property)) { | 
					
						
							|  |  |  | 					return false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (const String &lookup_property : lookup_properties) { | 
					
						
							|  |  |  | 		if (p_property_name.match(lookup_property)) { | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() { | 
					
						
							|  |  |  | 	// Scene Node's properties containing strings that will be fetched for translation.
 | 
					
						
							|  |  |  | 	lookup_properties.insert("text"); | 
					
						
							| 
									
										
										
										
											2024-03-08 17:40:35 +02:00
										 |  |  | 	lookup_properties.insert("*_text"); | 
					
						
							|  |  |  | 	lookup_properties.insert("popup/*/text"); | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 	lookup_properties.insert("title"); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 	lookup_properties.insert("filters"); | 
					
						
							|  |  |  | 	lookup_properties.insert("script"); | 
					
						
							| 
									
										
										
										
											2024-06-23 20:59:17 +01:00
										 |  |  | 	lookup_properties.insert("item_*/text"); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 02:53:36 -03:00
										 |  |  | 	// Exception list (to prevent false positives).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 14:12:19 +01:00
										 |  |  | 	exception_list.insert("LineEdit", { "text" }); | 
					
						
							|  |  |  | 	exception_list.insert("TextEdit", { "text" }); | 
					
						
							|  |  |  | 	exception_list.insert("CodeEdit", { "text" }); | 
					
						
							| 
									
										
										
										
											2020-06-23 13:48:59 +02:00
										 |  |  | } |