Skip to content
0.2.0-dev Download ZIP
577 lines ·19.7 KB Raw
1/**************************************************************************/
2/* editor_locale_dialog.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_locale_dialog.h"
32
33#include "core/config/project_settings.h"
34#include "core/object/callable_mp.h"
35#include "core/string/translation_server.h"
36#include "editor/editor_undo_redo_manager.h"
37#include "editor/themes/editor_scale.h"
38#include "scene/gui/check_button.h"
39#include "scene/gui/line_edit.h"
40#include "scene/gui/option_button.h"
41#include "scene/gui/tree.h"
42
43void EditorLocaleDialog::_notification(int p_what) {
44 if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {
45 // TRANSLATORS: This is the label for a list of writing systems.
46 script_label1->set_text(TTR("Script:", "Locale"));
47 // TRANSLATORS: This refers to a writing system.
48 script_label2->set_text(TTR("Script", "Locale"));
49
50 script_list->set_accessibility_name(TTR("Script", "Locale"));
51 script_code->set_accessibility_name(TTR("Script", "Locale"));
52 }
53}
54
55void EditorLocaleDialog::_bind_methods() {
56 ADD_SIGNAL(MethodInfo("locale_selected", PropertyInfo(Variant::STRING, "locale")));
57}
58
59void EditorLocaleDialog::ok_pressed() {
60 if (edit_filters->is_pressed()) {
61 return; // Do not update, if in filter edit mode.
62 }
63
64 String locale;
65 if (lang_code->get_text().is_empty()) {
66 return; // Language code is required.
67 }
68 locale = lang_code->get_text();
69
70 if (!script_code->get_text().is_empty()) {
71 locale += "_" + script_code->get_text();
72 }
73 if (!country_code->get_text().is_empty()) {
74 locale += "_" + country_code->get_text();
75 }
76 if (!variant_code->get_text().is_empty()) {
77 locale += "_" + variant_code->get_text();
78 }
79
80 emit_signal(SNAME("locale_selected"), TranslationServer::get_singleton()->standardize_locale(locale));
81 hide();
82}
83
84void EditorLocaleDialog::_item_selected() {
85 if (updating_lists) {
86 return;
87 }
88
89 if (edit_filters->is_pressed()) {
90 return; // Do not update, if in filter edit mode.
91 }
92
93 TreeItem *l = lang_list->get_selected();
94 if (l) {
95 lang_code->set_text(l->get_metadata(0).operator String());
96 }
97
98 TreeItem *s = script_list->get_selected();
99 if (s) {
100 script_code->set_text(s->get_metadata(0).operator String());
101 }
102
103 TreeItem *c = cnt_list->get_selected();
104 if (c) {
105 country_code->set_text(c->get_metadata(0).operator String());
106 }
107}
108
109void EditorLocaleDialog::_toggle_advanced(bool p_checked) {
110 if (!p_checked) {
111 script_code->set_text("");
112 variant_code->set_text("");
113 }
114 _update_tree();
115}
116
117void EditorLocaleDialog::_post_popup() {
118 ConfirmationDialog::_post_popup();
119
120 if (!locale_set) {
121 lang_code->set_text("");
122 script_code->set_text("");
123 country_code->set_text("");
124 variant_code->set_text("");
125 }
126 edit_filters->set_pressed(false);
127 _update_tree();
128}
129
130void EditorLocaleDialog::_filter_lang_option_changed() {
131 TreeItem *t = lang_list->get_edited();
132 String lang = t->get_metadata(0);
133 bool checked = t->is_checked(0);
134
135 Variant prev;
136 Array f_lang_all;
137
138 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/language_filter")) {
139 f_lang_all = GLOBAL_GET("internationalization/locale/language_filter");
140 prev = f_lang_all;
141 }
142
143 int l_idx = f_lang_all.find(lang);
144
145 if (checked) {
146 if (l_idx == -1) {
147 f_lang_all.append(lang);
148 }
149 } else {
150 if (l_idx != -1) {
151 f_lang_all.remove_at(l_idx);
152 }
153 }
154
155 f_lang_all.sort();
156
157 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
158 undo_redo->create_action(TTR("Changed Locale Language Filter"));
159 undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/language_filter", f_lang_all);
160 undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/language_filter", prev);
161 undo_redo->commit_action();
162}
163
164void EditorLocaleDialog::_filter_script_option_changed() {
165 TreeItem *t = script_list->get_edited();
166 String scr_code = t->get_metadata(0);
167 bool checked = t->is_checked(0);
168
169 Variant prev;
170 Array f_script_all;
171
172 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/script_filter")) {
173 f_script_all = GLOBAL_GET("internationalization/locale/script_filter");
174 prev = f_script_all;
175 }
176
177 int l_idx = f_script_all.find(scr_code);
178
179 if (checked) {
180 if (l_idx == -1) {
181 f_script_all.append(scr_code);
182 }
183 } else {
184 if (l_idx != -1) {
185 f_script_all.remove_at(l_idx);
186 }
187 }
188
189 f_script_all.sort();
190
191 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
192 undo_redo->create_action(TTR("Changed Locale Script Filter"));
193 undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/script_filter", f_script_all);
194 undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/script_filter", prev);
195 undo_redo->commit_action();
196}
197
198void EditorLocaleDialog::_filter_cnt_option_changed() {
199 TreeItem *t = cnt_list->get_edited();
200 String cnt = t->get_metadata(0);
201 bool checked = t->is_checked(0);
202
203 Variant prev;
204 Array f_cnt_all;
205
206 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/country_filter")) {
207 f_cnt_all = GLOBAL_GET("internationalization/locale/country_filter");
208 prev = f_cnt_all;
209 }
210
211 int l_idx = f_cnt_all.find(cnt);
212
213 if (checked) {
214 if (l_idx == -1) {
215 f_cnt_all.append(cnt);
216 }
217 } else {
218 if (l_idx != -1) {
219 f_cnt_all.remove_at(l_idx);
220 }
221 }
222
223 f_cnt_all.sort();
224
225 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
226 undo_redo->create_action(TTR("Changed Locale Country Filter"));
227 undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/country_filter", f_cnt_all);
228 undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/country_filter", prev);
229 undo_redo->commit_action();
230}
231
232void EditorLocaleDialog::_filter_mode_changed(int p_mode) {
233 int f_mode = filter_mode->get_selected_id();
234 Variant prev;
235
236 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter_mode")) {
237 prev = GLOBAL_GET("internationalization/locale/locale_filter_mode");
238 }
239
240 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
241 undo_redo->create_action(TTR("Changed Locale Filter Mode"));
242 undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter_mode", f_mode);
243 undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter_mode", prev);
244 undo_redo->commit_action();
245
246 _update_tree();
247}
248
249void EditorLocaleDialog::_edit_filters(bool p_checked) {
250 _update_tree();
251}
252
253void EditorLocaleDialog::_update_tree() {
254 updating_lists = true;
255
256 int filter = SHOW_ALL_LOCALES;
257 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter_mode")) {
258 filter = GLOBAL_GET_CACHED(int, "internationalization/locale/locale_filter_mode");
259 }
260 Array f_lang_all;
261 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/language_filter")) {
262 f_lang_all = GLOBAL_GET("internationalization/locale/language_filter");
263 }
264 Array f_cnt_all;
265 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/country_filter")) {
266 f_cnt_all = GLOBAL_GET("internationalization/locale/country_filter");
267 }
268 Array f_script_all;
269 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/script_filter")) {
270 f_script_all = GLOBAL_GET("internationalization/locale/script_filter");
271 }
272 bool is_edit_mode = edit_filters->is_pressed();
273
274 filter_mode->select(filter);
275
276 // Hide text advanced edit and disable OK button if in filter edit mode.
277 advanced->set_visible(!is_edit_mode);
278 hb_locale->set_visible(!is_edit_mode && advanced->is_pressed());
279 vb_script_list->set_visible(advanced->is_pressed());
280 get_ok_button()->set_disabled(is_edit_mode);
281
282 // Update language list.
283 lang_list->clear();
284 TreeItem *l_root = lang_list->create_item(nullptr);
285 lang_list->set_hide_root(true);
286
287 Vector<String> languages = TranslationServer::get_singleton()->get_all_languages();
288 for (const String &E : languages) {
289 if (is_edit_mode || (filter == SHOW_ALL_LOCALES) || f_lang_all.has(E) || f_lang_all.is_empty()) {
290 const String &lang = TranslationServer::get_singleton()->get_language_name(E);
291 TreeItem *t = lang_list->create_item(l_root);
292 if (is_edit_mode) {
293 t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
294 t->set_editable(0, true);
295 t->set_checked(0, f_lang_all.has(E));
296 } else if (lang_code->get_text() == E) {
297 t->select(0);
298 }
299 t->set_text(0, vformat("%s [%s]", lang, E));
300 t->set_metadata(0, E);
301 }
302 }
303
304 // Update script list.
305 script_list->clear();
306 TreeItem *s_root = script_list->create_item(nullptr);
307 script_list->set_hide_root(true);
308
309 if (!is_edit_mode) {
310 TreeItem *t = script_list->create_item(s_root);
311 t->set_text(0, TTRC("[Default]"));
312 t->set_metadata(0, "");
313 }
314
315 Vector<String> scripts = TranslationServer::get_singleton()->get_all_scripts();
316 for (const String &E : scripts) {
317 if (is_edit_mode || (filter == SHOW_ALL_LOCALES) || f_script_all.has(E) || f_script_all.is_empty()) {
318 const String &scr_code = TranslationServer::get_singleton()->get_script_name(E);
319 TreeItem *t = script_list->create_item(s_root);
320 if (is_edit_mode) {
321 t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
322 t->set_editable(0, true);
323 t->set_checked(0, f_script_all.has(E));
324 } else if (script_code->get_text() == E) {
325 t->select(0);
326 }
327 t->set_text(0, vformat("%s [%s]", scr_code, E));
328 t->set_metadata(0, E);
329 }
330 }
331
332 // Update country list.
333 cnt_list->clear();
334 TreeItem *c_root = cnt_list->create_item(nullptr);
335 cnt_list->set_hide_root(true);
336
337 if (!is_edit_mode) {
338 TreeItem *t = cnt_list->create_item(c_root);
339 t->set_text(0, TTRC("[Default]"));
340 t->set_metadata(0, "");
341 }
342
343 Vector<String> countries = TranslationServer::get_singleton()->get_all_countries();
344 for (const String &E : countries) {
345 if (is_edit_mode || (filter == SHOW_ALL_LOCALES) || f_cnt_all.has(E) || f_cnt_all.is_empty()) {
346 const String &cnt = TranslationServer::get_singleton()->get_country_name(E);
347 TreeItem *t = cnt_list->create_item(c_root);
348 if (is_edit_mode) {
349 t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
350 t->set_editable(0, true);
351 t->set_checked(0, f_cnt_all.has(E));
352 } else if (country_code->get_text() == E) {
353 t->select(0);
354 }
355 t->set_text(0, vformat("%s [%s]", cnt, E));
356 t->set_metadata(0, E);
357 }
358 }
359 updating_lists = false;
360}
361
362void EditorLocaleDialog::set_locale(const String &p_locale) {
363 const String &locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
364 if (locale.is_empty()) {
365 locale_set = false;
366
367 lang_code->set_text("");
368 script_code->set_text("");
369 country_code->set_text("");
370 variant_code->set_text("");
371 } else {
372 locale_set = true;
373
374 Vector<String> locale_elements = p_locale.split("_");
375 lang_code->set_text(locale_elements[0]);
376 if (locale_elements.size() >= 2) {
377 if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) {
378 script_code->set_text(locale_elements[1]);
379 advanced->set_pressed(true);
380 }
381 if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {
382 country_code->set_text(locale_elements[1]);
383 }
384 }
385 if (locale_elements.size() >= 3) {
386 if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {
387 country_code->set_text(locale_elements[2]);
388 } else {
389 variant_code->set_text(locale_elements[2].to_lower());
390 advanced->set_pressed(true);
391 }
392 }
393 if (locale_elements.size() >= 4) {
394 variant_code->set_text(locale_elements[3].to_lower());
395 advanced->set_pressed(true);
396 }
397 }
398}
399
400void EditorLocaleDialog::popup_locale_dialog() {
401 popup_centered_clamped(Size2(1050, 700) * EDSCALE, 0.8);
402}
403
404EditorLocaleDialog::EditorLocaleDialog() {
405 set_title(TTRC("Select a Locale"));
406
407 VBoxContainer *vb = memnew(VBoxContainer);
408 {
409 HBoxContainer *hb_filter = memnew(HBoxContainer);
410 {
411 filter_mode = memnew(OptionButton);
412 filter_mode->set_accessibility_name(TTRC("Locale Filter"));
413 filter_mode->add_item(TTRC("Show All Locales"), SHOW_ALL_LOCALES);
414 filter_mode->set_h_size_flags(Control::SIZE_EXPAND_FILL);
415 filter_mode->add_item(TTRC("Show Selected Locales Only"), SHOW_ONLY_SELECTED_LOCALES);
416 filter_mode->select(0);
417 filter_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorLocaleDialog::_filter_mode_changed));
418 hb_filter->add_child(filter_mode);
419 }
420 {
421 edit_filters = memnew(CheckButton);
422 edit_filters->set_text(TTRC("Edit Filters"));
423 edit_filters->set_toggle_mode(true);
424 edit_filters->set_pressed(false);
425 edit_filters->connect(SceneStringName(toggled), callable_mp(this, &EditorLocaleDialog::_edit_filters));
426 hb_filter->add_child(edit_filters);
427 }
428 {
429 advanced = memnew(CheckButton);
430 advanced->set_text(TTRC("Advanced"));
431 advanced->set_toggle_mode(true);
432 advanced->set_pressed(false);
433 advanced->connect(SceneStringName(toggled), callable_mp(this, &EditorLocaleDialog::_toggle_advanced));
434 hb_filter->add_child(advanced);
435 }
436 vb->add_child(hb_filter);
437 }
438 {
439 HBoxContainer *hb_lists = memnew(HBoxContainer);
440 hb_lists->set_v_size_flags(Control::SIZE_EXPAND_FILL);
441 {
442 VBoxContainer *vb_lang_list = memnew(VBoxContainer);
443 vb_lang_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
444 {
445 Label *lang_lbl = memnew(Label);
446 lang_lbl->set_text(TTRC("Language:"));
447 vb_lang_list->add_child(lang_lbl);
448 }
449 {
450 lang_list = memnew(Tree);
451 lang_list->set_accessibility_name(TTRC("Language:"));
452 lang_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
453 lang_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
454 lang_list->connect("cell_selected", callable_mp(this, &EditorLocaleDialog::_item_selected));
455 lang_list->set_columns(1);
456 lang_list->connect("item_edited", callable_mp(this, &EditorLocaleDialog::_filter_lang_option_changed));
457 vb_lang_list->add_child(lang_list);
458 }
459 hb_lists->add_child(vb_lang_list);
460 }
461 {
462 vb_script_list = memnew(VBoxContainer);
463 vb_script_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
464 {
465 script_label1 = memnew(Label);
466 script_label1->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
467 vb_script_list->add_child(script_label1);
468 }
469 {
470 script_list = memnew(Tree);
471 script_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
472 script_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
473 script_list->connect("cell_selected", callable_mp(this, &EditorLocaleDialog::_item_selected));
474 script_list->set_columns(1);
475 script_list->connect("item_edited", callable_mp(this, &EditorLocaleDialog::_filter_script_option_changed));
476 vb_script_list->add_child(script_list);
477 }
478 hb_lists->add_child(vb_script_list);
479 }
480 {
481 VBoxContainer *vb_cnt_list = memnew(VBoxContainer);
482 vb_cnt_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
483 {
484 Label *cnt_lbl = memnew(Label);
485 cnt_lbl->set_text(TTRC("Country:"));
486 vb_cnt_list->add_child(cnt_lbl);
487 }
488 {
489 cnt_list = memnew(Tree);
490 cnt_list->set_accessibility_name(TTRC("Country:"));
491 cnt_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
492 cnt_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
493 cnt_list->connect("cell_selected", callable_mp(this, &EditorLocaleDialog::_item_selected));
494 cnt_list->set_columns(1);
495 cnt_list->connect("item_edited", callable_mp(this, &EditorLocaleDialog::_filter_cnt_option_changed));
496 vb_cnt_list->add_child(cnt_list);
497 }
498 hb_lists->add_child(vb_cnt_list);
499 }
500 vb->add_child(hb_lists);
501 }
502 {
503 hb_locale = memnew(HBoxContainer);
504 hb_locale->set_h_size_flags(Control::SIZE_EXPAND_FILL);
505 {
506 {
507 VBoxContainer *vb_language = memnew(VBoxContainer);
508 vb_language->set_h_size_flags(Control::SIZE_EXPAND_FILL);
509 {
510 Label *language_lbl = memnew(Label);
511 language_lbl->set_text(TTRC("Language"));
512 vb_language->add_child(language_lbl);
513 }
514 {
515 lang_code = memnew(LineEdit);
516 lang_code->set_max_length(3);
517 lang_code->set_accessibility_name("Language");
518 vb_language->add_child(lang_code);
519 }
520 hb_locale->add_child(vb_language);
521 }
522 {
523 VBoxContainer *vb_script = memnew(VBoxContainer);
524 vb_script->set_h_size_flags(Control::SIZE_EXPAND_FILL);
525 {
526 script_label2 = memnew(Label);
527 script_label2->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
528 vb_script->add_child(script_label2);
529 }
530 {
531 script_code = memnew(LineEdit);
532 script_code->set_max_length(4);
533 vb_script->add_child(script_code);
534 }
535 hb_locale->add_child(vb_script);
536 }
537 {
538 VBoxContainer *vb_country = memnew(VBoxContainer);
539 vb_country->set_h_size_flags(Control::SIZE_EXPAND_FILL);
540 {
541 Label *country_lbl = memnew(Label);
542 country_lbl->set_text(TTRC("Country"));
543 vb_country->add_child(country_lbl);
544 }
545 {
546 country_code = memnew(LineEdit);
547 country_code->set_max_length(2);
548 country_code->set_tooltip_text("Country");
549 vb_country->add_child(country_code);
550 }
551 hb_locale->add_child(vb_country);
552 }
553 {
554 VBoxContainer *vb_variant = memnew(VBoxContainer);
555 vb_variant->set_h_size_flags(Control::SIZE_EXPAND_FILL);
556 {
557 Label *variant_lbl = memnew(Label);
558 variant_lbl->set_text(TTRC("Variant"));
559 vb_variant->add_child(variant_lbl);
560 }
561 {
562 variant_code = memnew(LineEdit);
563 variant_code->set_h_size_flags(Control::SIZE_EXPAND_FILL);
564 variant_code->set_placeholder("Variant");
565 variant_code->set_accessibility_name("Variant");
566 vb_variant->add_child(variant_code);
567 }
568 hb_locale->add_child(vb_variant);
569 }
570 }
571 vb->add_child(hb_locale);
572 }
573 add_child(vb);
574 _update_tree();
575
576 set_ok_button_text(TTRC("Select"));
577}