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