localization_editor.cpp 30 KB

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