editor_translation_parser.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**************************************************************************/
  2. /* editor_translation_parser.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 "editor_translation_parser.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/io/file_access.h"
  33. #include "core/object/script_language.h"
  34. #include "core/templates/hash_set.h"
  35. EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
  36. Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
  37. TypedArray<String> ids;
  38. TypedArray<Array> ids_ctx_plural;
  39. if (GDVIRTUAL_CALL(_parse_file, p_path, ids, ids_ctx_plural)) {
  40. // Add user's extracted translatable messages.
  41. for (int i = 0; i < ids.size(); i++) {
  42. r_ids->append(ids[i]);
  43. }
  44. // Add user's collected translatable messages with context or plurals.
  45. for (int i = 0; i < ids_ctx_plural.size(); i++) {
  46. Array arr = ids_ctx_plural[i];
  47. ERR_FAIL_COND_V_MSG(arr.size() != 3, ERR_INVALID_DATA, "Array entries written into `msgids_context_plural` in `parse_file()` method should have the form [\"message\", \"context\", \"plural message\"]");
  48. Vector<String> id_ctx_plural;
  49. id_ctx_plural.push_back(arr[0]);
  50. id_ctx_plural.push_back(arr[1]);
  51. id_ctx_plural.push_back(arr[2]);
  52. r_ids_ctx_plural->append(id_ctx_plural);
  53. }
  54. return OK;
  55. } else {
  56. ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined.");
  57. return ERR_UNAVAILABLE;
  58. }
  59. }
  60. void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
  61. Vector<String> extensions;
  62. if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
  63. for (int i = 0; i < extensions.size(); i++) {
  64. r_extensions->push_back(extensions[i]);
  65. }
  66. } else {
  67. ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
  68. }
  69. }
  70. void EditorTranslationParserPlugin::_bind_methods() {
  71. GDVIRTUAL_BIND(_parse_file, "path", "msgids", "msgids_context_plural");
  72. GDVIRTUAL_BIND(_get_recognized_extensions);
  73. }
  74. /////////////////////////
  75. void EditorTranslationParser::get_recognized_extensions(List<String> *r_extensions) const {
  76. HashSet<String> extensions;
  77. List<String> temp;
  78. for (int i = 0; i < standard_parsers.size(); i++) {
  79. standard_parsers[i]->get_recognized_extensions(&temp);
  80. }
  81. for (int i = 0; i < custom_parsers.size(); i++) {
  82. custom_parsers[i]->get_recognized_extensions(&temp);
  83. }
  84. // Remove duplicates.
  85. for (const String &E : temp) {
  86. extensions.insert(E);
  87. }
  88. for (const String &E : extensions) {
  89. r_extensions->push_back(E);
  90. }
  91. }
  92. bool EditorTranslationParser::can_parse(const String &p_extension) const {
  93. List<String> extensions;
  94. get_recognized_extensions(&extensions);
  95. for (const String &extension : extensions) {
  96. if (p_extension == extension) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const String &p_extension) const {
  103. // Consider user-defined parsers first.
  104. for (int i = 0; i < custom_parsers.size(); i++) {
  105. List<String> temp;
  106. custom_parsers[i]->get_recognized_extensions(&temp);
  107. for (const String &E : temp) {
  108. if (E == p_extension) {
  109. return custom_parsers[i];
  110. }
  111. }
  112. }
  113. for (int i = 0; i < standard_parsers.size(); i++) {
  114. List<String> temp;
  115. standard_parsers[i]->get_recognized_extensions(&temp);
  116. for (const String &E : temp) {
  117. if (E == p_extension) {
  118. return standard_parsers[i];
  119. }
  120. }
  121. }
  122. WARN_PRINT("No translation parser available for \"" + p_extension + "\" extension.");
  123. return nullptr;
  124. }
  125. void EditorTranslationParser::add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  126. if (p_type == ParserType::STANDARD) {
  127. standard_parsers.push_back(p_parser);
  128. } else if (p_type == ParserType::CUSTOM) {
  129. custom_parsers.push_back(p_parser);
  130. }
  131. }
  132. void EditorTranslationParser::remove_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
  133. if (p_type == ParserType::STANDARD) {
  134. standard_parsers.erase(p_parser);
  135. } else if (p_type == ParserType::CUSTOM) {
  136. custom_parsers.erase(p_parser);
  137. }
  138. }
  139. void EditorTranslationParser::clean_parsers() {
  140. standard_parsers.clear();
  141. custom_parsers.clear();
  142. }
  143. EditorTranslationParser *EditorTranslationParser::get_singleton() {
  144. if (!singleton) {
  145. singleton = memnew(EditorTranslationParser);
  146. }
  147. return singleton;
  148. }
  149. EditorTranslationParser::EditorTranslationParser() {
  150. }
  151. EditorTranslationParser::~EditorTranslationParser() {
  152. memdelete(singleton);
  153. singleton = nullptr;
  154. }