editor_translation_parser.cpp 7.1 KB

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