localization_editor.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*************************************************************************/
  2. /* localization_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "localization_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/string/translation.h"
  33. #include "editor/editor_file_dialog.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_translation_parser.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/filesystem_dock.h"
  39. #include "editor/pot_generator.h"
  40. #include "scene/gui/control.h"
  41. void LocalizationEditor::_notification(int p_what) {
  42. switch (p_what) {
  43. case NOTIFICATION_ENTER_TREE: {
  44. translation_list->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_delete));
  45. translation_pot_list->connect("button_clicked", callable_mp(this, &LocalizationEditor::_pot_delete));
  46. List<String> tfn;
  47. ResourceLoader::get_recognized_extensions_for_type("Translation", &tfn);
  48. for (const String &E : tfn) {
  49. translation_file_open->add_filter("*." + E);
  50. }
  51. List<String> rfn;
  52. ResourceLoader::get_recognized_extensions_for_type("Resource", &rfn);
  53. for (const String &E : rfn) {
  54. translation_res_file_open_dialog->add_filter("*." + E);
  55. translation_res_option_file_open_dialog->add_filter("*." + E);
  56. }
  57. _update_pot_file_extensions();
  58. pot_generate_dialog->add_filter("*.pot");
  59. } break;
  60. }
  61. }
  62. void LocalizationEditor::add_translation(const String &p_translation) {
  63. PackedStringArray translations;
  64. translations.push_back(p_translation);
  65. _translation_add(translations);
  66. }
  67. void LocalizationEditor::_translation_add(const PackedStringArray &p_paths) {
  68. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  69. for (int i = 0; i < p_paths.size(); i++) {
  70. if (!translations.has(p_paths[i])) {
  71. // Don't add duplicate translation paths.
  72. translations.push_back(p_paths[i]);
  73. }
  74. }
  75. undo_redo->create_action(vformat(TTR("Add %d Translations"), p_paths.size()));
  76. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  77. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  78. undo_redo->add_do_method(this, "update_translations");
  79. undo_redo->add_undo_method(this, "update_translations");
  80. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  81. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  82. undo_redo->commit_action();
  83. }
  84. void LocalizationEditor::_translation_file_open() {
  85. translation_file_open->popup_file_dialog();
  86. }
  87. void LocalizationEditor::_translation_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
  88. if (p_mouse_button != MouseButton::LEFT) {
  89. return;
  90. }
  91. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  92. ERR_FAIL_COND(!ti);
  93. int idx = ti->get_metadata(0);
  94. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  95. ERR_FAIL_INDEX(idx, translations.size());
  96. translations.remove_at(idx);
  97. undo_redo->create_action(TTR("Remove Translation"));
  98. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  99. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  100. undo_redo->add_do_method(this, "update_translations");
  101. undo_redo->add_undo_method(this, "update_translations");
  102. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  103. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  104. undo_redo->commit_action();
  105. }
  106. void LocalizationEditor::_translation_res_file_open() {
  107. translation_res_file_open_dialog->popup_file_dialog();
  108. }
  109. void LocalizationEditor::_translation_res_add(const PackedStringArray &p_paths) {
  110. Variant prev;
  111. Dictionary remaps;
  112. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  113. remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  114. prev = remaps;
  115. }
  116. for (int i = 0; i < p_paths.size(); i++) {
  117. if (!remaps.has(p_paths[i])) {
  118. // Don't overwrite with an empty remap array if an array already exists for the given path.
  119. remaps[p_paths[i]] = PackedStringArray();
  120. }
  121. }
  122. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Path(s)"), p_paths.size()));
  123. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  124. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", prev);
  125. undo_redo->add_do_method(this, "update_translations");
  126. undo_redo->add_undo_method(this, "update_translations");
  127. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  128. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  129. undo_redo->commit_action();
  130. }
  131. void LocalizationEditor::_translation_res_option_file_open() {
  132. translation_res_option_file_open_dialog->popup_file_dialog();
  133. }
  134. void LocalizationEditor::_translation_res_option_add(const PackedStringArray &p_paths) {
  135. ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps"));
  136. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  137. TreeItem *k = translation_remap->get_selected();
  138. ERR_FAIL_COND(!k);
  139. String key = k->get_metadata(0);
  140. ERR_FAIL_COND(!remaps.has(key));
  141. PackedStringArray r = remaps[key];
  142. for (int i = 0; i < p_paths.size(); i++) {
  143. r.push_back(p_paths[i] + ":" + "en");
  144. }
  145. remaps[key] = r;
  146. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Remap(s)"), p_paths.size()));
  147. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  148. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  149. undo_redo->add_do_method(this, "update_translations");
  150. undo_redo->add_undo_method(this, "update_translations");
  151. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  152. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  153. undo_redo->commit_action();
  154. }
  155. void LocalizationEditor::_translation_res_select() {
  156. if (updating_translations) {
  157. return;
  158. }
  159. call_deferred(SNAME("update_translations"));
  160. }
  161. void LocalizationEditor::_translation_res_option_popup(bool p_arrow_clicked) {
  162. TreeItem *ed = translation_remap_options->get_edited();
  163. ERR_FAIL_COND(!ed);
  164. locale_select->set_locale(ed->get_tooltip_text(1));
  165. locale_select->popup_locale_dialog();
  166. }
  167. void LocalizationEditor::_translation_res_option_selected(const String &p_locale) {
  168. TreeItem *ed = translation_remap_options->get_edited();
  169. ERR_FAIL_COND(!ed);
  170. ed->set_text(1, TranslationServer::get_singleton()->get_locale_name(p_locale));
  171. ed->set_tooltip_text(1, p_locale);
  172. LocalizationEditor::_translation_res_option_changed();
  173. }
  174. void LocalizationEditor::_translation_res_option_changed() {
  175. if (updating_translations) {
  176. return;
  177. }
  178. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  179. return;
  180. }
  181. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  182. TreeItem *k = translation_remap->get_selected();
  183. ERR_FAIL_COND(!k);
  184. TreeItem *ed = translation_remap_options->get_edited();
  185. ERR_FAIL_COND(!ed);
  186. String key = k->get_metadata(0);
  187. int idx = ed->get_metadata(0);
  188. String path = ed->get_metadata(1);
  189. String locale = ed->get_tooltip_text(1);
  190. ERR_FAIL_COND(!remaps.has(key));
  191. PackedStringArray r = remaps[key];
  192. r.set(idx, path + ":" + locale);
  193. remaps[key] = r;
  194. updating_translations = true;
  195. undo_redo->create_action(TTR("Change Resource Remap Language"));
  196. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  197. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  198. undo_redo->add_do_method(this, "update_translations");
  199. undo_redo->add_undo_method(this, "update_translations");
  200. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  201. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  202. undo_redo->commit_action();
  203. updating_translations = false;
  204. }
  205. void LocalizationEditor::_translation_res_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
  206. if (updating_translations) {
  207. return;
  208. }
  209. if (p_mouse_button != MouseButton::LEFT) {
  210. return;
  211. }
  212. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  213. return;
  214. }
  215. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  216. TreeItem *k = Object::cast_to<TreeItem>(p_item);
  217. String key = k->get_metadata(0);
  218. ERR_FAIL_COND(!remaps.has(key));
  219. remaps.erase(key);
  220. undo_redo->create_action(TTR("Remove Resource Remap"));
  221. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  222. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  223. undo_redo->add_do_method(this, "update_translations");
  224. undo_redo->add_undo_method(this, "update_translations");
  225. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  226. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  227. undo_redo->commit_action();
  228. }
  229. void LocalizationEditor::_translation_res_option_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
  230. if (updating_translations) {
  231. return;
  232. }
  233. if (p_mouse_button != MouseButton::LEFT) {
  234. return;
  235. }
  236. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  237. return;
  238. }
  239. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  240. TreeItem *k = translation_remap->get_selected();
  241. ERR_FAIL_COND(!k);
  242. TreeItem *ed = Object::cast_to<TreeItem>(p_item);
  243. ERR_FAIL_COND(!ed);
  244. String key = k->get_metadata(0);
  245. int idx = ed->get_metadata(0);
  246. ERR_FAIL_COND(!remaps.has(key));
  247. PackedStringArray r = remaps[key];
  248. ERR_FAIL_INDEX(idx, r.size());
  249. r.remove_at(idx);
  250. remaps[key] = r;
  251. undo_redo->create_action(TTR("Remove Resource Remap Option"));
  252. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  253. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  254. undo_redo->add_do_method(this, "update_translations");
  255. undo_redo->add_undo_method(this, "update_translations");
  256. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  257. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  258. undo_redo->commit_action();
  259. }
  260. void LocalizationEditor::_pot_add(const PackedStringArray &p_paths) {
  261. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  262. for (int i = 0; i < p_paths.size(); i++) {
  263. if (!pot_translations.has(p_paths[i])) {
  264. pot_translations.push_back(p_paths[i]);
  265. }
  266. }
  267. undo_redo->create_action(vformat(TTR("Add %d file(s) for POT generation"), p_paths.size()));
  268. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  269. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  270. undo_redo->add_do_method(this, "update_translations");
  271. undo_redo->add_undo_method(this, "update_translations");
  272. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  273. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  274. undo_redo->commit_action();
  275. }
  276. void LocalizationEditor::_pot_delete(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
  277. if (p_mouse_button != MouseButton::LEFT) {
  278. return;
  279. }
  280. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  281. ERR_FAIL_COND(!ti);
  282. int idx = ti->get_metadata(0);
  283. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  284. ERR_FAIL_INDEX(idx, pot_translations.size());
  285. pot_translations.remove_at(idx);
  286. undo_redo->create_action(TTR("Remove file from POT generation"));
  287. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  288. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  289. undo_redo->add_do_method(this, "update_translations");
  290. undo_redo->add_undo_method(this, "update_translations");
  291. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  292. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  293. undo_redo->commit_action();
  294. }
  295. void LocalizationEditor::_pot_file_open() {
  296. pot_file_open_dialog->popup_file_dialog();
  297. }
  298. void LocalizationEditor::_pot_generate_open() {
  299. pot_generate_dialog->popup_file_dialog();
  300. }
  301. void LocalizationEditor::_pot_generate(const String &p_file) {
  302. POTGenerator::get_singleton()->generate_pot(p_file);
  303. }
  304. void LocalizationEditor::_update_pot_file_extensions() {
  305. pot_file_open_dialog->clear_filters();
  306. List<String> translation_parse_file_extensions;
  307. EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
  308. for (const String &E : translation_parse_file_extensions) {
  309. pot_file_open_dialog->add_filter("*." + E);
  310. }
  311. }
  312. void LocalizationEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
  313. p_fs_dock->connect("files_moved", callable_mp(this, &LocalizationEditor::_filesystem_files_moved));
  314. p_fs_dock->connect("file_removed", callable_mp(this, &LocalizationEditor::_filesystem_file_removed));
  315. }
  316. void LocalizationEditor::_filesystem_files_moved(const String &p_old_file, const String &p_new_file) {
  317. // Update remaps if the moved file is a part of them.
  318. Dictionary remaps;
  319. bool remaps_changed = false;
  320. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  321. remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  322. }
  323. // Check for the keys.
  324. if (remaps.has(p_old_file)) {
  325. PackedStringArray remapped_files = remaps[p_old_file];
  326. remaps.erase(p_old_file);
  327. remaps[p_new_file] = remapped_files;
  328. remaps_changed = true;
  329. print_verbose(vformat("Changed remap key \"%s\" to \"%s\" due to a moved file.", p_old_file, p_new_file));
  330. }
  331. // Check for the Array elements of the values.
  332. Array remap_keys = remaps.keys();
  333. for (int i = 0; i < remap_keys.size(); i++) {
  334. PackedStringArray remapped_files = remaps[remap_keys[i]];
  335. bool remapped_files_updated = false;
  336. for (int j = 0; j < remapped_files.size(); j++) {
  337. int splitter_pos = remapped_files[j].rfind(":");
  338. String res_path = remapped_files[j].substr(0, splitter_pos);
  339. if (res_path == p_old_file) {
  340. String locale_name = remapped_files[j].substr(splitter_pos + 1);
  341. // Replace the element at that index.
  342. remapped_files.insert(j, p_new_file + ":" + locale_name);
  343. remapped_files.remove_at(j + 1);
  344. remaps_changed = true;
  345. remapped_files_updated = true;
  346. 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_keys[i]));
  347. }
  348. }
  349. if (remapped_files_updated) {
  350. remaps[remap_keys[i]] = remapped_files;
  351. }
  352. }
  353. if (remaps_changed) {
  354. ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_remaps", remaps);
  355. update_translations();
  356. emit_signal("localization_changed");
  357. }
  358. }
  359. void LocalizationEditor::_filesystem_file_removed(const String &p_file) {
  360. // Check if the remaps are affected.
  361. Dictionary remaps;
  362. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  363. remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  364. }
  365. bool remaps_changed = remaps.has(p_file);
  366. if (!remaps_changed) {
  367. Array remap_keys = remaps.keys();
  368. for (int i = 0; i < remap_keys.size() && !remaps_changed; i++) {
  369. PackedStringArray remapped_files = remaps[remap_keys[i]];
  370. for (int j = 0; j < remapped_files.size() && !remaps_changed; j++) {
  371. int splitter_pos = remapped_files[j].rfind(":");
  372. String res_path = remapped_files[j].substr(0, splitter_pos);
  373. remaps_changed = p_file == res_path;
  374. if (remaps_changed) {
  375. print_verbose(vformat("Remap value \"%s\" of key \"%s\" has been removed from the file system.", remapped_files[j], remap_keys[i]));
  376. }
  377. }
  378. }
  379. } else {
  380. print_verbose(vformat("Remap key \"%s\" has been removed from the file system.", p_file));
  381. }
  382. if (remaps_changed) {
  383. update_translations();
  384. emit_signal("localization_changed");
  385. }
  386. }
  387. void LocalizationEditor::update_translations() {
  388. if (updating_translations) {
  389. return;
  390. }
  391. updating_translations = true;
  392. translation_list->clear();
  393. TreeItem *root = translation_list->create_item(nullptr);
  394. translation_list->set_hide_root(true);
  395. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
  396. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  397. for (int i = 0; i < translations.size(); i++) {
  398. TreeItem *t = translation_list->create_item(root);
  399. t->set_editable(0, false);
  400. t->set_text(0, translations[i].replace_first("res://", ""));
  401. t->set_tooltip_text(0, translations[i]);
  402. t->set_metadata(0, i);
  403. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  404. }
  405. }
  406. // Update translation remaps.
  407. String remap_selected;
  408. if (translation_remap->get_selected()) {
  409. remap_selected = translation_remap->get_selected()->get_metadata(0);
  410. }
  411. translation_remap->clear();
  412. translation_remap_options->clear();
  413. root = translation_remap->create_item(nullptr);
  414. TreeItem *root2 = translation_remap_options->create_item(nullptr);
  415. translation_remap->set_hide_root(true);
  416. translation_remap_options->set_hide_root(true);
  417. translation_res_option_add_button->set_disabled(true);
  418. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  419. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  420. List<Variant> rk;
  421. remaps.get_key_list(&rk);
  422. Vector<String> keys;
  423. for (const Variant &E : rk) {
  424. keys.push_back(E);
  425. }
  426. keys.sort();
  427. for (int i = 0; i < keys.size(); i++) {
  428. TreeItem *t = translation_remap->create_item(root);
  429. t->set_editable(0, false);
  430. t->set_text(0, keys[i].replace_first("res://", ""));
  431. t->set_tooltip_text(0, keys[i]);
  432. t->set_metadata(0, keys[i]);
  433. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  434. // Display that it has been removed if this is the case.
  435. if (!FileAccess::exists(keys[i])) {
  436. t->set_text(0, t->get_text(0) + vformat(" (%s)", TTR("Removed")));
  437. t->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t->get_tooltip_text(0)));
  438. }
  439. if (keys[i] == remap_selected) {
  440. t->select(0);
  441. translation_res_option_add_button->set_disabled(false);
  442. PackedStringArray selected = remaps[keys[i]];
  443. for (int j = 0; j < selected.size(); j++) {
  444. String s2 = selected[j];
  445. int qp = s2.rfind(":");
  446. String path = s2.substr(0, qp);
  447. String locale = s2.substr(qp + 1, s2.length());
  448. TreeItem *t2 = translation_remap_options->create_item(root2);
  449. t2->set_editable(0, false);
  450. t2->set_text(0, path.replace_first("res://", ""));
  451. t2->set_tooltip_text(0, path);
  452. t2->set_metadata(0, j);
  453. t2->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  454. t2->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  455. t2->set_text(1, TranslationServer::get_singleton()->get_locale_name(locale));
  456. t2->set_editable(1, true);
  457. t2->set_metadata(1, path);
  458. t2->set_tooltip_text(1, locale);
  459. // Display that it has been removed if this is the case.
  460. if (!FileAccess::exists(path)) {
  461. t2->set_text(0, t2->get_text(0) + vformat(" (%s)", TTR("Removed")));
  462. t2->set_tooltip_text(0, vformat(TTR("%s cannot be found."), t2->get_tooltip_text(0)));
  463. }
  464. }
  465. }
  466. }
  467. }
  468. // Update translation POT files.
  469. translation_pot_list->clear();
  470. root = translation_pot_list->create_item(nullptr);
  471. translation_pot_list->set_hide_root(true);
  472. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations_pot_files")) {
  473. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  474. for (int i = 0; i < pot_translations.size(); i++) {
  475. TreeItem *t = translation_pot_list->create_item(root);
  476. t->set_editable(0, false);
  477. t->set_text(0, pot_translations[i].replace_first("res://", ""));
  478. t->set_tooltip_text(0, pot_translations[i]);
  479. t->set_metadata(0, i);
  480. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  481. }
  482. }
  483. // New translation parser plugin might extend possible file extensions in POT generation.
  484. _update_pot_file_extensions();
  485. updating_translations = false;
  486. }
  487. void LocalizationEditor::_bind_methods() {
  488. ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
  489. ADD_SIGNAL(MethodInfo("localization_changed"));
  490. }
  491. LocalizationEditor::LocalizationEditor() {
  492. undo_redo = EditorNode::get_undo_redo();
  493. localization_changed = "localization_changed";
  494. TabContainer *translations = memnew(TabContainer);
  495. translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  496. add_child(translations);
  497. {
  498. VBoxContainer *tvb = memnew(VBoxContainer);
  499. tvb->set_name(TTR("Translations"));
  500. translations->add_child(tvb);
  501. HBoxContainer *thb = memnew(HBoxContainer);
  502. Label *l = memnew(Label(TTR("Translations:")));
  503. l->set_theme_type_variation("HeaderSmall");
  504. thb->add_child(l);
  505. thb->add_spacer();
  506. tvb->add_child(thb);
  507. Button *addtr = memnew(Button(TTR("Add...")));
  508. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_file_open));
  509. thb->add_child(addtr);
  510. VBoxContainer *tmc = memnew(VBoxContainer);
  511. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  512. tvb->add_child(tmc);
  513. translation_list = memnew(Tree);
  514. translation_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  515. tmc->add_child(translation_list);
  516. locale_select = memnew(EditorLocaleDialog);
  517. locale_select->connect("locale_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_selected));
  518. add_child(locale_select);
  519. translation_file_open = memnew(EditorFileDialog);
  520. translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  521. translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
  522. add_child(translation_file_open);
  523. }
  524. {
  525. VBoxContainer *tvb = memnew(VBoxContainer);
  526. tvb->set_name(TTR("Remaps"));
  527. translations->add_child(tvb);
  528. HBoxContainer *thb = memnew(HBoxContainer);
  529. Label *l = memnew(Label(TTR("Resources:")));
  530. l->set_theme_type_variation("HeaderSmall");
  531. thb->add_child(l);
  532. thb->add_spacer();
  533. tvb->add_child(thb);
  534. Button *addtr = memnew(Button(TTR("Add...")));
  535. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_file_open));
  536. thb->add_child(addtr);
  537. VBoxContainer *tmc = memnew(VBoxContainer);
  538. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  539. tvb->add_child(tmc);
  540. translation_remap = memnew(Tree);
  541. translation_remap->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  542. translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
  543. translation_remap->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_delete));
  544. tmc->add_child(translation_remap);
  545. translation_res_file_open_dialog = memnew(EditorFileDialog);
  546. translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  547. translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
  548. add_child(translation_res_file_open_dialog);
  549. thb = memnew(HBoxContainer);
  550. l = memnew(Label(TTR("Remaps by Locale:")));
  551. l->set_theme_type_variation("HeaderSmall");
  552. thb->add_child(l);
  553. thb->add_spacer();
  554. tvb->add_child(thb);
  555. addtr = memnew(Button(TTR("Add...")));
  556. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
  557. translation_res_option_add_button = addtr;
  558. thb->add_child(addtr);
  559. tmc = memnew(VBoxContainer);
  560. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  561. tvb->add_child(tmc);
  562. translation_remap_options = memnew(Tree);
  563. translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  564. translation_remap_options->set_columns(2);
  565. translation_remap_options->set_column_title(0, TTR("Path"));
  566. translation_remap_options->set_column_title(1, TTR("Locale"));
  567. translation_remap_options->set_column_titles_visible(true);
  568. translation_remap_options->set_column_expand(0, true);
  569. translation_remap_options->set_column_clip_content(0, true);
  570. translation_remap_options->set_column_expand(1, false);
  571. translation_remap_options->set_column_clip_content(1, false);
  572. translation_remap_options->set_column_custom_minimum_width(1, 250);
  573. translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
  574. translation_remap_options->connect("button_clicked", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
  575. translation_remap_options->connect("custom_popup_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_popup));
  576. tmc->add_child(translation_remap_options);
  577. translation_res_option_file_open_dialog = memnew(EditorFileDialog);
  578. translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  579. translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
  580. add_child(translation_res_option_file_open_dialog);
  581. }
  582. {
  583. VBoxContainer *tvb = memnew(VBoxContainer);
  584. tvb->set_name(TTR("POT Generation"));
  585. translations->add_child(tvb);
  586. HBoxContainer *thb = memnew(HBoxContainer);
  587. Label *l = memnew(Label(TTR("Files with translation strings:")));
  588. l->set_theme_type_variation("HeaderSmall");
  589. thb->add_child(l);
  590. thb->add_spacer();
  591. tvb->add_child(thb);
  592. Button *addtr = memnew(Button(TTR("Add...")));
  593. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_file_open));
  594. thb->add_child(addtr);
  595. Button *generate = memnew(Button(TTR("Generate POT")));
  596. generate->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_generate_open));
  597. thb->add_child(generate);
  598. VBoxContainer *tmc = memnew(VBoxContainer);
  599. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  600. tvb->add_child(tmc);
  601. translation_pot_list = memnew(Tree);
  602. translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  603. tmc->add_child(translation_pot_list);
  604. pot_generate_dialog = memnew(EditorFileDialog);
  605. pot_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  606. pot_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_pot_generate));
  607. add_child(pot_generate_dialog);
  608. pot_file_open_dialog = memnew(EditorFileDialog);
  609. pot_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  610. pot_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_pot_add));
  611. add_child(pot_file_open_dialog);
  612. }
  613. }