Debian Codegraph @

editor/translations/editor_translation.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_translation.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.h"
32
33 #include "core/io/compression.h"
34 #include "core/io/file_access_memory.h"
35 #include "core/io/translation_loader_po.h"
36 #include "core/string/translation_server.h"
37 #include "editor/translations/doc_translations.gen.h"
38 #include "editor/translations/editor_translations.gen.h"
39 #include "editor/translations/extractable_translations.gen.h"
40 #include "editor/translations/property_translations.gen.h"
41
42 Vector<String> get_editor_locales() {
43 Vector<String> locales;
44
45 for (const EditorTranslationList *etl = _editor_translations; etl->data; etl++) {
46 const String &locale = etl->lang;
47 locales.push_back(locale);
48 }
49
50 return locales;
51 }
52
53 static void _load(const Ref<TranslationDomain> p_domain, const String &p_locale, const EditorTranslationList *p_etl) {
54 for (const EditorTranslationList *etl = p_etl; etl->data; etl++) {
55 if (etl->lang == p_locale) {
56 LocalVector<uint8_t> data;
57 data.resize_uninitialized(etl->uncomp_size);
58 const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
59 ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
60
61 Ref<FileAccessMemory> fa;
62 fa.instantiate();
63 fa->open_custom(data.ptr(), data.size());
64
65 Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
66 if (tr.is_valid()) {
67 tr->set_locale(etl->lang);
68 p_domain->add_translation(tr);
69 break;
70 }
71 }
72 }
73 }
74
75 void load_editor_translations(const String &p_locale) {
76 Ref<TranslationDomain> domain;
77
78 domain = TranslationServer::get_singleton()->get_editor_domain();
79 domain->clear();
80 _load(domain, p_locale, _editor_translations);
81 _load(domain, p_locale, _extractable_translations);
82
83 domain = TranslationServer::get_singleton()->get_property_domain();
84 domain->clear();
85 _load(domain, p_locale, _property_translations);
86 }
87
88 void load_doc_translations(const String &p_locale) {
89 const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_doc_domain();
90 domain->clear();
91 _load(domain, p_locale, _doc_translations);
92 }
93
94 Vector<Vector<String>> get_extractable_message_list() {
95 Vector<Vector<String>> list;
96
97 for (const EditorTranslationList *etl = _extractable_translations; etl->data; etl++) {
98 if (strcmp(etl->lang, "source")) {
99 continue;
100 }
101
102 LocalVector<uint8_t> data;
103 data.resize_uninitialized(etl->uncomp_size);
104 const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
105 ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");
106
107 Ref<FileAccessMemory> fa;
108 fa.instantiate();
109 fa->open_custom(data.ptr(), data.size());
110
111 // Taken from TranslationLoaderPO, modified to work specifically with POTs.
112 {
113 const String path = fa->get_path();
114
115 fa->seek(0);
116
117 enum Status {
118 STATUS_NONE,
119 STATUS_READING_ID,
120 STATUS_READING_STRING,
121 STATUS_READING_CONTEXT,
122 STATUS_READING_PLURAL,
123 };
124
125 Status status = STATUS_NONE;
126
127 String msg_id;
128 String msg_id_plural;
129 String msg_context;
130
131 int line = 1;
132 bool entered_context = false;
133 bool is_eof = false;
134
135 while (!is_eof) {
136 String l = fa->get_line().strip_edges();
137 is_eof = fa->eof_reached();
138
139 // If we reached last line and it's not a content line, break, otherwise let processing that last loop.
140 if (is_eof && l.is_empty()) {
141 if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || status == STATUS_READING_PLURAL) {
142 ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected EOF while reading POT file at: " + path + ":" + itos(line));
143 } else {
144 break;
145 }
146 }
147
148 if (l.begins_with("msgctxt")) {
149 ERR_FAIL_COND_V_MSG(status != STATUS_READING_STRING && status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
150 "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
151
152 // In POT files, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
153 // and set "entered_context" to true to prevent adding twice.
154 if (!msg_id.is_empty()) {
155 Vector<String> msgs;
156 msgs.push_back(msg_id);
157 msgs.push_back(msg_context);
158 msgs.push_back(msg_id_plural);
159 list.push_back(msgs);
160 }
161 msg_context = "";
162 l = l.substr(7).strip_edges();
163 status = STATUS_READING_CONTEXT;
164 entered_context = true;
165 }
166
167 if (l.begins_with("msgid_plural")) {
168 if (status != STATUS_READING_ID) {
169 ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
170 }
171 l = l.substr(12).strip_edges();
172 status = STATUS_READING_PLURAL;
173 } else if (l.begins_with("msgid")) {
174 ERR_FAIL_COND_V_MSG(status == STATUS_READING_ID, Vector<Vector<String>>(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
175
176 if (!msg_id.is_empty() && !entered_context) {
177 Vector<String> msgs;
178 msgs.push_back(msg_id);
179 msgs.push_back(msg_context);
180 msgs.push_back(msg_id_plural);
181 list.push_back(msgs);
182 }
183
184 l = l.substr(5).strip_edges();
185 status = STATUS_READING_ID;
186 // If we did not encounter msgctxt, we reset context to empty to reset it.
187 if (!entered_context) {
188 msg_context = "";
189 }
190 msg_id = "";
191 msg_id_plural = "";
192 entered_context = false;
193 }
194
195 if (l.begins_with("msgstr[")) {
196 ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
197 "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
198 l = l.substr(9).strip_edges();
199 } else if (l.begins_with("msgstr")) {
200 ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Vector<Vector<String>>(),
201 "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
202 l = l.substr(6).strip_edges();
203 status = STATUS_READING_STRING;
204 }
205
206 if (l.is_empty() || l.begins_with("#")) {
207 line++;
208 continue; // Nothing to read or comment.
209 }
210
211 ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Vector<Vector<String>>(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
212
213 l = l.substr(1);
214 // Find final quote, ignoring escaped ones (\").
215 // The escape_next logic is necessary to properly parse things like \\"
216 // where the backslash is the one being escaped, not the quote.
217 int end_pos = -1;
218 bool escape_next = false;
219 for (int i = 0; i < l.length(); i++) {
220 if (l[i] == '\\' && !escape_next) {
221 escape_next = true;
222 continue;
223 }
224
225 if (l[i] == '"' && !escape_next) {
226 end_pos = i;
227 break;
228 }
229
230 escape_next = false;
231 }
232
233 ERR_FAIL_COND_V_MSG(end_pos == -1, Vector<Vector<String>>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
234
235 l = l.substr(0, end_pos);
236 l = l.c_unescape();
237
238 if (status == STATUS_READING_ID) {
239 msg_id += l;
240 } else if (status == STATUS_READING_CONTEXT) {
241 msg_context += l;
242 } else if (status == STATUS_READING_PLURAL) {
243 msg_id_plural += l;
244 }
245
246 line++;
247 }
248 }
249 }
250
251 return list;
252 }