packed_scene_translation_parser_plugin.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "packed_scene_translation_parser_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/object/script_language.h"
  33. #include "scene/gui/option_button.h"
  34. #include "scene/resources/packed_scene.h"
  35. void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  36. ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions);
  37. }
  38. Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
  39. // Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property.
  40. // These properties are translated with the tr() function in the C++ code when being set or updated.
  41. Error err;
  42. Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  43. if (err) {
  44. ERR_PRINT("Failed to load " + p_path);
  45. return err;
  46. }
  47. Ref<SceneState> state = Ref<PackedScene>(loaded_res)->get_state();
  48. Vector<String> parsed_strings;
  49. Vector<Pair<NodePath, bool>> atr_owners;
  50. Vector<String> tabcontainer_paths;
  51. for (int i = 0; i < state->get_node_count(); i++) {
  52. String node_type = state->get_node_type(i);
  53. String parent_path = state->get_node_path(i, true);
  54. // Handle instanced scenes.
  55. if (node_type.is_empty()) {
  56. Ref<PackedScene> instance = state->get_node_instance(i);
  57. if (instance.is_valid()) {
  58. Ref<SceneState> _state = instance->get_state();
  59. node_type = _state->get_node_type(0);
  60. }
  61. }
  62. // Find the `auto_translate_mode` property.
  63. bool auto_translating = true;
  64. bool auto_translate_mode_found = false;
  65. for (int j = 0; j < state->get_node_property_count(i); j++) {
  66. String property = state->get_node_property_name(i, j);
  67. // If an old scene wasn't saved in the new version, the `auto_translate` property won't be converted into `auto_translate_mode`,
  68. // so the deprecated property still needs to be checked as well.
  69. // TODO: Remove the check for "auto_translate" once the property if fully removed.
  70. if (property != "auto_translate_mode" && property != "auto_translate") {
  71. continue;
  72. }
  73. auto_translate_mode_found = true;
  74. int idx_last = atr_owners.size() - 1;
  75. if (idx_last > 0 && !parent_path.begins_with(atr_owners[idx_last].first)) {
  76. // Exit from the current owner nesting into the previous one.
  77. atr_owners.remove_at(idx_last);
  78. }
  79. if (property == "auto_translate_mode") {
  80. int auto_translate_mode = (int)state->get_node_property_value(i, j);
  81. if (auto_translate_mode == Node::AUTO_TRANSLATE_MODE_DISABLED) {
  82. auto_translating = false;
  83. }
  84. } else {
  85. // TODO: Remove this once `auto_translate` is fully removed.
  86. auto_translating = (bool)state->get_node_property_value(i, j);
  87. }
  88. atr_owners.push_back(Pair(state->get_node_path(i), auto_translating));
  89. break;
  90. }
  91. // If `auto_translate_mode` wasn't found, that means it is set to its default value (`AUTO_TRANSLATE_MODE_INHERIT`).
  92. if (!auto_translate_mode_found) {
  93. int idx_last = atr_owners.size() - 1;
  94. if (idx_last > 0 && parent_path.begins_with(atr_owners[idx_last].first)) {
  95. auto_translating = atr_owners[idx_last].second;
  96. } else {
  97. atr_owners.push_back(Pair(state->get_node_path(i), true));
  98. }
  99. }
  100. // Parse the names of children of `TabContainer`s, as they are used for tab titles.
  101. if (!tabcontainer_paths.is_empty()) {
  102. if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) {
  103. // Switch to the previous `TabContainer` this was nested in, if that was the case.
  104. tabcontainer_paths.remove_at(tabcontainer_paths.size() - 1);
  105. }
  106. if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") &&
  107. parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) {
  108. parsed_strings.push_back(state->get_node_name(i));
  109. }
  110. }
  111. if (!auto_translating) {
  112. continue;
  113. }
  114. if (node_type == "TabContainer") {
  115. tabcontainer_paths.push_back(state->get_node_path(i));
  116. }
  117. for (int j = 0; j < state->get_node_property_count(i); j++) {
  118. String property_name = state->get_node_property_name(i, j);
  119. if (!match_property(property_name, node_type)) {
  120. continue;
  121. }
  122. Variant property_value = state->get_node_property_value(i, j);
  123. if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) {
  124. // Parse built-in script.
  125. Ref<Script> s = Object::cast_to<Script>(property_value);
  126. if (!s->is_built_in()) {
  127. continue;
  128. }
  129. String extension = s->get_language()->get_extension();
  130. if (EditorTranslationParser::get_singleton()->can_parse(extension)) {
  131. Vector<String> temp;
  132. Vector<Vector<String>> ids_context_plural;
  133. EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp, &ids_context_plural);
  134. parsed_strings.append_array(temp);
  135. r_ids_ctx_plural->append_array(ids_context_plural);
  136. }
  137. } else if (node_type == "FileDialog" && property_name == "filters") {
  138. // Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".
  139. Vector<String> str_values = property_value;
  140. for (int k = 0; k < str_values.size(); k++) {
  141. String desc = str_values[k].get_slice(";", 1).strip_edges();
  142. if (!desc.is_empty()) {
  143. parsed_strings.push_back(desc);
  144. }
  145. }
  146. } else if (property_value.get_type() == Variant::STRING) {
  147. String str_value = String(property_value);
  148. // Prevent reading text containing only spaces.
  149. if (!str_value.strip_edges().is_empty()) {
  150. parsed_strings.push_back(str_value);
  151. }
  152. }
  153. }
  154. }
  155. r_ids->append_array(parsed_strings);
  156. return OK;
  157. }
  158. bool PackedSceneEditorTranslationParserPlugin::match_property(const String &p_property_name, const String &p_node_type) {
  159. for (const KeyValue<String, Vector<String>> &exception : exception_list) {
  160. const String &exception_node_type = exception.key;
  161. if (ClassDB::is_parent_class(p_node_type, exception_node_type)) {
  162. const Vector<String> &exception_properties = exception.value;
  163. for (const String &exception_property : exception_properties) {
  164. if (p_property_name.match(exception_property)) {
  165. return false;
  166. }
  167. }
  168. }
  169. }
  170. for (const String &lookup_property : lookup_properties) {
  171. if (p_property_name.match(lookup_property)) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() {
  178. // Scene Node's properties containing strings that will be fetched for translation.
  179. lookup_properties.insert("text");
  180. lookup_properties.insert("*_text");
  181. lookup_properties.insert("popup/*/text");
  182. lookup_properties.insert("title");
  183. lookup_properties.insert("filters");
  184. lookup_properties.insert("script");
  185. lookup_properties.insert("item_*/text");
  186. // Exception list (to prevent false positives).
  187. exception_list.insert("LineEdit", { "text" });
  188. exception_list.insert("TextEdit", { "text" });
  189. exception_list.insert("CodeEdit", { "text" });
  190. }