Debian Codegraph @

editor/translations/editor_translation_parser.cpp

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