localization_editor.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. for (int j = 0; j < pot_translations.size(); j++) {
  309. if (pot_translations[j] == p_paths[i]) {
  310. continue; //exists
  311. }
  312. }
  313. pot_translations.push_back(p_paths[i]);
  314. }
  315. undo_redo->create_action(vformat(TTR("Add %d file(s) for POT generation"), p_paths.size()));
  316. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  317. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  318. undo_redo->add_do_method(this, "update_translations");
  319. undo_redo->add_undo_method(this, "update_translations");
  320. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  321. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  322. undo_redo->commit_action();
  323. }
  324. void LocalizationEditor::_pot_delete(Object *p_item, int p_column, int p_button) {
  325. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  326. ERR_FAIL_COND(!ti);
  327. int idx = ti->get_metadata(0);
  328. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  329. ERR_FAIL_INDEX(idx, pot_translations.size());
  330. pot_translations.remove(idx);
  331. undo_redo->create_action(TTR("Remove file from POT generation"));
  332. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  333. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  334. undo_redo->add_do_method(this, "update_translations");
  335. undo_redo->add_undo_method(this, "update_translations");
  336. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  337. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  338. undo_redo->commit_action();
  339. }
  340. void LocalizationEditor::_pot_file_open() {
  341. pot_file_open_dialog->popup_file_dialog();
  342. }
  343. void LocalizationEditor::_pot_generate_open() {
  344. pot_generate_dialog->popup_file_dialog();
  345. }
  346. void LocalizationEditor::_pot_generate(const String &p_file) {
  347. POTGenerator::get_singleton()->generate_pot(p_file);
  348. }
  349. void LocalizationEditor::_update_pot_file_extensions() {
  350. pot_file_open_dialog->clear_filters();
  351. List<String> translation_parse_file_extensions;
  352. EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
  353. for (const String &E : translation_parse_file_extensions) {
  354. pot_file_open_dialog->add_filter("*." + E);
  355. }
  356. }
  357. void LocalizationEditor::update_translations() {
  358. if (updating_translations) {
  359. return;
  360. }
  361. updating_translations = true;
  362. translation_list->clear();
  363. TreeItem *root = translation_list->create_item(nullptr);
  364. translation_list->set_hide_root(true);
  365. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
  366. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  367. for (int i = 0; i < translations.size(); i++) {
  368. TreeItem *t = translation_list->create_item(root);
  369. t->set_editable(0, false);
  370. t->set_text(0, translations[i].replace_first("res://", ""));
  371. t->set_tooltip(0, translations[i]);
  372. t->set_metadata(0, i);
  373. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  374. }
  375. }
  376. Vector<String> langs = TranslationServer::get_all_locales();
  377. Vector<String> names = TranslationServer::get_all_locale_names();
  378. // Update filter tab
  379. Array l_filter_all;
  380. bool is_arr_empty = true;
  381. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  382. l_filter_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  383. if (l_filter_all.size() == 2) {
  384. translation_locale_filter_mode->select(l_filter_all[0]);
  385. is_arr_empty = false;
  386. }
  387. }
  388. if (is_arr_empty) {
  389. l_filter_all.append(0);
  390. l_filter_all.append(Array());
  391. translation_locale_filter_mode->select(0);
  392. }
  393. int filter_mode = l_filter_all[0];
  394. Array l_filter = l_filter_all[1];
  395. int s = names.size();
  396. bool is_short_list_when_show_all_selected = filter_mode == SHOW_ALL_LOCALES && translation_filter_treeitems.size() < s;
  397. bool is_full_list_when_show_only_selected = filter_mode == SHOW_ONLY_SELECTED_LOCALES && translation_filter_treeitems.size() == s;
  398. bool should_recreate_locales_list = is_short_list_when_show_all_selected || is_full_list_when_show_only_selected;
  399. if (!translation_locales_list_created || should_recreate_locales_list) {
  400. translation_locales_list_created = true;
  401. translation_filter->clear();
  402. root = translation_filter->create_item(nullptr);
  403. translation_filter->set_hide_root(true);
  404. translation_filter_treeitems.clear();
  405. for (int i = 0; i < s; i++) {
  406. String n = names[i];
  407. String l = langs[i];
  408. bool is_checked = l_filter.has(l);
  409. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && !is_checked) {
  410. continue;
  411. }
  412. TreeItem *t = translation_filter->create_item(root);
  413. t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  414. t->set_text(0, n);
  415. t->set_editable(0, true);
  416. t->set_tooltip(0, l);
  417. t->set_checked(0, is_checked);
  418. translation_filter_treeitems.push_back(t);
  419. }
  420. } else {
  421. for (int i = 0; i < translation_filter_treeitems.size(); i++) {
  422. TreeItem *t = translation_filter_treeitems[i];
  423. t->set_checked(0, l_filter.has(t->get_tooltip(0)));
  424. }
  425. }
  426. // Update translation remaps.
  427. String remap_selected;
  428. if (translation_remap->get_selected()) {
  429. remap_selected = translation_remap->get_selected()->get_metadata(0);
  430. }
  431. translation_remap->clear();
  432. translation_remap_options->clear();
  433. root = translation_remap->create_item(nullptr);
  434. TreeItem *root2 = translation_remap_options->create_item(nullptr);
  435. translation_remap->set_hide_root(true);
  436. translation_remap_options->set_hide_root(true);
  437. translation_res_option_add_button->set_disabled(true);
  438. translation_locales_idxs_remap.clear();
  439. translation_locales_idxs_remap.resize(l_filter.size());
  440. int fl_idx_count = translation_locales_idxs_remap.size();
  441. String langnames = "";
  442. int l_idx = 0;
  443. for (int i = 0; i < names.size(); i++) {
  444. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && fl_idx_count != 0) {
  445. if (l_filter.size() > 0) {
  446. if (l_filter.find(langs[i]) != -1) {
  447. if (langnames.length() > 0) {
  448. langnames += ",";
  449. }
  450. langnames += names[i];
  451. translation_locales_idxs_remap.write[l_idx] = i;
  452. l_idx++;
  453. }
  454. }
  455. } else {
  456. if (i > 0) {
  457. langnames += ",";
  458. }
  459. langnames += names[i];
  460. }
  461. }
  462. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  463. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  464. List<Variant> rk;
  465. remaps.get_key_list(&rk);
  466. Vector<String> keys;
  467. for (const Variant &E : rk) {
  468. keys.push_back(E);
  469. }
  470. keys.sort();
  471. for (int i = 0; i < keys.size(); i++) {
  472. TreeItem *t = translation_remap->create_item(root);
  473. t->set_editable(0, false);
  474. t->set_text(0, keys[i].replace_first("res://", ""));
  475. t->set_tooltip(0, keys[i]);
  476. t->set_metadata(0, keys[i]);
  477. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  478. if (keys[i] == remap_selected) {
  479. t->select(0);
  480. translation_res_option_add_button->set_disabled(false);
  481. PackedStringArray selected = remaps[keys[i]];
  482. for (int j = 0; j < selected.size(); j++) {
  483. String s2 = selected[j];
  484. int qp = s2.rfind(":");
  485. String path = s2.substr(0, qp);
  486. String locale = s2.substr(qp + 1, s2.length());
  487. TreeItem *t2 = translation_remap_options->create_item(root2);
  488. t2->set_editable(0, false);
  489. t2->set_text(0, path.replace_first("res://", ""));
  490. t2->set_tooltip(0, path);
  491. t2->set_metadata(0, j);
  492. t2->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  493. t2->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  494. t2->set_text(1, langnames);
  495. t2->set_editable(1, true);
  496. t2->set_metadata(1, path);
  497. int idx = langs.find(locale);
  498. if (idx < 0) {
  499. idx = 0;
  500. }
  501. int f_idx = translation_locales_idxs_remap.find(idx);
  502. if (f_idx != -1 && fl_idx_count > 0 && filter_mode == SHOW_ONLY_SELECTED_LOCALES) {
  503. t2->set_range(1, f_idx);
  504. } else {
  505. t2->set_range(1, idx);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. // Update translation POT files.
  512. translation_pot_list->clear();
  513. root = translation_pot_list->create_item(nullptr);
  514. translation_pot_list->set_hide_root(true);
  515. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations_pot_files")) {
  516. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  517. for (int i = 0; i < pot_translations.size(); i++) {
  518. TreeItem *t = translation_pot_list->create_item(root);
  519. t->set_editable(0, false);
  520. t->set_text(0, pot_translations[i].replace_first("res://", ""));
  521. t->set_tooltip(0, pot_translations[i]);
  522. t->set_metadata(0, i);
  523. t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
  524. }
  525. }
  526. // New translation parser plugin might extend possible file extensions in POT generation.
  527. _update_pot_file_extensions();
  528. updating_translations = false;
  529. }
  530. void LocalizationEditor::_bind_methods() {
  531. ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
  532. ADD_SIGNAL(MethodInfo("localization_changed"));
  533. }
  534. LocalizationEditor::LocalizationEditor() {
  535. undo_redo = EditorNode::get_undo_redo();
  536. updating_translations = false;
  537. localization_changed = "localization_changed";
  538. translation_locales_idxs_remap = Vector<int>();
  539. translation_locales_list_created = false;
  540. TabContainer *translations = memnew(TabContainer);
  541. translations->set_tab_align(TabContainer::ALIGN_LEFT);
  542. translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  543. add_child(translations);
  544. {
  545. VBoxContainer *tvb = memnew(VBoxContainer);
  546. tvb->set_name(TTR("Translations"));
  547. translations->add_child(tvb);
  548. HBoxContainer *thb = memnew(HBoxContainer);
  549. Label *l = memnew(Label(TTR("Translations:")));
  550. l->set_theme_type_variation("HeaderSmall");
  551. thb->add_child(l);
  552. thb->add_spacer();
  553. tvb->add_child(thb);
  554. Button *addtr = memnew(Button(TTR("Add...")));
  555. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_file_open));
  556. thb->add_child(addtr);
  557. VBoxContainer *tmc = memnew(VBoxContainer);
  558. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  559. tvb->add_child(tmc);
  560. translation_list = memnew(Tree);
  561. translation_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  562. tmc->add_child(translation_list);
  563. translation_file_open = memnew(EditorFileDialog);
  564. translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  565. translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
  566. add_child(translation_file_open);
  567. }
  568. {
  569. VBoxContainer *tvb = memnew(VBoxContainer);
  570. tvb->set_name(TTR("Remaps"));
  571. translations->add_child(tvb);
  572. HBoxContainer *thb = memnew(HBoxContainer);
  573. Label *l = memnew(Label(TTR("Resources:")));
  574. l->set_theme_type_variation("HeaderSmall");
  575. thb->add_child(l);
  576. thb->add_spacer();
  577. tvb->add_child(thb);
  578. Button *addtr = memnew(Button(TTR("Add...")));
  579. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_file_open));
  580. thb->add_child(addtr);
  581. VBoxContainer *tmc = memnew(VBoxContainer);
  582. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  583. tvb->add_child(tmc);
  584. translation_remap = memnew(Tree);
  585. translation_remap->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  586. translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
  587. translation_remap->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_delete));
  588. tmc->add_child(translation_remap);
  589. translation_res_file_open_dialog = memnew(EditorFileDialog);
  590. translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  591. translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
  592. add_child(translation_res_file_open_dialog);
  593. thb = memnew(HBoxContainer);
  594. l = memnew(Label(TTR("Remaps by Locale:")));
  595. l->set_theme_type_variation("HeaderSmall");
  596. thb->add_child(l);
  597. thb->add_spacer();
  598. tvb->add_child(thb);
  599. addtr = memnew(Button(TTR("Add...")));
  600. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
  601. translation_res_option_add_button = addtr;
  602. thb->add_child(addtr);
  603. tmc = memnew(VBoxContainer);
  604. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  605. tvb->add_child(tmc);
  606. translation_remap_options = memnew(Tree);
  607. translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  608. translation_remap_options->set_columns(2);
  609. translation_remap_options->set_column_title(0, TTR("Path"));
  610. translation_remap_options->set_column_title(1, TTR("Locale"));
  611. translation_remap_options->set_column_titles_visible(true);
  612. translation_remap_options->set_column_expand(0, true);
  613. translation_remap_options->set_column_clip_content(0, true);
  614. translation_remap_options->set_column_expand(1, false);
  615. translation_remap_options->set_column_clip_content(1, true);
  616. translation_remap_options->set_column_custom_minimum_width(1, 200);
  617. translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
  618. translation_remap_options->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
  619. tmc->add_child(translation_remap_options);
  620. translation_res_option_file_open_dialog = memnew(EditorFileDialog);
  621. translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  622. translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
  623. add_child(translation_res_option_file_open_dialog);
  624. }
  625. {
  626. VBoxContainer *tvb = memnew(VBoxContainer);
  627. tvb->set_name(TTR("Locales Filter"));
  628. translations->add_child(tvb);
  629. VBoxContainer *tmc = memnew(VBoxContainer);
  630. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  631. tvb->add_child(tmc);
  632. translation_locale_filter_mode = memnew(OptionButton);
  633. translation_locale_filter_mode->add_item(TTR("Show All Locales"), SHOW_ALL_LOCALES);
  634. translation_locale_filter_mode->add_item(TTR("Show Selected Locales Only"), SHOW_ONLY_SELECTED_LOCALES);
  635. translation_locale_filter_mode->select(0);
  636. translation_locale_filter_mode->connect("item_selected", callable_mp(this, &LocalizationEditor::_translation_filter_mode_changed));
  637. tmc->add_margin_child(TTR("Filter mode:"), translation_locale_filter_mode);
  638. Label *l = memnew(Label(TTR("Locales:")));
  639. l->set_theme_type_variation("HeaderSmall");
  640. tmc->add_child(l);
  641. translation_filter = memnew(Tree);
  642. translation_filter->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  643. translation_filter->set_columns(1);
  644. translation_filter->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_filter_option_changed));
  645. tmc->add_child(translation_filter);
  646. }
  647. {
  648. VBoxContainer *tvb = memnew(VBoxContainer);
  649. tvb->set_name(TTR("POT Generation"));
  650. translations->add_child(tvb);
  651. HBoxContainer *thb = memnew(HBoxContainer);
  652. Label *l = memnew(Label(TTR("Files with translation strings:")));
  653. l->set_theme_type_variation("HeaderSmall");
  654. thb->add_child(l);
  655. thb->add_spacer();
  656. tvb->add_child(thb);
  657. Button *addtr = memnew(Button(TTR("Add...")));
  658. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_file_open));
  659. thb->add_child(addtr);
  660. Button *generate = memnew(Button(TTR("Generate POT")));
  661. generate->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_generate_open));
  662. thb->add_child(generate);
  663. VBoxContainer *tmc = memnew(VBoxContainer);
  664. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  665. tvb->add_child(tmc);
  666. translation_pot_list = memnew(Tree);
  667. translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  668. tmc->add_child(translation_pot_list);
  669. pot_generate_dialog = memnew(EditorFileDialog);
  670. pot_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  671. pot_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_pot_generate));
  672. add_child(pot_generate_dialog);
  673. pot_file_open_dialog = memnew(EditorFileDialog);
  674. pot_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  675. pot_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_pot_add));
  676. add_child(pot_file_open_dialog);
  677. }
  678. }