packed_scene_translation_parser_plugin.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/gui/option_button.h"
  33. #include "scene/resources/packed_scene.h"
  34. void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  35. ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions);
  36. }
  37. Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
  38. // Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property.
  39. // These properties are translated with the tr() function in the C++ code when being set or updated.
  40. Error err;
  41. Ref<Resource> loaded_res = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  42. if (err) {
  43. ERR_PRINT("Failed to load " + p_path);
  44. return err;
  45. }
  46. Ref<SceneState> state = Ref<PackedScene>(loaded_res)->get_state();
  47. Vector<String> parsed_strings;
  48. for (int i = 0; i < state->get_node_count(); i++) {
  49. String node_type = state->get_node_type(i);
  50. if (!ClassDB::is_parent_class(node_type, "Control") && !ClassDB::is_parent_class(node_type, "Window")) {
  51. continue;
  52. }
  53. // Find the `auto_translate` property, and abort the string parsing of the node if disabled.
  54. bool auto_translating = true;
  55. for (int j = 0; j < state->get_node_property_count(i); j++) {
  56. if (state->get_node_property_name(i, j) == "auto_translate" && (bool)state->get_node_property_value(i, j) == false) {
  57. auto_translating = false;
  58. break;
  59. }
  60. }
  61. if (!auto_translating) {
  62. continue;
  63. }
  64. for (int j = 0; j < state->get_node_property_count(i); j++) {
  65. String property_name = state->get_node_property_name(i, j);
  66. if (!lookup_properties.has(property_name) || (exception_list.has(node_type) && exception_list[node_type].has(property_name))) {
  67. continue;
  68. }
  69. Variant property_value = state->get_node_property_value(i, j);
  70. if (property_name == "script" && property_value.get_type() == Variant::OBJECT && !property_value.is_null()) {
  71. // Parse built-in script.
  72. Ref<Script> s = Object::cast_to<Script>(property_value);
  73. String extension = s->get_language()->get_extension();
  74. if (EditorTranslationParser::get_singleton()->can_parse(extension)) {
  75. Vector<String> temp;
  76. Vector<Vector<String>> ids_context_plural;
  77. EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp, &ids_context_plural);
  78. parsed_strings.append_array(temp);
  79. r_ids_ctx_plural->append_array(ids_context_plural);
  80. }
  81. } else if ((node_type == "MenuButton" || node_type == "OptionButton") && property_name == "items") {
  82. Vector<String> str_values = property_value;
  83. int incr_value = node_type == "MenuButton" ? PopupMenu::ITEM_PROPERTY_SIZE : OptionButton::ITEM_PROPERTY_SIZE;
  84. for (int k = 0; k < str_values.size(); k += incr_value) {
  85. String desc = str_values[k].get_slice(";", 1).strip_edges();
  86. if (!desc.is_empty()) {
  87. parsed_strings.push_back(desc);
  88. }
  89. }
  90. } else if (node_type == "FileDialog" && property_name == "filters") {
  91. // Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".
  92. Vector<String> str_values = property_value;
  93. for (int k = 0; k < str_values.size(); k++) {
  94. String desc = str_values[k].get_slice(";", 1).strip_edges();
  95. if (!desc.is_empty()) {
  96. parsed_strings.push_back(desc);
  97. }
  98. }
  99. } else if (property_value.get_type() == Variant::STRING) {
  100. String str_value = String(property_value);
  101. // Prevent reading text containing only spaces.
  102. if (!str_value.strip_edges().is_empty()) {
  103. parsed_strings.push_back(str_value);
  104. }
  105. }
  106. }
  107. }
  108. r_ids->append_array(parsed_strings);
  109. return OK;
  110. }
  111. PackedSceneEditorTranslationParserPlugin::PackedSceneEditorTranslationParserPlugin() {
  112. // Scene Node's properties containing strings that will be fetched for translation.
  113. lookup_properties.insert("text");
  114. lookup_properties.insert("tooltip_text");
  115. lookup_properties.insert("placeholder_text");
  116. lookup_properties.insert("items");
  117. lookup_properties.insert("title");
  118. lookup_properties.insert("dialog_text");
  119. lookup_properties.insert("filters");
  120. lookup_properties.insert("script");
  121. // Exception list (to prevent false positives).
  122. exception_list.insert("LineEdit", { "text" });
  123. exception_list.insert("TextEdit", { "text" });
  124. exception_list.insert("CodeEdit", { "text" });
  125. }