Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/translations/localization_editor.cpp
20852 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/template_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
template_source_list->connect("button_clicked", callable_mp(this, &LocalizationEditor::_template_source_delete));
49
template_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_template_source_file_extensions();
66
template_generate_dialog->add_filter("*.pot");
67
template_generate_dialog->add_filter("*.csv");
68
} break;
69
70
case NOTIFICATION_DRAG_END: {
71
for (Tree *tree : trees) {
72
tree->set_drop_mode_flags(Tree::DROP_MODE_DISABLED);
73
}
74
} break;
75
}
76
}
77
78
void LocalizationEditor::add_translation(const String &p_translation) {
79
PackedStringArray translations;
80
translations.push_back(p_translation);
81
_translation_add(translations);
82
}
83
84
void LocalizationEditor::_translation_add(const PackedStringArray &p_paths) {
85
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
86
int count = 0;
87
for (const String &path : p_paths) {
88
if (!translations.has(path)) {
89
// Don't add duplicate translation paths.
90
translations.push_back(path);
91
count += 1;
92
}
93
}
94
if (count == 0) {
95
return;
96
}
97
98
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
99
undo_redo->create_action(vformat(TTRN("Add %d Translation", "Add %d Translations", count), count));
100
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
101
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", GLOBAL_GET("internationalization/locale/translations"));
102
undo_redo->add_do_method(this, "update_translations");
103
undo_redo->add_undo_method(this, "update_translations");
104
undo_redo->add_do_method(this, "emit_signal", localization_changed);
105
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
106
undo_redo->commit_action();
107
}
108
109
void LocalizationEditor::_translation_file_open() {
110
translation_file_open->popup_file_dialog();
111
}
112
113
void LocalizationEditor::_translation_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
114
if (p_mouse_button != MouseButton::LEFT) {
115
return;
116
}
117
118
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
119
ERR_FAIL_NULL(ti);
120
121
int idx = ti->get_metadata(0);
122
123
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
124
125
ERR_FAIL_INDEX(idx, translations.size());
126
127
translations.remove_at(idx);
128
129
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
130
undo_redo->create_action(TTR("Remove Translation"));
131
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
132
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", GLOBAL_GET("internationalization/locale/translations"));
133
undo_redo->add_do_method(this, "update_translations");
134
undo_redo->add_undo_method(this, "update_translations");
135
undo_redo->add_do_method(this, "emit_signal", localization_changed);
136
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
137
undo_redo->commit_action();
138
}
139
140
void LocalizationEditor::_translation_res_file_open() {
141
translation_res_file_open_dialog->popup_file_dialog();
142
}
143
144
void LocalizationEditor::_translation_res_add(const PackedStringArray &p_paths) {
145
Variant prev;
146
Dictionary remaps;
147
148
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
149
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
150
prev = remaps;
151
}
152
153
int count = 0;
154
for (const String &path : p_paths) {
155
if (!remaps.has(path)) {
156
// Don't overwrite with an empty remap array if an array already exists for the given path.
157
remaps[path] = PackedStringArray();
158
count += 1;
159
}
160
}
161
if (count == 0) {
162
return;
163
}
164
165
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
166
undo_redo->create_action(vformat(TTRN("Translation Resource Remap: Add %d Path", "Translation Resource Remap: Add %d Paths", count), count));
167
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
168
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", prev);
169
undo_redo->add_do_method(this, "update_translations");
170
undo_redo->add_undo_method(this, "update_translations");
171
undo_redo->add_do_method(this, "emit_signal", localization_changed);
172
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
173
undo_redo->commit_action();
174
}
175
176
void LocalizationEditor::_translation_res_option_file_open() {
177
translation_res_option_file_open_dialog->popup_file_dialog();
178
}
179
180
void LocalizationEditor::_translation_res_option_add(const PackedStringArray &p_paths) {
181
ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps"));
182
183
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
184
185
TreeItem *k = translation_remap->get_selected();
186
ERR_FAIL_NULL(k);
187
188
String key = k->get_metadata(0);
189
190
ERR_FAIL_COND(!remaps.has(key));
191
PackedStringArray r = remaps[key];
192
for (int i = 0; i < p_paths.size(); i++) {
193
r.push_back(p_paths[i] + ":" + "en");
194
}
195
remaps[key] = r;
196
197
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
198
undo_redo->create_action(vformat(TTRN("Translation Resource Remap: Add %d Remap", "Translation Resource Remap: Add %d Remaps", p_paths.size()), p_paths.size()));
199
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
200
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
201
undo_redo->add_do_method(this, "update_translations");
202
undo_redo->add_undo_method(this, "update_translations");
203
undo_redo->add_do_method(this, "emit_signal", localization_changed);
204
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
205
undo_redo->commit_action();
206
}
207
208
void LocalizationEditor::_translation_res_select() {
209
if (updating_translations) {
210
return;
211
}
212
callable_mp(this, &LocalizationEditor::update_translations).call_deferred();
213
}
214
215
void LocalizationEditor::_translation_res_option_popup(bool p_arrow_clicked) {
216
TreeItem *ed = translation_remap_options->get_edited();
217
ERR_FAIL_NULL(ed);
218
219
locale_select->set_locale(ed->get_tooltip_text(1));
220
locale_select->popup_locale_dialog();
221
}
222
223
void LocalizationEditor::_translation_res_option_selected(const String &p_locale) {
224
TreeItem *ed = translation_remap_options->get_edited();
225
ERR_FAIL_NULL(ed);
226
227
ed->set_text(1, TranslationServer::get_singleton()->get_locale_name(p_locale));
228
ed->set_tooltip_text(1, p_locale);
229
230
LocalizationEditor::_translation_res_option_changed();
231
}
232
233
void LocalizationEditor::_translation_res_option_changed() {
234
if (updating_translations) {
235
return;
236
}
237
238
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
239
return;
240
}
241
242
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
243
244
TreeItem *k = translation_remap->get_selected();
245
ERR_FAIL_NULL(k);
246
TreeItem *ed = translation_remap_options->get_edited();
247
ERR_FAIL_NULL(ed);
248
249
String key = k->get_metadata(0);
250
int idx = ed->get_metadata(0);
251
String path = ed->get_metadata(1);
252
String locale = ed->get_tooltip_text(1);
253
254
ERR_FAIL_COND(!remaps.has(key));
255
PackedStringArray r = remaps[key];
256
r.set(idx, path + ":" + locale);
257
remaps[key] = r;
258
259
updating_translations = true;
260
261
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
262
undo_redo->create_action(TTR("Change Resource Remap Language"));
263
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
264
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
265
undo_redo->add_do_method(this, "update_translations");
266
undo_redo->add_undo_method(this, "update_translations");
267
undo_redo->add_do_method(this, "emit_signal", localization_changed);
268
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
269
undo_redo->commit_action();
270
updating_translations = false;
271
}
272
273
void LocalizationEditor::_translation_res_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
274
if (updating_translations) {
275
return;
276
}
277
278
if (p_mouse_button != MouseButton::LEFT) {
279
return;
280
}
281
282
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
283
return;
284
}
285
286
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
287
288
TreeItem *k = Object::cast_to<TreeItem>(p_item);
289
290
String key = k->get_metadata(0);
291
ERR_FAIL_COND(!remaps.has(key));
292
293
remaps.erase(key);
294
295
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
296
undo_redo->create_action(TTR("Remove Resource Remap"));
297
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
298
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
299
undo_redo->add_do_method(this, "update_translations");
300
undo_redo->add_undo_method(this, "update_translations");
301
undo_redo->add_do_method(this, "emit_signal", localization_changed);
302
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
303
undo_redo->commit_action();
304
}
305
306
void LocalizationEditor::_translation_res_option_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
307
if (updating_translations) {
308
return;
309
}
310
311
if (p_mouse_button != MouseButton::LEFT) {
312
return;
313
}
314
315
if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
316
return;
317
}
318
319
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
320
321
TreeItem *k = translation_remap->get_selected();
322
ERR_FAIL_NULL(k);
323
TreeItem *ed = Object::cast_to<TreeItem>(p_item);
324
ERR_FAIL_NULL(ed);
325
326
String key = k->get_metadata(0);
327
int idx = ed->get_metadata(0);
328
329
ERR_FAIL_COND(!remaps.has(key));
330
PackedStringArray r = remaps[key];
331
ERR_FAIL_INDEX(idx, r.size());
332
r.remove_at(idx);
333
remaps[key] = r;
334
335
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
336
undo_redo->create_action(TTR("Remove Resource Remap Option"));
337
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
338
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", GLOBAL_GET("internationalization/locale/translation_remaps"));
339
undo_redo->add_do_method(this, "update_translations");
340
undo_redo->add_undo_method(this, "update_translations");
341
undo_redo->add_do_method(this, "emit_signal", localization_changed);
342
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
343
undo_redo->commit_action();
344
}
345
346
void LocalizationEditor::_template_source_add(const PackedStringArray &p_paths) {
347
PackedStringArray sources = GLOBAL_GET("internationalization/locale/translations_pot_files");
348
int count = 0;
349
for (const String &path : p_paths) {
350
if (!sources.has(path)) {
351
sources.push_back(path);
352
count += 1;
353
}
354
}
355
if (count == 0) {
356
return;
357
}
358
359
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
360
undo_redo->create_action(vformat(TTRN("Add %d file for template generation", "Add %d files for template generation", count), count));
361
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", sources);
362
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", GLOBAL_GET("internationalization/locale/translations_pot_files"));
363
undo_redo->add_do_method(this, "update_translations");
364
undo_redo->add_undo_method(this, "update_translations");
365
undo_redo->add_do_method(this, "emit_signal", localization_changed);
366
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
367
undo_redo->commit_action();
368
}
369
370
void LocalizationEditor::_template_source_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
371
if (p_mouse_button != MouseButton::LEFT) {
372
return;
373
}
374
375
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
376
ERR_FAIL_NULL(ti);
377
378
int idx = ti->get_metadata(0);
379
380
PackedStringArray sources = GLOBAL_GET("internationalization/locale/translations_pot_files");
381
382
ERR_FAIL_INDEX(idx, sources.size());
383
384
sources.remove_at(idx);
385
386
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
387
undo_redo->create_action(TTR("Remove file from template generation"));
388
undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", sources);
389
undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", GLOBAL_GET("internationalization/locale/translations_pot_files"));
390
undo_redo->add_do_method(this, "update_translations");
391
undo_redo->add_undo_method(this, "update_translations");
392
undo_redo->add_do_method(this, "emit_signal", localization_changed);
393
undo_redo->add_undo_method(this, "emit_signal", localization_changed);
394
undo_redo->commit_action();
395
}
396
397
void LocalizationEditor::_template_source_file_open() {
398
template_source_open_dialog->popup_file_dialog();
399
}
400
401
void LocalizationEditor::_template_generate_open() {
402
template_generate_dialog->popup_file_dialog();
403
}
404
405
void LocalizationEditor::_template_add_builtin_toggled() {
406
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_add_builtin_strings_to_pot", template_add_builtin->is_pressed());
407
ProjectSettings::get_singleton()->save();
408
}
409
410
void LocalizationEditor::_template_generate(const String &p_file) {
411
EditorSettings::get_singleton()->set_project_metadata("pot_generator", "last_pot_path", p_file);
412
TranslationTemplateGenerator::get_singleton()->generate(p_file);
413
}
414
415
void LocalizationEditor::_update_template_source_file_extensions() {
416
template_source_open_dialog->clear_filters();
417
List<String> translation_parse_file_extensions;
418
EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
419
for (const String &E : translation_parse_file_extensions) {
420
template_source_open_dialog->add_filter("*." + E);
421
}
422
}
423
424
void LocalizationEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
425
p_fs_dock->connect("files_moved", callable_mp(this, &LocalizationEditor::_filesystem_files_moved));
426
p_fs_dock->connect("file_removed", callable_mp(this, &LocalizationEditor::_filesystem_file_removed));
427
}
428
429
void LocalizationEditor::_filesystem_files_moved(const String &p_old_file, const String &p_new_file) {
430
// Update source files if the moved file is a part of them.
431
PackedStringArray sources = GLOBAL_GET("internationalization/locale/translations_pot_files");
432
if (sources.has(p_old_file)) {
433
sources.erase(p_old_file);
434
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translations_pot_files", sources);
435
436
PackedStringArray new_file;
437
new_file.push_back(p_new_file);
438
_template_source_add(new_file);
439
}
440
441
// Update remaps if the moved file is a part of them.
442
Dictionary remaps;
443
bool remaps_changed = false;
444
445
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
446
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
447
}
448
449
// Check for the keys.
450
if (remaps.has(p_old_file)) {
451
PackedStringArray remapped_files = remaps[p_old_file];
452
remaps.erase(p_old_file);
453
remaps[p_new_file] = remapped_files;
454
remaps_changed = true;
455
print_verbose(vformat("Changed remap key \"%s\" to \"%s\" due to a moved file.", p_old_file, p_new_file));
456
}
457
458
// Check for the Array elements of the values.
459
Array remap_keys = remaps.keys();
460
for (const Variant &remap_key : remap_keys) {
461
PackedStringArray remapped_files = remaps[remap_key];
462
bool remapped_files_updated = false;
463
464
for (int j = 0; j < remapped_files.size(); j++) {
465
int splitter_pos = remapped_files[j].rfind_char(':');
466
String res_path = remapped_files[j].substr(0, splitter_pos);
467
468
if (res_path == p_old_file) {
469
String locale_name = remapped_files[j].substr(splitter_pos + 1);
470
// Replace the element at that index.
471
remapped_files.insert(j, p_new_file + ":" + locale_name);
472
remapped_files.remove_at(j + 1);
473
remaps_changed = true;
474
remapped_files_updated = true;
475
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));
476
}
477
}
478
479
if (remapped_files_updated) {
480
remaps[remap_key] = remapped_files;
481
}
482
}
483
484
if (remaps_changed) {
485
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_remaps", remaps);
486
update_translations();
487
emit_signal("localization_changed");
488
}
489
}
490
491
void LocalizationEditor::_filesystem_file_removed(const String &p_file) {
492
// Check if the source files are affected.
493
PackedStringArray sources = GLOBAL_GET("internationalization/locale/translations_pot_files");
494
if (sources.has(p_file)) {
495
sources.erase(p_file);
496
ProjectSettings::get_singleton()->set_setting("internationalization/locale/translations_pot_files", sources);
497
}
498
499
// Check if the remaps are affected.
500
Dictionary remaps;
501
502
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
503
remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
504
}
505
506
bool remaps_changed = remaps.has(p_file);
507
508
if (!remaps_changed) {
509
Array remap_keys = remaps.keys();
510
for (int i = 0; i < remap_keys.size() && !remaps_changed; i++) {
511
PackedStringArray remapped_files = remaps[remap_keys[i]];
512
for (int j = 0; j < remapped_files.size() && !remaps_changed; j++) {
513
int splitter_pos = remapped_files[j].rfind_char(':');
514
String res_path = remapped_files[j].substr(0, splitter_pos);
515
remaps_changed = p_file == res_path;
516
if (remaps_changed) {
517
print_verbose(vformat("Remap value \"%s\" of key \"%s\" has been removed from the file system.", remapped_files[j], remap_keys[i]));
518
}
519
}
520
}
521
} else {
522
print_verbose(vformat("Remap key \"%s\" has been removed from the file system.", p_file));
523
}
524
525
if (remaps_changed) {
526
update_translations();
527
emit_signal("localization_changed");
528
}
529
}
530
531
Variant LocalizationEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
532
Tree *tree = Object::cast_to<Tree>(p_from);
533
ERR_FAIL_COND_V(trees.find(tree) == -1, Variant());
534
535
if (tree->get_button_id_at_position(p_point) != -1) {
536
return Variant();
537
}
538
539
TreeItem *selected = tree->get_next_selected(nullptr);
540
if (!selected) {
541
return Variant();
542
}
543
tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
544
545
Label *preview = memnew(Label);
546
preview->set_text(selected->get_text(0));
547
set_drag_preview(preview);
548
549
Dictionary drag_data;
550
drag_data["type"] = tree_data_types[tree];
551
drag_data["item"] = selected;
552
553
return drag_data;
554
}
555
556
bool LocalizationEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
557
Tree *tree = Object::cast_to<Tree>(p_from);
558
ERR_FAIL_COND_V(trees.find(tree) == -1, false);
559
560
Dictionary drop_data = p_data;
561
return drop_data.get("type", "") == tree_data_types[tree];
562
}
563
564
void LocalizationEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
565
Tree *tree = Object::cast_to<Tree>(p_from);
566
ERR_FAIL_COND(trees.find(tree) == -1);
567
568
if (!can_drop_data_fw(p_point, p_data, p_from)) {
569
return;
570
}
571
572
TreeItem *item = tree->get_item_at_position(p_point);
573
if (!item) {
574
return;
575
}
576
int section = MAX(tree->get_drop_section_at_position(p_point), 0);
577
Dictionary drop_data = p_data;
578
579
TreeItem *from = Object::cast_to<TreeItem>(drop_data["item"]);
580
if (item == from) {
581
return;
582
}
583
584
const StringName &setting = tree_settings[tree];
585
PackedStringArray setting_value = GLOBAL_GET(setting);
586
const PackedStringArray original_setting_value = setting_value;
587
const int index_from = from->get_metadata(0);
588
const String path = setting_value[index_from];
589
590
int target_index = item->get_metadata(0);
591
target_index = MAX(target_index + section, 0);
592
if (target_index > index_from) {
593
target_index -= 1; // Account for item being removed.
594
}
595
596
if (target_index == index_from) {
597
return;
598
}
599
600
setting_value.remove_at(index_from);
601
setting_value.insert(target_index, path);
602
603
EditorUndoRedoManager *ur_man = EditorUndoRedoManager::get_singleton();
604
ur_man->create_action(TTR("Rearrange Localization Items"));
605
ur_man->add_do_method(ProjectSettings::get_singleton(), "set", setting, setting_value);
606
ur_man->add_do_method(ProjectSettings::get_singleton(), "save");
607
ur_man->add_do_method(this, "update_translations");
608
ur_man->add_undo_method(ProjectSettings::get_singleton(), "set", setting, original_setting_value);
609
ur_man->add_undo_method(ProjectSettings::get_singleton(), "save");
610
ur_man->add_undo_method(this, "update_translations");
611
ur_man->commit_action();
612
}
613
614
void LocalizationEditor::update_translations() {
615
if (updating_translations) {
616
return;
617
}
618
619
updating_translations = true;
620
621
translation_list->clear();
622
TreeItem *root = translation_list->create_item(nullptr);
623
translation_list->set_hide_root(true);
624
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
625
PackedStringArray translations = GLOBAL_GET("internationalization/locale/translations");
626
for (int i = 0; i < translations.size(); i++) {
627
TreeItem *t = translation_list->create_item(root);
628
t->set_editable(0, false);
629
t->set_text(0, translations[i].replace_first("res://", ""));
630
t->set_tooltip_text(0, translations[i]);
631
t->set_metadata(0, i);
632
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
633
}
634
}
635
636
// Update translation remaps.
637
String remap_selected;
638
if (translation_remap->get_selected()) {
639
remap_selected = translation_remap->get_selected()->get_metadata(0);
640
}
641
642
translation_remap->clear();
643
translation_remap_options->clear();
644
root = translation_remap->create_item(nullptr);
645
TreeItem *root2 = translation_remap_options->create_item(nullptr);
646
translation_remap->set_hide_root(true);
647
translation_remap_options->set_hide_root(true);
648
translation_res_option_add_button->set_disabled(true);
649
650
if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
651
Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
652
Vector<String> keys;
653
for (const KeyValue<Variant, Variant> &kv : remaps) {
654
keys.push_back(kv.key);
655
}
656
keys.sort();
657
658
for (int i = 0; i < keys.size(); i++) {
659
TreeItem *t = translation_remap->create_item(root);
660
t->set_editable(0, false);
661
t->set_text(0, keys[i].replace_first("res://", ""));
662
t->set_tooltip_text(0, keys[i]);
663
t->set_metadata(0, keys[i]);
664
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
665
666
// Display that it has been removed if this is the case.
667
if (!FileAccess::exists(keys[i])) {
668
t->set_text(0, t->get_text(0) + vformat(" (%s)", TTR("Removed")));
669
t->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t->get_tooltip_text(0)));
670
}
671
672
if (keys[i] == remap_selected) {
673
t->select(0);
674
translation_res_option_add_button->set_disabled(false);
675
676
PackedStringArray selected = remaps[keys[i]];
677
for (int j = 0; j < selected.size(); j++) {
678
const String &s2 = selected[j];
679
int qp = s2.rfind_char(':');
680
String path = s2.substr(0, qp);
681
String locale = s2.substr(qp + 1);
682
683
TreeItem *t2 = translation_remap_options->create_item(root2);
684
t2->set_editable(0, false);
685
t2->set_text(0, path.replace_first("res://", ""));
686
t2->set_tooltip_text(0, path);
687
t2->set_metadata(0, j);
688
t2->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
689
t2->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
690
t2->set_text(1, TranslationServer::get_singleton()->get_locale_name(locale));
691
t2->set_editable(1, true);
692
t2->set_metadata(1, path);
693
t2->set_tooltip_text(1, locale);
694
695
// Display that it has been removed if this is the case.
696
if (!FileAccess::exists(path)) {
697
t2->set_text(0, t2->get_text(0) + vformat(" (%s)", TTR("Removed")));
698
t2->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t2->get_tooltip_text(0)));
699
}
700
}
701
}
702
}
703
}
704
705
// Update translation source files.
706
template_source_list->clear();
707
root = template_source_list->create_item(nullptr);
708
template_source_list->set_hide_root(true);
709
PackedStringArray sources = GLOBAL_GET("internationalization/locale/translations_pot_files");
710
for (int i = 0; i < sources.size(); i++) {
711
TreeItem *t = template_source_list->create_item(root);
712
t->set_editable(0, false);
713
t->set_text(0, sources[i].replace_first("res://", ""));
714
t->set_tooltip_text(0, sources[i]);
715
t->set_metadata(0, i);
716
t->add_button(0, get_editor_theme_icon(SNAME("Remove")), 0, false, TTRC("Remove"));
717
}
718
719
// New translation parser plugin might extend possible file extensions in template generation.
720
_update_template_source_file_extensions();
721
722
updating_translations = false;
723
}
724
725
void LocalizationEditor::_bind_methods() {
726
ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
727
728
ADD_SIGNAL(MethodInfo("localization_changed"));
729
}
730
731
LocalizationEditor::LocalizationEditor() {
732
localization_changed = "localization_changed";
733
734
TabContainer *translations = memnew(TabContainer);
735
translations->set_theme_type_variation("TabContainerInner");
736
translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
737
add_child(translations);
738
739
{
740
VBoxContainer *tvb = memnew(VBoxContainer);
741
tvb->set_name(TTRC("Translations"));
742
translations->add_child(tvb);
743
744
HBoxContainer *thb = memnew(HBoxContainer);
745
Label *l = memnew(Label(TTRC("Translations:")));
746
l->set_theme_type_variation("HeaderSmall");
747
thb->add_child(l);
748
thb->add_spacer();
749
tvb->add_child(thb);
750
751
Button *addtr = memnew(Button(TTRC("Add...")));
752
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_file_open));
753
thb->add_child(addtr);
754
755
MarginContainer *mc = memnew(MarginContainer);
756
mc->set_theme_type_variation("NoBorderHorizontalBottomWide");
757
mc->set_v_size_flags(SIZE_EXPAND_FILL);
758
tvb->add_child(mc);
759
760
translation_list = memnew(Tree);
761
translation_list->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_TOP);
762
mc->add_child(translation_list);
763
trees.push_back(translation_list);
764
tree_data_types[translation_list] = "localization_editor_translation_item";
765
tree_settings[translation_list] = "internationalization/locale/translations";
766
767
locale_select = memnew(EditorLocaleDialog);
768
locale_select->connect("locale_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_selected));
769
add_child(locale_select);
770
771
translation_file_open = memnew(EditorFileDialog);
772
translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
773
translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
774
add_child(translation_file_open);
775
}
776
777
{
778
VBoxContainer *tvb = memnew(VBoxContainer);
779
tvb->set_name(TTRC("Remaps"));
780
translations->add_child(tvb);
781
782
HBoxContainer *thb = memnew(HBoxContainer);
783
Label *l = memnew(Label(TTRC("Resources:")));
784
l->set_theme_type_variation("HeaderSmall");
785
thb->add_child(l);
786
thb->add_spacer();
787
tvb->add_child(thb);
788
789
Button *addtr = memnew(Button(TTRC("Add...")));
790
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_res_file_open));
791
thb->add_child(addtr);
792
793
MarginContainer *mc = memnew(MarginContainer);
794
mc->set_theme_type_variation("NoBorderHorizontalWide");
795
mc->set_v_size_flags(SIZE_EXPAND_FILL);
796
tvb->add_child(mc);
797
798
translation_remap = memnew(Tree);
799
translation_remap->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
800
translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
801
translation_remap->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_delete));
802
mc->add_child(translation_remap);
803
804
translation_res_file_open_dialog = memnew(EditorFileDialog);
805
translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
806
translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
807
add_child(translation_res_file_open_dialog);
808
809
thb = memnew(HBoxContainer);
810
l = memnew(Label(TTRC("Remaps by Locale:")));
811
l->set_theme_type_variation("HeaderSmall");
812
thb->add_child(l);
813
thb->add_spacer();
814
tvb->add_child(thb);
815
816
addtr = memnew(Button(TTRC("Add...")));
817
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
818
translation_res_option_add_button = addtr;
819
thb->add_child(addtr);
820
821
mc = memnew(MarginContainer);
822
mc->set_theme_type_variation("NoBorderHorizontalBottomWide");
823
mc->set_v_size_flags(SIZE_EXPAND_FILL);
824
tvb->add_child(mc);
825
826
translation_remap_options = memnew(Tree);
827
translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
828
translation_remap_options->set_theme_type_variation("TreeTable");
829
translation_remap_options->set_hide_folding(true);
830
translation_remap_options->set_columns(2);
831
translation_remap_options->set_column_title(0, TTRC("Path"));
832
translation_remap_options->set_column_title(1, TTRC("Locale"));
833
translation_remap_options->set_column_titles_visible(true);
834
translation_remap_options->set_column_expand(0, true);
835
translation_remap_options->set_column_clip_content(0, true);
836
translation_remap_options->set_column_expand(1, false);
837
translation_remap_options->set_column_clip_content(1, false);
838
translation_remap_options->set_column_custom_minimum_width(1, 250);
839
translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
840
translation_remap_options->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
841
translation_remap_options->connect("custom_popup_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_popup));
842
mc->add_child(translation_remap_options);
843
844
translation_res_option_file_open_dialog = memnew(EditorFileDialog);
845
translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
846
translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
847
add_child(translation_res_option_file_open_dialog);
848
}
849
850
{
851
VBoxContainer *tvb = memnew(VBoxContainer);
852
tvb->set_name(TTRC("Template Generation"));
853
translations->add_child(tvb);
854
855
HBoxContainer *thb = memnew(HBoxContainer);
856
Label *l = memnew(Label(TTRC("Files with translation strings:")));
857
l->set_theme_type_variation("HeaderSmall");
858
thb->add_child(l);
859
thb->add_spacer();
860
tvb->add_child(thb);
861
862
Button *addtr = memnew(Button(TTRC("Add...")));
863
addtr->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_template_source_file_open));
864
thb->add_child(addtr);
865
866
template_generate_button = memnew(Button(TTRC("Generate")));
867
template_generate_button->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_template_generate_open));
868
thb->add_child(template_generate_button);
869
870
MarginContainer *mc = memnew(MarginContainer);
871
mc->set_theme_type_variation("NoBorderHorizontalWide");
872
mc->set_v_size_flags(SIZE_EXPAND_FILL);
873
tvb->add_child(mc);
874
875
template_source_list = memnew(Tree);
876
template_source_list->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
877
mc->add_child(template_source_list);
878
trees.push_back(template_source_list);
879
tree_data_types[template_source_list] = "localization_editor_pot_item";
880
tree_settings[template_source_list] = "internationalization/locale/translations_pot_files";
881
882
template_add_builtin = memnew(CheckBox(TTRC("Add Built-in Strings")));
883
template_add_builtin->set_tooltip_text(TTRC("Add strings from built-in components such as certain Control nodes."));
884
template_add_builtin->connect(SceneStringName(pressed), callable_mp(this, &LocalizationEditor::_template_add_builtin_toggled));
885
tvb->add_child(template_add_builtin);
886
887
template_generate_dialog = memnew(EditorFileDialog);
888
template_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
889
template_generate_dialog->set_current_path(EditorSettings::get_singleton()->get_project_metadata("pot_generator", "last_pot_path", String()));
890
template_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_template_generate));
891
add_child(template_generate_dialog);
892
893
template_source_open_dialog = memnew(EditorFileDialog);
894
template_source_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
895
template_source_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_template_source_add));
896
add_child(template_source_open_dialog);
897
}
898
899
for (Tree *tree : trees) {
900
SET_DRAG_FORWARDING_GCD(tree, LocalizationEditor);
901
}
902
}
903
904