localization_editor.cpp 31 KB

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