localization_editor.cpp 31 KB

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