localization_editor.cpp 31 KB

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