Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/translations/localization_editor.cpp
9903 views
1
/**************************************************************************/
2
/* localization_editor.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 "localization_editor.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/string/translation_server.h"
35
#include "editor/docks/filesystem_dock.h"
36
#include "editor/editor_undo_redo_manager.h"
37
#include "editor/gui/editor_file_dialog.h"
38
#include "editor/settings/editor_settings.h"
39
#include "editor/translations/editor_translation_parser.h"
40
#include "editor/translations/pot_generator.h"
41
#include "scene/gui/control.h"
42
#include "scene/gui/tab_container.h"
43
44
void LocalizationEditor::_notification(int p_what) {
45
switch (p_what) {
46
case NOTIFICATION_ENTER_TREE: {
47
translation_list->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_delete));
48
translation_pot_list->connect("button_clicked", callable_mp(this, &LocalizationEditor::_pot_delete));
49
translation_pot_add_builtin->set_pressed(GLOBAL_GET("internationalization/locale/translation_add_builtin_strings_to_pot"));
50
51
List<String> tfn;
52
ResourceLoader::get_recognized_extensions_for_type("Translation", &tfn);
53
tfn.erase("csv"); // CSV is recognized by the resource importer to generate translation files, but it's not a translation file itself.
54
for (const String &E : tfn) {
55
translation_file_open->add_filter("*." + E);
56
}
57
58
List<String> rfn;
59
ResourceLoader::get_recognized_extensions_for_type("Resource", &rfn);
60
for (const String &E : rfn) {
61
translation_res_file_open_dialog->add_filter("*." + E);
62
translation_res_option_file_open_dialog->add_filter("*." + E);
63
}
64
65
_update_pot_file_extensions();
66
pot_generate_dialog->add_filter("*.pot");
67
} break;
68
}
69
}
70
71
void LocalizationEditor::add_translation(const String &p_translation) {
72
PackedStringArray translations;
73
translations.push_back(p_translation);
74
_translation_add(translations);
75
}
76
77
void LocalizationEditor::_translation_add(const PackedStringArray &p_paths) {
78
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
79
int count = 0;
80
for (const String &path : p_paths) {
81
if (!translations.has(path)) {
82
// Don't add duplicate translation paths.
83
translations.push_back(path);
84
count += 1;
85
}
86
}
87
if (count == 0) {
88
return;
89
}
90
91
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
92
undo_redo->create_action(vformat(TTRN("Add %d Translation", "Add %d Translations", count), count));
93
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
94
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", GLOBAL_GET("internationalization/locale/translations"));
95
undo_redo->add_do_method(this, "update_translations");
96
undo_redo->add_undo_method(this, "update_translations");
97
undo_redo->add_do_method(this, "emit_signal", localization_changed);
98
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
99
undo_redo->commit_action();
100
}
101
102
void LocalizationEditor::_translation_file_open() {
103
translation_file_open->popup_file_dialog();
104
}
105
106
void LocalizationEditor::_translation_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
107
if (p_mouse_button != MouseButton::LEFT) {
108
return;
109
}
110
111
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
112
ERR_FAIL_NULL(ti);
113
114
int idx = ti->get_metadata(0);
115
116
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
117
118
ERR_FAIL_INDEX(idx, translations.size());
119
120
translations.remove_at(idx);
121
122
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
123
undo_redo->create_action(TTR("Remove Translation"));
124
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
125
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", GLOBAL_GET("internationalization/locale/translations"));
126
undo_redo->add_do_method(this, "update_translations");
127
undo_redo->add_undo_method(this, "update_translations");
128
undo_redo->add_do_method(this, "emit_signal", localization_changed);
129
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
130
undo_redo->commit_action();
131
}
132
133
void LocalizationEditor::_translation_res_file_open() {
134
translation_res_file_open_dialog->popup_file_dialog();
135
}
136
137
void LocalizationEditor::_translation_res_add(const PackedStringArray &p_paths) {
138
Variant prev;
139
Dictionary remaps;
140
141
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
142
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
143
prev = remaps;
144
}
145
146
int count = 0;
147
for (const String &path : p_paths) {
148
if (!remaps.has(path)) {
149
// Don't overwrite with an empty remap array if an array already exists for the given path.
150
remaps[path] = PackedStringArray();
151
count += 1;
152
}
153
}
154
if (count == 0) {
155
return;
156
}
157
158
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
159
undo_redo->create_action(vformat(TTRN("Translation Resource Remap: Add %d Path", "Translation Resource Remap: Add %d Paths", count), count));
160
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
161
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", prev);
162
undo_redo->add_do_method(this, "update_translations");
163
undo_redo->add_undo_method(this, "update_translations");
164
undo_redo->add_do_method(this, "emit_signal", localization_changed);
165
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
166
undo_redo->commit_action();
167
}
168
169
void LocalizationEditor::_translation_res_option_file_open() {
170
translation_res_option_file_open_dialog->popup_file_dialog();
171
}
172
173
void LocalizationEditor::_translation_res_option_add(const PackedStringArray &p_paths) {
174
ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps"));
175
176
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
177
178
TreeItem *k = translation_remap->get_selected();
179
ERR_FAIL_NULL(k);
180
181
String key = k->get_metadata(0);
182
183
ERR_FAIL_COND(!remaps.has(key));
184
PackedStringArray r = remaps[key];
185
for (int i = 0; i < p_paths.size(); i++) {
186
r.push_back(p_paths[i] + ":" + "en");
187
}
188
remaps[key] = r;
189
190
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
191
undo_redo->create_action(vformat(TTRN("Translation Resource Remap: Add %d Remap", "Translation Resource Remap: Add %d Remaps", p_paths.size()), p_paths.size()));
192
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
193
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
194
undo_redo->add_do_method(this, "update_translations");
195
undo_redo->add_undo_method(this, "update_translations");
196
undo_redo->add_do_method(this, "emit_signal", localization_changed);
197
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
198
undo_redo->commit_action();
199
}
200
201
void LocalizationEditor::_translation_res_select() {
202
if (updating_translations) {
203
return;
204
}
205
callable_mp(this, &LocalizationEditor::update_translations).call_deferred();
206
}
207
208
void LocalizationEditor::_translation_res_option_popup(bool p_arrow_clicked) {
209
TreeItem *ed = translation_remap_options->get_edited();
210
ERR_FAIL_NULL(ed);
211
212
locale_select->set_locale(ed->get_tooltip_text(1));
213
locale_select->popup_locale_dialog();
214
}
215
216
void LocalizationEditor::_translation_res_option_selected(const String &p_locale) {
217
TreeItem *ed = translation_remap_options->get_edited();
218
ERR_FAIL_NULL(ed);
219
220
ed->set_text(1, TranslationServer::get_singleton()->get_locale_name(p_locale));
221
ed->set_tooltip_text(1, p_locale);
222
223
LocalizationEditor::_translation_res_option_changed();
224
}
225
226
void LocalizationEditor::_translation_res_option_changed() {
227
if (updating_translations) {
228
return;
229
}
230
231
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
232
return;
233
}
234
235
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
236
237
TreeItem *k = translation_remap->get_selected();
238
ERR_FAIL_NULL(k);
239
TreeItem *ed = translation_remap_options->get_edited();
240
ERR_FAIL_NULL(ed);
241
242
String key = k->get_metadata(0);
243
int idx = ed->get_metadata(0);
244
String path = ed->get_metadata(1);
245
String locale = ed->get_tooltip_text(1);
246
247
ERR_FAIL_COND(!remaps.has(key));
248
PackedStringArray r = remaps[key];
249
r.set(idx, path + ":" + locale);
250
remaps[key] = r;
251
252
updating_translations = true;
253
254
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
255
undo_redo->create_action(TTR("Change Resource Remap Language"));
256
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
257
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
258
undo_redo->add_do_method(this, "update_translations");
259
undo_redo->add_undo_method(this, "update_translations");
260
undo_redo->add_do_method(this, "emit_signal", localization_changed);
261
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
262
undo_redo->commit_action();
263
updating_translations = false;
264
}
265
266
void LocalizationEditor::_translation_res_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
267
if (updating_translations) {
268
return;
269
}
270
271
if (p_mouse_button != MouseButton::LEFT) {
272
return;
273
}
274
275
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
276
return;
277
}
278
279
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
280
281
TreeItem *k = Object::cast_to<TreeItem>(p_item);
282
283
String key = k->get_metadata(0);
284
ERR_FAIL_COND(!remaps.has(key));
285
286
remaps.erase(key);
287
288
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
289
undo_redo->create_action(TTR("Remove Resource Remap"));
290
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
291
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
292
undo_redo->add_do_method(this, "update_translations");
293
undo_redo->add_undo_method(this, "update_translations");
294
undo_redo->add_do_method(this, "emit_signal", localization_changed);
295
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
296
undo_redo->commit_action();
297
}
298
299
void LocalizationEditor::_translation_res_option_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
300
if (updating_translations) {
301
return;
302
}
303
304
if (p_mouse_button != MouseButton::LEFT) {
305
return;
306
}
307
308
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
309
return;
310
}
311
312
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
313
314
TreeItem *k = translation_remap->get_selected();
315
ERR_FAIL_NULL(k);
316
TreeItem *ed = Object::cast_to<TreeItem>(p_item);
317
ERR_FAIL_NULL(ed);
318
319
String key = k->get_metadata(0);
320
int idx = ed->get_metadata(0);
321
322
ERR_FAIL_COND(!remaps.has(key));
323
PackedStringArray r = remaps[key];
324
ERR_FAIL_INDEX(idx, r.size());
325
r.remove_at(idx);
326
remaps[key] = r;
327
328
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
329
undo_redo->create_action(TTR("Remove Resource Remap Option"));
330
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
331
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
332
undo_redo->add_do_method(this, "update_translations");
333
undo_redo->add_undo_method(this, "update_translations");
334
undo_redo->add_do_method(this, "emit_signal", localization_changed);
335
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
336
undo_redo->commit_action();
337
}
338
339
void LocalizationEditor::_pot_add(const PackedStringArray &p_paths) {
340
PackedStringArray pot_translations = GLOBAL_GET("internationalization/locale/translations_pot_files");
341
int count = 0;
342
for (const String &path : p_paths) {
343
if (!pot_translations.has(path)) {
344
pot_translations.push_back(path);
345
count += 1;
346
}
347
}
348
if (count == 0) {
349
return;
350
}
351
352
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
353
undo_redo->create_action(vformat(TTRN("Add %d file for POT generation", "Add %d files for POT generation", count), count));
354
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
355
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", GLOBAL_GET("internationalization/locale/translations_pot_files"));
356
undo_redo->add_do_method(this, "update_translations");
357
undo_redo->add_undo_method(this, "update_translations");
358
undo_redo->add_do_method(this, "emit_signal", localization_changed);
359
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
360
undo_redo->commit_action();
361
}
362
363
void LocalizationEditor::_pot_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
364
if (p_mouse_button != MouseButton::LEFT) {
365
return;
366
}
367
368
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
369
ERR_FAIL_NULL(ti);
370
371
int idx = ti->get_metadata(0);
372
373
PackedStringArray pot_translations = GLOBAL_GET("internationalization/locale/translations_pot_files");
374
375
ERR_FAIL_INDEX(idx, pot_translations.size());
376
377
pot_translations.remove_at(idx);
378
379
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
380
undo_redo->create_action(TTR("Remove file from POT generation"));
381
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
382
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", GLOBAL_GET("internationalization/locale/translations_pot_files"));
383
undo_redo->add_do_method(this, "update_translations");
384
undo_redo->add_undo_method(this, "update_translations");
385
undo_redo->add_do_method(this, "emit_signal", localization_changed);
386
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
387
undo_redo->commit_action();
388
}
389
390
void LocalizationEditor::_pot_file_open() {
391
pot_file_open_dialog->popup_file_dialog();
392
}
393
394
void LocalizationEditor::_pot_generate_open() {
395
pot_generate_dialog->popup_file_dialog();
396
}
397
398
void LocalizationEditor::_pot_add_builtin_toggled() {
399
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_add_builtin_strings_to_pot", translation_pot_add_builtin->is_pressed());
400
ProjectSettings::get_singleton()->save();
401
}
402
403
void LocalizationEditor::_pot_generate(const String &p_file) {
404
EditorSettings::get_singleton()->set_project_metadata("pot_generator", "last_pot_path", p_file);
405
POTGenerator::get_singleton()->generate_pot(p_file);
406
}
407
408
void LocalizationEditor::_update_pot_file_extensions() {
409
pot_file_open_dialog->clear_filters();
410
List<String> translation_parse_file_extensions;
411
EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
412
for (const String &E : translation_parse_file_extensions) {
413
pot_file_open_dialog->add_filter("*." + E);
414
}
415
}
416
417
void LocalizationEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
418
p_fs_dock->connect("files_moved", callable_mp(this, &LocalizationEditor::_filesystem_files_moved));
419
p_fs_dock->connect("file_removed", callable_mp(this, &LocalizationEditor::_filesystem_file_removed));
420
}
421
422
void LocalizationEditor::_filesystem_files_moved(const String &p_old_file, const String &p_new_file) {
423
// Update remaps if the moved file is a part of them.
424
Dictionary remaps;
425
bool remaps_changed = false;
426
427
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
428
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
429
}
430
431
// Check for the keys.
432
if (remaps.has(p_old_file)) {
433
PackedStringArray remapped_files = remaps[p_old_file];
434
remaps.erase(p_old_file);
435
remaps[p_new_file] = remapped_files;
436
remaps_changed = true;
437
print_verbose(vformat("Changed remap key \"%s\" to \"%s\" due to a moved file.", p_old_file, p_new_file));
438
}
439
440
// Check for the Array elements of the values.
441
Array remap_keys = remaps.keys();
442
for (const Variant &remap_key : remap_keys) {
443
PackedStringArray remapped_files = remaps[remap_key];
444
bool remapped_files_updated = false;
445
446
for (int j = 0; j < remapped_files.size(); j++) {
447
int splitter_pos = remapped_files[j].rfind_char(':');
448
String res_path = remapped_files[j].substr(0, splitter_pos);
449
450
if (res_path == p_old_file) {
451
String locale_name = remapped_files[j].substr(splitter_pos + 1);
452
// Replace the element at that index.
453
remapped_files.insert(j, p_new_file + ":" + locale_name);
454
remapped_files.remove_at(j + 1);
455
remaps_changed = true;
456
remapped_files_updated = true;
457
print_verbose(vformat("Changed remap value \"%s\" to \"%s\" of key \"%s\" due to a moved file.", res_path + ":" + locale_name, remapped_files[j], remap_key));
458
}
459
}
460
461
if (remapped_files_updated) {
462
remaps[remap_key] = remapped_files;
463
}
464
}
465
466
if (remaps_changed) {
467
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_remaps", remaps);
468
update_translations();
469
emit_signal("localization_changed");
470
}
471
}
472
473
void LocalizationEditor::_filesystem_file_removed(const String &p_file) {
474
// Check if the remaps are affected.
475
Dictionary remaps;
476
477
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
478
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
479
}
480
481
bool remaps_changed = remaps.has(p_file);
482
483
if (!remaps_changed) {
484
Array remap_keys = remaps.keys();
485
for (int i = 0; i < remap_keys.size() && !remaps_changed; i++) {
486
PackedStringArray remapped_files = remaps[remap_keys[i]];
487
for (int j = 0; j < remapped_files.size() && !remaps_changed; j++) {
488
int splitter_pos = remapped_files[j].rfind_char(':');
489
String res_path = remapped_files[j].substr(0, splitter_pos);
490
remaps_changed = p_file == res_path;
491
if (remaps_changed) {
492
print_verbose(vformat("Remap value \"%s\" of key \"%s\" has been removed from the file system.", remapped_files[j], remap_keys[i]));
493
}
494
}
495
}
496
} else {
497
print_verbose(vformat("Remap key \"%s\" has been removed from the file system.", p_file));
498
}
499
500
if (remaps_changed) {
501
update_translations();
502
emit_signal("localization_changed");
503
}
504
}
505
506
void LocalizationEditor::update_translations() {
507
if (updating_translations) {
508
return;
509
}
510
511
updating_translations = true;
512
513
translation_list->clear();
514
TreeItem *root = translation_list->create_item(nullptr);
515
translation_list->set_hide_root(true);
516
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
517
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
518
for (int i = 0; i < translations.size(); i++) {
519
TreeItem *t = translation_list->create_item(root);
520
t->set_editable(0, false);
521
t->set_text(0, translations[i].replace_first("res://", ""));
522
t->set_tooltip_text(0, translations[i]);
523
t->set_metadata(0, i);
524
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
525
}
526
}
527
528
// Update translation remaps.
529
String remap_selected;
530
if (translation_remap->get_selected()) {
531
remap_selected = translation_remap->get_selected()->get_metadata(0);
532
}
533
534
translation_remap->clear();
535
translation_remap_options->clear();
536
root = translation_remap->create_item(nullptr);
537
TreeItem *root2 = translation_remap_options->create_item(nullptr);
538
translation_remap->set_hide_root(true);
539
translation_remap_options->set_hide_root(true);
540
translation_res_option_add_button->set_disabled(true);
541
542
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
543
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
544
Vector<String> keys;
545
for (const KeyValue<Variant, Variant> &kv : remaps) {
546
keys.push_back(kv.key);
547
}
548
keys.sort();
549
550
for (int i = 0; i < keys.size(); i++) {
551
TreeItem *t = translation_remap->create_item(root);
552
t->set_editable(0, false);
553
t->set_text(0, keys[i].replace_first("res://", ""));
554
t->set_tooltip_text(0, keys[i]);
555
t->set_metadata(0, keys[i]);
556
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
557
558
// Display that it has been removed if this is the case.
559
if (!FileAccess::exists(keys[i])) {
560
t->set_text(0, t->get_text(0) + vformat(" (%s)", TTR("Removed")));
561
t->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t->get_tooltip_text(0)));
562
}
563
564
if (keys[i] == remap_selected) {
565
t->select(0);
566
translation_res_option_add_button->set_disabled(false);
567
568
PackedStringArray selected = remaps[keys[i]];
569
for (int j = 0; j < selected.size(); j++) {
570
const String &s2 = selected[j];
571
int qp = s2.rfind_char(':');
572
String path = s2.substr(0, qp);
573
String locale = s2.substr(qp + 1);
574
575
TreeItem *t2 = translation_remap_options->create_item(root2);
576
t2->set_editable(0, false);
577
t2->set_text(0, path.replace_first("res://", ""));
578
t2->set_tooltip_text(0, path);
579
t2->set_metadata(0, j);
580
t2->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
581
t2->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
582
t2->set_text(1, TranslationServer::get_singleton()->get_locale_name(locale));
583
t2->set_editable(1, true);
584
t2->set_metadata(1, path);
585
t2->set_tooltip_text(1, locale);
586
587
// Display that it has been removed if this is the case.
588
if (!FileAccess::exists(path)) {
589
t2->set_text(0, t2->get_text(0) + vformat(" (%s)", TTR("Removed")));
590
t2->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t2->get_tooltip_text(0)));
591
}
592
}
593
}
594
}
595
}
596
597
// Update translation POT files.
598
translation_pot_list->clear();
599
root = translation_pot_list->create_item(nullptr);
600
translation_pot_list->set_hide_root(true);
601
PackedStringArray pot_translations = GLOBAL_GET("internationalization/locale/translations_pot_files");
602
for (int i = 0; i < pot_translations.size(); i++) {
603
TreeItem *t = translation_pot_list->create_item(root);
604
t->set_editable(0, false);
605
t->set_text(0, pot_translations[i].replace_first("res://", ""));
606
t->set_tooltip_text(0, pot_translations[i]);
607
t->set_metadata(0, i);
608
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
609
}
610
611
// New translation parser plugin might extend possible file extensions in POT generation.
612
_update_pot_file_extensions();
613
614
pot_generate_button->set_disabled(pot_translations.is_empty());
615
616
updating_translations = false;
617
}
618
619
void LocalizationEditor::_bind_methods() {
620
ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
621
622
ADD_SIGNAL(MethodInfo("localization_changed"));
623
}
624
625
LocalizationEditor::LocalizationEditor() {
626
localization_changed = "localization_changed";
627
628
TabContainer *translations = memnew(TabContainer);
629
translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
630
add_child(translations);
631
632
{
633
VBoxContainer *tvb = memnew(VBoxContainer);
634
tvb->set_name(TTRC("Translations"));
635
translations->add_child(tvb);
636
637
HBoxContainer *thb = memnew(HBoxContainer);
638
Label *l = memnew(Label(TTRC("Translations:")));
639
l->set_theme_type_variation("HeaderSmall");
640
thb->add_child(l);
641
thb->add_spacer();
642
tvb->add_child(thb);
643
644
Button *addtr = memnew(Button(TTRC("Add...")));
645
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_file_open));
646
thb->add_child(addtr);
647
648
VBoxContainer *tmc = memnew(VBoxContainer);
649
tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
650
tvb->add_child(tmc);
651
652
translation_list = memnew(Tree);
653
translation_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
654
tmc->add_child(translation_list);
655
656
locale_select = memnew(EditorLocaleDialog);
657
locale_select->connect("locale_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_selected));
658
add_child(locale_select);
659
660
translation_file_open = memnew(EditorFileDialog);
661
translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
662
translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
663
add_child(translation_file_open);
664
}
665
666
{
667
VBoxContainer *tvb = memnew(VBoxContainer);
668
tvb->set_name(TTRC("Remaps"));
669
translations->add_child(tvb);
670
671
HBoxContainer *thb = memnew(HBoxContainer);
672
Label *l = memnew(Label(TTRC("Resources:")));
673
l->set_theme_type_variation("HeaderSmall");
674
thb->add_child(l);
675
thb->add_spacer();
676
tvb->add_child(thb);
677
678
Button *addtr = memnew(Button(TTRC("Add...")));
679
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_res_file_open));
680
thb->add_child(addtr);
681
682
VBoxContainer *tmc = memnew(VBoxContainer);
683
tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
684
tvb->add_child(tmc);
685
686
translation_remap = memnew(Tree);
687
translation_remap->set_v_size_flags(Control::SIZE_EXPAND_FILL);
688
translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
689
translation_remap->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_delete));
690
tmc->add_child(translation_remap);
691
692
translation_res_file_open_dialog = memnew(EditorFileDialog);
693
translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
694
translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
695
add_child(translation_res_file_open_dialog);
696
697
thb = memnew(HBoxContainer);
698
l = memnew(Label(TTRC("Remaps by Locale:")));
699
l->set_theme_type_variation("HeaderSmall");
700
thb->add_child(l);
701
thb->add_spacer();
702
tvb->add_child(thb);
703
704
addtr = memnew(Button(TTRC("Add...")));
705
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
706
translation_res_option_add_button = addtr;
707
thb->add_child(addtr);
708
709
tmc = memnew(VBoxContainer);
710
tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
711
tvb->add_child(tmc);
712
713
translation_remap_options = memnew(Tree);
714
translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
715
translation_remap_options->set_columns(2);
716
translation_remap_options->set_column_title(0, TTRC("Path"));
717
translation_remap_options->set_column_title(1, TTRC("Locale"));
718
translation_remap_options->set_column_titles_visible(true);
719
translation_remap_options->set_column_expand(0, true);
720
translation_remap_options->set_column_clip_content(0, true);
721
translation_remap_options->set_column_expand(1, false);
722
translation_remap_options->set_column_clip_content(1, false);
723
translation_remap_options->set_column_custom_minimum_width(1, 250);
724
translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
725
translation_remap_options->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
726
translation_remap_options->connect("custom_popup_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_popup));
727
tmc->add_child(translation_remap_options);
728
729
translation_res_option_file_open_dialog = memnew(EditorFileDialog);
730
translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
731
translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
732
add_child(translation_res_option_file_open_dialog);
733
}
734
735
{
736
VBoxContainer *tvb = memnew(VBoxContainer);
737
tvb->set_name(TTRC("POT Generation"));
738
translations->add_child(tvb);
739
740
HBoxContainer *thb = memnew(HBoxContainer);
741
Label *l = memnew(Label(TTRC("Files with translation strings:")));
742
l->set_theme_type_variation("HeaderSmall");
743
thb->add_child(l);
744
thb->add_spacer();
745
tvb->add_child(thb);
746
747
Button *addtr = memnew(Button(TTRC("Add...")));
748
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_pot_file_open));
749
thb->add_child(addtr);
750
751
pot_generate_button = memnew(Button(TTRC("Generate POT")));
752
pot_generate_button->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_pot_generate_open));
753
thb->add_child(pot_generate_button);
754
755
translation_pot_list = memnew(Tree);
756
translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
757
tvb->add_child(translation_pot_list);
758
759
translation_pot_add_builtin = memnew(CheckBox(TTRC("Add Built-in Strings to POT")));
760
translation_pot_add_builtin->set_tooltip_text(TTRC("Add strings from built-in components such as certain Control nodes."));
761
translation_pot_add_builtin->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_pot_add_builtin_toggled));
762
tvb->add_child(translation_pot_add_builtin);
763
764
pot_generate_dialog = memnew(EditorFileDialog);
765
pot_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
766
pot_generate_dialog->set_current_path(EditorSettings::get_singleton()->get_project_metadata("pot_generator", "last_pot_path", String()));
767
pot_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_pot_generate));
768
add_child(pot_generate_dialog);
769
770
pot_file_open_dialog = memnew(EditorFileDialog);
771
pot_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
772
pot_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_pot_add));
773
add_child(pot_file_open_dialog);
774
}
775
}
776
777