Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/translations/editor_translation.cpp
9896 views
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
const EditorTranslationList *etl = _editor_translations;
46
while (etl->data) {
47
const String &locale = etl->lang;
48
locales.push_back(locale);
49
50
etl++;
51
}
52
53
return locales;
54
}
55
56
void load_editor_translations(const String &p_locale) {
57
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
58
59
const EditorTranslationList *etl = _editor_translations;
60
while (etl->data) {
61
if (etl->lang == p_locale) {
62
Vector<uint8_t> data;
63
data.resize(etl->uncomp_size);
64
const int64_t ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
65
ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
66
67
Ref<FileAccessMemory> fa;
68
fa.instantiate();
69
fa->open_custom(data.ptr(), data.size());
70
71
Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
72
73
if (tr.is_valid()) {
74
tr->set_locale(etl->lang);
75
domain->add_translation(tr);
76
break;
77
}
78
}
79
80
etl++;
81
}
82
}
83
84
void load_property_translations(const String &p_locale) {
85
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.properties");
86
87
const PropertyTranslationList *etl = _property_translations;
88
while (etl->data) {
89
if (etl->lang == p_locale) {
90
Vector<uint8_t> data;
91
data.resize(etl->uncomp_size);
92
const int64_t ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
93
ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
94
95
Ref<FileAccessMemory> fa;
96
fa.instantiate();
97
fa->open_custom(data.ptr(), data.size());
98
99
Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
100
101
if (tr.is_valid()) {
102
tr->set_locale(etl->lang);
103
domain->add_translation(tr);
104
break;
105
}
106
}
107
108
etl++;
109
}
110
}
111
112
void load_doc_translations(const String &p_locale) {
113
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.documentation");
114
115
const DocTranslationList *dtl = _doc_translations;
116
while (dtl->data) {
117
if (dtl->lang == p_locale) {
118
Vector<uint8_t> data;
119
data.resize(dtl->uncomp_size);
120
const int64_t ret = Compression::decompress(data.ptrw(), dtl->uncomp_size, dtl->data, dtl->comp_size, Compression::MODE_DEFLATE);
121
ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
122
123
Ref<FileAccessMemory> fa;
124
fa.instantiate();
125
fa->open_custom(data.ptr(), data.size());
126
127
Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
128
129
if (tr.is_valid()) {
130
tr->set_locale(dtl->lang);
131
domain->add_translation(tr);
132
break;
133
}
134
}
135
136
dtl++;
137
}
138
}
139
140
void load_extractable_translations(const String &p_locale) {
141
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
142
143
const ExtractableTranslationList *etl = _extractable_translations;
144
while (etl->data) {
145
if (etl->lang == p_locale) {
146
Vector<uint8_t> data;
147
data.resize(etl->uncomp_size);
148
const int64_t ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
149
ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
150
151
Ref<FileAccessMemory> fa;
152
fa.instantiate();
153
fa->open_custom(data.ptr(), data.size());
154
155
Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
156
157
if (tr.is_valid()) {
158
tr->set_locale(etl->lang);
159
domain->add_translation(tr);
160
break;
161
}
162
}
163
164
etl++;
165
}
166
}
167
168
Vector<Vector<String>> get_extractable_message_list() {
169
const ExtractableTranslationList *etl = _extractable_translations;
170
Vector<Vector<String>> list;
171
172
while (etl->data) {
173
if (strcmp(etl->lang, "source")) {
174
etl++;
175
continue;
176
}
177
178
Vector<uint8_t> data;
179
data.resize(etl->uncomp_size);
180
const int64_t ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
181
ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");
182
183
Ref<FileAccessMemory> fa;
184
fa.instantiate();
185
fa->open_custom(data.ptr(), data.size());
186
187
// Taken from TranslationLoaderPO, modified to work specifically with POTs.
188
{
189
const String path = fa->get_path();
190
191
fa->seek(0);
192
193
enum Status {
194
STATUS_NONE,
195
STATUS_READING_ID,
196
STATUS_READING_STRING,
197
STATUS_READING_CONTEXT,
198
STATUS_READING_PLURAL,
199
};
200
201
Status status = STATUS_NONE;
202
203
String msg_id;
204
String msg_id_plural;
205
String msg_context;
206
207
int line = 1;
208
bool entered_context = false;
209
bool is_eof = false;
210
211
while (!is_eof) {
212
String l = fa->get_line().strip_edges();
213
is_eof = fa->eof_reached();
214
215
// If we reached last line and it's not a content line, break, otherwise let processing that last loop.
216
if (is_eof && l.is_empty()) {
217
if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || status == STATUS_READING_PLURAL) {
218
ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected EOF while reading POT file at: " + path + ":" + itos(line));
219
} else {
220
break;
221
}
222
}
223
224
if (l.begins_with("msgctxt")) {
225
ERR_FAIL_COND_V_MSG(status != STATUS_READING_STRING && status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
226
"Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
227
228
// In POT files, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
229
// and set "entered_context" to true to prevent adding twice.
230
if (!msg_id.is_empty()) {
231
Vector<String> msgs;
232
msgs.push_back(msg_id);
233
msgs.push_back(msg_context);
234
msgs.push_back(msg_id_plural);
235
list.push_back(msgs);
236
}
237
msg_context = "";
238
l = l.substr(7).strip_edges();
239
status = STATUS_READING_CONTEXT;
240
entered_context = true;
241
}
242
243
if (l.begins_with("msgid_plural")) {
244
if (status != STATUS_READING_ID) {
245
ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
246
}
247
l = l.substr(12).strip_edges();
248
status = STATUS_READING_PLURAL;
249
} else if (l.begins_with("msgid")) {
250
ERR_FAIL_COND_V_MSG(status == STATUS_READING_ID, Vector<Vector<String>>(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
251
252
if (!msg_id.is_empty() && !entered_context) {
253
Vector<String> msgs;
254
msgs.push_back(msg_id);
255
msgs.push_back(msg_context);
256
msgs.push_back(msg_id_plural);
257
list.push_back(msgs);
258
}
259
260
l = l.substr(5).strip_edges();
261
status = STATUS_READING_ID;
262
// If we did not encounter msgctxt, we reset context to empty to reset it.
263
if (!entered_context) {
264
msg_context = "";
265
}
266
msg_id = "";
267
msg_id_plural = "";
268
entered_context = false;
269
}
270
271
if (l.begins_with("msgstr[")) {
272
ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
273
"Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
274
l = l.substr(9).strip_edges();
275
} else if (l.begins_with("msgstr")) {
276
ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Vector<Vector<String>>(),
277
"Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
278
l = l.substr(6).strip_edges();
279
status = STATUS_READING_STRING;
280
}
281
282
if (l.is_empty() || l.begins_with("#")) {
283
line++;
284
continue; // Nothing to read or comment.
285
}
286
287
ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Vector<Vector<String>>(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
288
289
l = l.substr(1);
290
// Find final quote, ignoring escaped ones (\").
291
// The escape_next logic is necessary to properly parse things like \\"
292
// where the backslash is the one being escaped, not the quote.
293
int end_pos = -1;
294
bool escape_next = false;
295
for (int i = 0; i < l.length(); i++) {
296
if (l[i] == '\\' && !escape_next) {
297
escape_next = true;
298
continue;
299
}
300
301
if (l[i] == '"' && !escape_next) {
302
end_pos = i;
303
break;
304
}
305
306
escape_next = false;
307
}
308
309
ERR_FAIL_COND_V_MSG(end_pos == -1, Vector<Vector<String>>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
310
311
l = l.substr(0, end_pos);
312
l = l.c_unescape();
313
314
if (status == STATUS_READING_ID) {
315
msg_id += l;
316
} else if (status == STATUS_READING_CONTEXT) {
317
msg_context += l;
318
} else if (status == STATUS_READING_PLURAL) {
319
msg_id_plural += l;
320
}
321
322
line++;
323
}
324
}
325
326
etl++;
327
}
328
329
return list;
330
}
331
332