localization_editor.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*************************************************************************/
  2. /* localization_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "localization_editor.h"
  31. #include "core/string/translation.h"
  32. #include "editor_node.h"
  33. #include "editor_translation_parser.h"
  34. #include "pot_generator.h"
  35. #include "scene/gui/control.h"
  36. void LocalizationEditor::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_ENTER_TREE) {
  38. translation_list->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_delete));
  39. translation_pot_list->connect("button_pressed", callable_mp(this, &LocalizationEditor::_pot_delete));
  40. List<String> tfn;
  41. ResourceLoader::get_recognized_extensions_for_type("Translation", &tfn);
  42. for (const String &E : tfn) {
  43. translation_file_open->add_filter("*." + E);
  44. }
  45. List<String> rfn;
  46. ResourceLoader::get_recognized_extensions_for_type("Resource", &rfn);
  47. for (const String &E : rfn) {
  48. translation_res_file_open_dialog->add_filter("*." + E);
  49. translation_res_option_file_open_dialog->add_filter("*." + E);
  50. }
  51. _update_pot_file_extensions();
  52. pot_generate_dialog->add_filter("*.pot");
  53. }
  54. }
  55. void LocalizationEditor::add_translation(const String &p_translation) {
  56. PackedStringArray translations;
  57. translations.push_back(p_translation);
  58. _translation_add(translations);
  59. }
  60. void LocalizationEditor::_translation_add(const PackedStringArray &p_paths) {
  61. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  62. for (int i = 0; i < p_paths.size(); i++) {
  63. if (!translations.has(p_paths[i])) {
  64. // Don't add duplicate translation paths.
  65. translations.push_back(p_paths[i]);
  66. }
  67. }
  68. undo_redo->create_action(vformat(TTR("Add %d Translations"), p_paths.size()));
  69. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  70. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  71. undo_redo->add_do_method(this, "update_translations");
  72. undo_redo->add_undo_method(this, "update_translations");
  73. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  74. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  75. undo_redo->commit_action();
  76. }
  77. void LocalizationEditor::_translation_file_open() {
  78. translation_file_open->popup_file_dialog();
  79. }
  80. void LocalizationEditor::_translation_delete(Object *p_item, int p_column, int p_button) {
  81. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  82. ERR_FAIL_COND(!ti);
  83. int idx = ti->get_metadata(0);
  84. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  85. ERR_FAIL_INDEX(idx, translations.size());
  86. translations.remove(idx);
  87. undo_redo->create_action(TTR("Remove Translation"));
  88. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  89. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  90. undo_redo->add_do_method(this, "update_translations");
  91. undo_redo->add_undo_method(this, "update_translations");
  92. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  93. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  94. undo_redo->commit_action();
  95. }
  96. void LocalizationEditor::_translation_res_file_open() {
  97. translation_res_file_open_dialog->popup_file_dialog();
  98. }
  99. void LocalizationEditor::_translation_res_add(const PackedStringArray &p_paths) {
  100. Variant prev;
  101. Dictionary remaps;
  102. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  103. remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  104. prev = remaps;
  105. }
  106. for (int i = 0; i < p_paths.size(); i++) {
  107. if (!remaps.has(p_paths[i])) {
  108. // Don't overwrite with an empty remap array if an array already exists for the given path.
  109. remaps[p_paths[i]] = PackedStringArray();
  110. }
  111. }
  112. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Path(s)"), p_paths.size()));
  113. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  114. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", prev);
  115. undo_redo->add_do_method(this, "update_translations");
  116. undo_redo->add_undo_method(this, "update_translations");
  117. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  118. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  119. undo_redo->commit_action();
  120. }
  121. void LocalizationEditor::_translation_res_option_file_open() {
  122. translation_res_option_file_open_dialog->popup_file_dialog();
  123. }
  124. void LocalizationEditor::_translation_res_option_add(const PackedStringArray &p_paths) {
  125. ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps"));
  126. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  127. TreeItem *k = translation_remap->get_selected();
  128. ERR_FAIL_COND(!k);
  129. String key = k->get_metadata(0);
  130. ERR_FAIL_COND(!remaps.has(key));
  131. PackedStringArray r = remaps[key];
  132. for (int i = 0; i < p_paths.size(); i++) {
  133. r.push_back(p_paths[i] + ":" + "en");
  134. }
  135. remaps[key] = r;
  136. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Remap(s)"), p_paths.size()));
  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", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  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_select() {
  146. if (updating_translations) {
  147. return;
  148. }
  149. call_deferred(SNAME("update_translations"));
  150. }
  151. void LocalizationEditor::_translation_res_option_changed() {
  152. if (updating_translations) {
  153. return;
  154. }
  155. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  156. return;
  157. }
  158. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  159. TreeItem *k = translation_remap->get_selected();
  160. ERR_FAIL_COND(!k);
  161. TreeItem *ed = translation_remap_options->get_edited();
  162. ERR_FAIL_COND(!ed);
  163. String key = k->get_metadata(0);
  164. int idx = ed->get_metadata(0);
  165. String path = ed->get_metadata(1);
  166. int which = ed->get_range(1);
  167. Vector<String> langs = TranslationServer::get_all_locales();
  168. ERR_FAIL_INDEX(which, langs.size());
  169. ERR_FAIL_COND(!remaps.has(key));
  170. PackedStringArray r = remaps[key];
  171. ERR_FAIL_INDEX(idx, r.size());
  172. if (translation_locales_idxs_remap.size() > which) {
  173. r.set(idx, path + ":" + langs[translation_locales_idxs_remap[which]]);
  174. } else {
  175. r.set(idx, path + ":" + langs[which]);
  176. }
  177. remaps[key] = r;
  178. updating_translations = true;
  179. undo_redo->create_action(TTR("Change Resource Remap Language"));
  180. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  181. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  182. undo_redo->add_do_method(this, "update_translations");
  183. undo_redo->add_undo_method(this, "update_translations");
  184. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  185. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  186. undo_redo->commit_action();
  187. updating_translations = false;
  188. }
  189. void LocalizationEditor::_translation_res_delete(Object *p_item, int p_column, int p_button) {
  190. if (updating_translations) {
  191. return;
  192. }
  193. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  194. return;
  195. }
  196. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  197. TreeItem *k = Object::cast_to<TreeItem>(p_item);
  198. String key = k->get_metadata(0);
  199. ERR_FAIL_COND(!remaps.has(key));
  200. remaps.erase(key);
  201. undo_redo->create_action(TTR("Remove Resource Remap"));
  202. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  203. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  204. undo_redo->add_do_method(this, "update_translations");
  205. undo_redo->add_undo_method(this, "update_translations");
  206. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  207. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  208. undo_redo->commit_action();
  209. }
  210. void LocalizationEditor::_translation_res_option_delete(Object *p_item, int p_column, int p_button) {
  211. if (updating_translations) {
  212. return;
  213. }
  214. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  215. return;
  216. }
  217. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  218. TreeItem *k = translation_remap->get_selected();
  219. ERR_FAIL_COND(!k);
  220. TreeItem *ed = Object::cast_to<TreeItem>(p_item);
  221. ERR_FAIL_COND(!ed);
  222. String key = k->get_metadata(0);
  223. int idx = ed->get_metadata(0);
  224. ERR_FAIL_COND(!remaps.has(key));
  225. PackedStringArray r = remaps[key];
  226. ERR_FAIL_INDEX(idx, r.size());
  227. r.remove(idx);
  228. remaps[key] = r;
  229. undo_redo->create_action(TTR("Remove Resource Remap Option"));
  230. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  231. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  232. undo_redo->add_do_method(this, "update_translations");
  233. undo_redo->add_undo_method(this, "update_translations");
  234. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  235. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  236. undo_redo->commit_action();
  237. }
  238. void LocalizationEditor::_translation_filter_option_changed() {
  239. int sel_id = translation_locale_filter_mode->get_selected_id();
  240. TreeItem *t = translation_filter->get_edited();
  241. String locale = t->get_tooltip(0);
  242. bool checked = t->is_checked(0);
  243. Variant prev;
  244. Array f_locales_all;
  245. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  246. f_locales_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  247. prev = f_locales_all;
  248. if (f_locales_all.size() != 2) {
  249. f_locales_all.clear();
  250. f_locales_all.append(sel_id);
  251. f_locales_all.append(Array());
  252. }
  253. } else {
  254. f_locales_all.append(sel_id);
  255. f_locales_all.append(Array());
  256. }
  257. Array f_locales = f_locales_all[1];
  258. int l_idx = f_locales.find(locale);
  259. if (checked) {
  260. if (l_idx == -1) {
  261. f_locales.append(locale);
  262. }
  263. } else {
  264. if (l_idx != -1) {
  265. f_locales.remove(l_idx);
  266. }
  267. }
  268. f_locales.sort();
  269. undo_redo->create_action(TTR("Changed Locale Filter"));
  270. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", f_locales_all);
  271. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", prev);
  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::_translation_filter_mode_changed(int p_mode) {
  279. int sel_id = translation_locale_filter_mode->get_selected_id();
  280. Variant prev;
  281. Array f_locales_all;
  282. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  283. f_locales_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  284. prev = f_locales_all;
  285. if (f_locales_all.size() != 2) {
  286. f_locales_all.clear();
  287. f_locales_all.append(sel_id);
  288. f_locales_all.append(Array());
  289. } else {
  290. f_locales_all[0] = sel_id;
  291. }
  292. } else {
  293. f_locales_all.append(sel_id);
  294. f_locales_all.append(Array());
  295. }
  296. undo_redo->create_action(TTR("Changed Locale Filter Mode"));
  297. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", f_locales_all);
  298. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", prev);
  299. undo_redo->add_do_method(this, "update_translations");
  300. undo_redo->add_undo_method(this, "update_translations");
  301. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  302. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  303. undo_redo->commit_action();
  304. }
  305. void LocalizationEditor::_pot_add(const PackedStringArray &p_paths) {
  306. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  307. for (int i = 0; i < p_paths.size(); i++) {
  308. if (!pot_translations.has(p_paths[i])) {
  309. pot_translations.push_back(p_paths[i]);
  310. }
  311. }
  312. undo_redo->create_action(vformat(TTR("Add %d file(s) for POT generation"), p_paths.size()));
  313. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  314. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  315. undo_redo->add_do_method(this, "update_translations");
  316. undo_redo->add_undo_method(this, "update_translations");
  317. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  318. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  319. undo_redo->commit_action();
  320. }
  321. void LocalizationEditor::_pot_delete(Object *p_item, int p_column, int p_button) {
  322. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  323. ERR_FAIL_COND(!ti);
  324. int idx = ti->get_metadata(0);
  325. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  326. ERR_FAIL_INDEX(idx, pot_translations.size());
  327. pot_translations.remove(idx);
  328. undo_redo->create_action(TTR("Remove file from POT generation"));
  329. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  330. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  331. undo_redo->add_do_method(this, "update_translations");
  332. undo_redo->add_undo_method(this, "update_translations");
  333. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  334. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  335. undo_redo->commit_action();
  336. }
  337. void LocalizationEditor::_pot_file_open() {
  338. pot_file_open_dialog->popup_file_dialog();
  339. }
  340. void LocalizationEditor::_pot_generate_open() {
  341. pot_generate_dialog->popup_file_dialog();
  342. }
  343. void LocalizationEditor::_pot_generate(const String &p_file) {
  344. POTGenerator::get_singleton()->generate_pot(p_file);
  345. }
  346. void LocalizationEditor::_update_pot_file_extensions() {
  347. pot_file_open_dialog->clear_filters();
  348. List<String> translation_parse_file_extensions;
  349. EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
  350. for (const String &E : translation_parse_file_extensions) {
  351. pot_file_open_dialog->add_filter("*." + E);
  352. }
  353. }
  354. void LocalizationEditor::update_translations() {
  355. if (updating_translations) {
  356. return;
  357. }
  358. updating_translations = true;
  359. translation_list->clear();
  360. TreeItem *root = translation_list->create_item(nullptr);
  361. translation_list->set_hide_root(true);
  362. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
  363. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  364. for (int i = 0; i < translations.size(); i++) {
  365. TreeItem *t = translation_list->create_item(root);
  366. t->set_editable(0, false);
  367. t->set_text(0, translations[i].replace_first("res://", ""));
  368. t->set_tooltip(0, translations[i]);
  369. t->set_metadata(0, i);
  370. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  371. }
  372. }
  373. Vector<String> langs = TranslationServer::get_all_locales();
  374. Vector<String> names = TranslationServer::get_all_locale_names();
  375. // Update filter tab
  376. Array l_filter_all;
  377. bool is_arr_empty = true;
  378. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  379. l_filter_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  380. if (l_filter_all.size() == 2) {
  381. translation_locale_filter_mode->select(l_filter_all[0]);
  382. is_arr_empty = false;
  383. }
  384. }
  385. if (is_arr_empty) {
  386. l_filter_all.append(0);
  387. l_filter_all.append(Array());
  388. translation_locale_filter_mode->select(0);
  389. }
  390. int filter_mode = l_filter_all[0];
  391. Array l_filter = l_filter_all[1];
  392. int s = names.size();
  393. bool is_short_list_when_show_all_selected = filter_mode == SHOW_ALL_LOCALES && translation_filter_treeitems.size() < s;
  394. bool is_full_list_when_show_only_selected = filter_mode == SHOW_ONLY_SELECTED_LOCALES && translation_filter_treeitems.size() == s;
  395. bool should_recreate_locales_list = is_short_list_when_show_all_selected || is_full_list_when_show_only_selected;
  396. if (!translation_locales_list_created || should_recreate_locales_list) {
  397. translation_locales_list_created = true;
  398. translation_filter->clear();
  399. root = translation_filter->create_item(nullptr);
  400. translation_filter->set_hide_root(true);
  401. translation_filter_treeitems.clear();
  402. for (int i = 0; i < s; i++) {
  403. String n = names[i];
  404. String l = langs[i];
  405. bool is_checked = l_filter.has(l);
  406. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && !is_checked) {
  407. continue;
  408. }
  409. TreeItem *t = translation_filter->create_item(root);
  410. t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  411. t->set_text(0, n);
  412. t->set_editable(0, true);
  413. t->set_tooltip(0, l);
  414. t->set_checked(0, is_checked);
  415. translation_filter_treeitems.push_back(t);
  416. }
  417. } else {
  418. for (int i = 0; i < translation_filter_treeitems.size(); i++) {
  419. TreeItem *t = translation_filter_treeitems[i];
  420. t->set_checked(0, l_filter.has(t->get_tooltip(0)));
  421. }
  422. }
  423. // Update translation remaps.
  424. String remap_selected;
  425. if (translation_remap->get_selected()) {
  426. remap_selected = translation_remap->get_selected()->get_metadata(0);
  427. }
  428. translation_remap->clear();
  429. translation_remap_options->clear();
  430. root = translation_remap->create_item(nullptr);
  431. TreeItem *root2 = translation_remap_options->create_item(nullptr);
  432. translation_remap->set_hide_root(true);
  433. translation_remap_options->set_hide_root(true);
  434. translation_res_option_add_button->set_disabled(true);
  435. translation_locales_idxs_remap.clear();
  436. translation_locales_idxs_remap.resize(l_filter.size());
  437. int fl_idx_count = translation_locales_idxs_remap.size();
  438. String langnames = "";
  439. int l_idx = 0;
  440. for (int i = 0; i < names.size(); i++) {
  441. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && fl_idx_count != 0) {
  442. if (l_filter.size() > 0) {
  443. if (l_filter.find(langs[i]) != -1) {
  444. if (langnames.length() > 0) {
  445. langnames += ",";
  446. }
  447. langnames += names[i];
  448. translation_locales_idxs_remap.write[l_idx] = i;
  449. l_idx++;
  450. }
  451. }
  452. } else {
  453. if (i > 0) {
  454. langnames += ",";
  455. }
  456. langnames += names[i];
  457. }
  458. }
  459. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  460. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  461. List<Variant> rk;
  462. remaps.get_key_list(&rk);
  463. Vector<String> keys;
  464. for (const Variant &E : rk) {
  465. keys.push_back(E);
  466. }
  467. keys.sort();
  468. for (int i = 0; i < keys.size(); i++) {
  469. TreeItem *t = translation_remap->create_item(root);
  470. t->set_editable(0, false);
  471. t->set_text(0, keys[i].replace_first("res://", ""));
  472. t->set_tooltip(0, keys[i]);
  473. t->set_metadata(0, keys[i]);
  474. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  475. if (keys[i] == remap_selected) {
  476. t->select(0);
  477. translation_res_option_add_button->set_disabled(false);
  478. PackedStringArray selected = remaps[keys[i]];
  479. for (int j = 0; j < selected.size(); j++) {
  480. String s2 = selected[j];
  481. int qp = s2.rfind(":");
  482. String path = s2.substr(0, qp);
  483. String locale = s2.substr(qp + 1, s2.length());
  484. TreeItem *t2 = translation_remap_options->create_item(root2);
  485. t2->set_editable(0, false);
  486. t2->set_text(0, path.replace_first("res://", ""));
  487. t2->set_tooltip(0, path);
  488. t2->set_metadata(0, j);
  489. t2->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  490. t2->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  491. t2->set_text(1, langnames);
  492. t2->set_editable(1, true);
  493. t2->set_metadata(1, path);
  494. int idx = langs.find(locale);
  495. if (idx < 0) {
  496. idx = 0;
  497. }
  498. int f_idx = translation_locales_idxs_remap.find(idx);
  499. if (f_idx != -1 && fl_idx_count > 0 && filter_mode == SHOW_ONLY_SELECTED_LOCALES) {
  500. t2->set_range(1, f_idx);
  501. } else {
  502. t2->set_range(1, idx);
  503. }
  504. }
  505. }
  506. }
  507. }
  508. // Update translation POT files.
  509. translation_pot_list->clear();
  510. root = translation_pot_list->create_item(nullptr);
  511. translation_pot_list->set_hide_root(true);
  512. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations_pot_files")) {
  513. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  514. for (int i = 0; i < pot_translations.size(); i++) {
  515. TreeItem *t = translation_pot_list->create_item(root);
  516. t->set_editable(0, false);
  517. t->set_text(0, pot_translations[i].replace_first("res://", ""));
  518. t->set_tooltip(0, pot_translations[i]);
  519. t->set_metadata(0, i);
  520. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  521. }
  522. }
  523. // New translation parser plugin might extend possible file extensions in POT generation.
  524. _update_pot_file_extensions();
  525. updating_translations = false;
  526. }
  527. void LocalizationEditor::_bind_methods() {
  528. ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
  529. ADD_SIGNAL(MethodInfo("localization_changed"));
  530. }
  531. LocalizationEditor::LocalizationEditor() {
  532. undo_redo = EditorNode::get_undo_redo();
  533. updating_translations = false;
  534. localization_changed = "localization_changed";
  535. translation_locales_idxs_remap = Vector<int>();
  536. translation_locales_list_created = false;
  537. TabContainer *translations = memnew(TabContainer);
  538. translations->set_tab_align(TabContainer::ALIGN_LEFT);
  539. translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  540. add_child(translations);
  541. {
  542. VBoxContainer *tvb = memnew(VBoxContainer);
  543. tvb->set_name(TTR("Translations"));
  544. translations->add_child(tvb);
  545. HBoxContainer *thb = memnew(HBoxContainer);
  546. Label *l = memnew(Label(TTR("Translations:")));
  547. l->set_theme_type_variation("HeaderSmall");
  548. thb->add_child(l);
  549. thb->add_spacer();
  550. tvb->add_child(thb);
  551. Button *addtr = memnew(Button(TTR("Add...")));
  552. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_file_open));
  553. thb->add_child(addtr);
  554. VBoxContainer *tmc = memnew(VBoxContainer);
  555. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  556. tvb->add_child(tmc);
  557. translation_list = memnew(Tree);
  558. translation_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  559. tmc->add_child(translation_list);
  560. translation_file_open = memnew(EditorFileDialog);
  561. translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  562. translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
  563. add_child(translation_file_open);
  564. }
  565. {
  566. VBoxContainer *tvb = memnew(VBoxContainer);
  567. tvb->set_name(TTR("Remaps"));
  568. translations->add_child(tvb);
  569. HBoxContainer *thb = memnew(HBoxContainer);
  570. Label *l = memnew(Label(TTR("Resources:")));
  571. l->set_theme_type_variation("HeaderSmall");
  572. thb->add_child(l);
  573. thb->add_spacer();
  574. tvb->add_child(thb);
  575. Button *addtr = memnew(Button(TTR("Add...")));
  576. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_file_open));
  577. thb->add_child(addtr);
  578. VBoxContainer *tmc = memnew(VBoxContainer);
  579. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  580. tvb->add_child(tmc);
  581. translation_remap = memnew(Tree);
  582. translation_remap->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  583. translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
  584. translation_remap->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_delete));
  585. tmc->add_child(translation_remap);
  586. translation_res_file_open_dialog = memnew(EditorFileDialog);
  587. translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  588. translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
  589. add_child(translation_res_file_open_dialog);
  590. thb = memnew(HBoxContainer);
  591. l = memnew(Label(TTR("Remaps by Locale:")));
  592. l->set_theme_type_variation("HeaderSmall");
  593. thb->add_child(l);
  594. thb->add_spacer();
  595. tvb->add_child(thb);
  596. addtr = memnew(Button(TTR("Add...")));
  597. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
  598. translation_res_option_add_button = addtr;
  599. thb->add_child(addtr);
  600. tmc = memnew(VBoxContainer);
  601. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  602. tvb->add_child(tmc);
  603. translation_remap_options = memnew(Tree);
  604. translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  605. translation_remap_options->set_columns(2);
  606. translation_remap_options->set_column_title(0, TTR("Path"));
  607. translation_remap_options->set_column_title(1, TTR("Locale"));
  608. translation_remap_options->set_column_titles_visible(true);
  609. translation_remap_options->set_column_expand(0, true);
  610. translation_remap_options->set_column_clip_content(0, true);
  611. translation_remap_options->set_column_expand(1, false);
  612. translation_remap_options->set_column_clip_content(1, true);
  613. translation_remap_options->set_column_custom_minimum_width(1, 200);
  614. translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
  615. translation_remap_options->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
  616. tmc->add_child(translation_remap_options);
  617. translation_res_option_file_open_dialog = memnew(EditorFileDialog);
  618. translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  619. translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
  620. add_child(translation_res_option_file_open_dialog);
  621. }
  622. {
  623. VBoxContainer *tvb = memnew(VBoxContainer);
  624. tvb->set_name(TTR("Locales Filter"));
  625. translations->add_child(tvb);
  626. VBoxContainer *tmc = memnew(VBoxContainer);
  627. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  628. tvb->add_child(tmc);
  629. translation_locale_filter_mode = memnew(OptionButton);
  630. translation_locale_filter_mode->add_item(TTR("Show All Locales"), SHOW_ALL_LOCALES);
  631. translation_locale_filter_mode->add_item(TTR("Show Selected Locales Only"), SHOW_ONLY_SELECTED_LOCALES);
  632. translation_locale_filter_mode->select(0);
  633. translation_locale_filter_mode->connect("item_selected", callable_mp(this, &LocalizationEditor::_translation_filter_mode_changed));
  634. tmc->add_margin_child(TTR("Filter mode:"), translation_locale_filter_mode);
  635. Label *l = memnew(Label(TTR("Locales:")));
  636. l->set_theme_type_variation("HeaderSmall");
  637. tmc->add_child(l);
  638. translation_filter = memnew(Tree);
  639. translation_filter->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  640. translation_filter->set_columns(1);
  641. translation_filter->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_filter_option_changed));
  642. tmc->add_child(translation_filter);
  643. }
  644. {
  645. VBoxContainer *tvb = memnew(VBoxContainer);
  646. tvb->set_name(TTR("POT Generation"));
  647. translations->add_child(tvb);
  648. HBoxContainer *thb = memnew(HBoxContainer);
  649. Label *l = memnew(Label(TTR("Files with translation strings:")));
  650. l->set_theme_type_variation("HeaderSmall");
  651. thb->add_child(l);
  652. thb->add_spacer();
  653. tvb->add_child(thb);
  654. Button *addtr = memnew(Button(TTR("Add...")));
  655. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_file_open));
  656. thb->add_child(addtr);
  657. Button *generate = memnew(Button(TTR("Generate POT")));
  658. generate->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_generate_open));
  659. thb->add_child(generate);
  660. VBoxContainer *tmc = memnew(VBoxContainer);
  661. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  662. tvb->add_child(tmc);
  663. translation_pot_list = memnew(Tree);
  664. translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  665. tmc->add_child(translation_pot_list);
  666. pot_generate_dialog = memnew(EditorFileDialog);
  667. pot_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  668. pot_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_pot_generate));
  669. add_child(pot_generate_dialog);
  670. pot_file_open_dialog = memnew(EditorFileDialog);
  671. pot_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  672. pot_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_pot_add));
  673. add_child(pot_file_open_dialog);
  674. }
  675. }