localization_editor.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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_TEXT_SERVER_CHANGED) {
  38. ts_name->set_text(TTR("Text server: ") + TS->get_name());
  39. FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  40. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  41. if (file_check->file_exists("res://" + TS->get_support_data_filename())) {
  42. ts_data_status->set_text(TTR("Support data: ") + TTR("Installed"));
  43. ts_install->set_disabled(true);
  44. } else {
  45. ts_data_status->set_text(TTR("Support data: ") + TTR("Not installed"));
  46. ts_install->set_disabled(false);
  47. }
  48. } else {
  49. ts_data_status->set_text(TTR("Support data: ") + TTR("Not supported"));
  50. ts_install->set_disabled(false);
  51. }
  52. ts_data_info->set_text(TTR("Info: ") + TS->get_support_data_info());
  53. }
  54. if (p_what == NOTIFICATION_ENTER_TREE) {
  55. translation_list->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_delete));
  56. translation_pot_list->connect("button_pressed", callable_mp(this, &LocalizationEditor::_pot_delete));
  57. List<String> tfn;
  58. ResourceLoader::get_recognized_extensions_for_type("Translation", &tfn);
  59. for (List<String>::Element *E = tfn.front(); E; E = E->next()) {
  60. translation_file_open->add_filter("*." + E->get());
  61. }
  62. List<String> rfn;
  63. ResourceLoader::get_recognized_extensions_for_type("Resource", &rfn);
  64. for (List<String>::Element *E = rfn.front(); E; E = E->next()) {
  65. translation_res_file_open_dialog->add_filter("*." + E->get());
  66. translation_res_option_file_open_dialog->add_filter("*." + E->get());
  67. }
  68. _update_pot_file_extensions();
  69. pot_generate_dialog->add_filter("*.pot");
  70. }
  71. }
  72. void LocalizationEditor::add_translation(const String &p_translation) {
  73. PackedStringArray translations;
  74. translations.push_back(p_translation);
  75. _translation_add(translations);
  76. }
  77. void LocalizationEditor::_translation_add(const PackedStringArray &p_paths) {
  78. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  79. for (int i = 0; i < p_paths.size(); i++) {
  80. if (!translations.has(p_paths[i])) {
  81. // Don't add duplicate translation paths.
  82. translations.push_back(p_paths[i]);
  83. }
  84. }
  85. undo_redo->create_action(vformat(TTR("Add %d Translations"), p_paths.size()));
  86. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  87. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  88. undo_redo->add_do_method(this, "update_translations");
  89. undo_redo->add_undo_method(this, "update_translations");
  90. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  91. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  92. undo_redo->commit_action();
  93. }
  94. void LocalizationEditor::_translation_file_open() {
  95. translation_file_open->popup_file_dialog();
  96. }
  97. void LocalizationEditor::_translation_delete(Object *p_item, int p_column, int p_button) {
  98. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  99. ERR_FAIL_COND(!ti);
  100. int idx = ti->get_metadata(0);
  101. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  102. ERR_FAIL_INDEX(idx, translations.size());
  103. translations.remove(idx);
  104. undo_redo->create_action(TTR("Remove Translation"));
  105. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", translations);
  106. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations", ProjectSettings::get_singleton()->get("internationalization/locale/translations"));
  107. undo_redo->add_do_method(this, "update_translations");
  108. undo_redo->add_undo_method(this, "update_translations");
  109. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  110. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  111. undo_redo->commit_action();
  112. }
  113. void LocalizationEditor::_translation_res_file_open() {
  114. translation_res_file_open_dialog->popup_file_dialog();
  115. }
  116. void LocalizationEditor::_translation_res_add(const PackedStringArray &p_paths) {
  117. Variant prev;
  118. Dictionary remaps;
  119. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  120. remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  121. prev = remaps;
  122. }
  123. for (int i = 0; i < p_paths.size(); i++) {
  124. if (!remaps.has(p_paths[i])) {
  125. // Don't overwrite with an empty remap array if an array already exists for the given path.
  126. remaps[p_paths[i]] = PackedStringArray();
  127. }
  128. }
  129. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Path(s)"), p_paths.size()));
  130. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  131. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", prev);
  132. undo_redo->add_do_method(this, "update_translations");
  133. undo_redo->add_undo_method(this, "update_translations");
  134. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  135. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  136. undo_redo->commit_action();
  137. }
  138. void LocalizationEditor::_translation_res_option_file_open() {
  139. translation_res_option_file_open_dialog->popup_file_dialog();
  140. }
  141. void LocalizationEditor::_translation_res_option_add(const PackedStringArray &p_paths) {
  142. ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps"));
  143. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  144. TreeItem *k = translation_remap->get_selected();
  145. ERR_FAIL_COND(!k);
  146. String key = k->get_metadata(0);
  147. ERR_FAIL_COND(!remaps.has(key));
  148. PackedStringArray r = remaps[key];
  149. for (int i = 0; i < p_paths.size(); i++) {
  150. r.push_back(p_paths[i] + ":" + "en");
  151. }
  152. remaps[key] = r;
  153. undo_redo->create_action(vformat(TTR("Translation Resource Remap: Add %d Remap(s)"), p_paths.size()));
  154. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  155. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  156. undo_redo->add_do_method(this, "update_translations");
  157. undo_redo->add_undo_method(this, "update_translations");
  158. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  159. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  160. undo_redo->commit_action();
  161. }
  162. void LocalizationEditor::_translation_res_select() {
  163. if (updating_translations) {
  164. return;
  165. }
  166. call_deferred("update_translations");
  167. }
  168. void LocalizationEditor::_translation_res_option_changed() {
  169. if (updating_translations) {
  170. return;
  171. }
  172. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  173. return;
  174. }
  175. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  176. TreeItem *k = translation_remap->get_selected();
  177. ERR_FAIL_COND(!k);
  178. TreeItem *ed = translation_remap_options->get_edited();
  179. ERR_FAIL_COND(!ed);
  180. String key = k->get_metadata(0);
  181. int idx = ed->get_metadata(0);
  182. String path = ed->get_metadata(1);
  183. int which = ed->get_range(1);
  184. Vector<String> langs = TranslationServer::get_all_locales();
  185. ERR_FAIL_INDEX(which, langs.size());
  186. ERR_FAIL_COND(!remaps.has(key));
  187. PackedStringArray r = remaps[key];
  188. ERR_FAIL_INDEX(idx, r.size());
  189. if (translation_locales_idxs_remap.size() > which) {
  190. r.set(idx, path + ":" + langs[translation_locales_idxs_remap[which]]);
  191. } else {
  192. r.set(idx, path + ":" + langs[which]);
  193. }
  194. remaps[key] = r;
  195. updating_translations = true;
  196. undo_redo->create_action(TTR("Change Resource Remap Language"));
  197. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  198. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  199. undo_redo->add_do_method(this, "update_translations");
  200. undo_redo->add_undo_method(this, "update_translations");
  201. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  202. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  203. undo_redo->commit_action();
  204. updating_translations = false;
  205. }
  206. void LocalizationEditor::_translation_res_delete(Object *p_item, int p_column, int p_button) {
  207. if (updating_translations) {
  208. return;
  209. }
  210. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  211. return;
  212. }
  213. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  214. TreeItem *k = Object::cast_to<TreeItem>(p_item);
  215. String key = k->get_metadata(0);
  216. ERR_FAIL_COND(!remaps.has(key));
  217. remaps.erase(key);
  218. undo_redo->create_action(TTR("Remove Resource Remap"));
  219. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  220. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  221. undo_redo->add_do_method(this, "update_translations");
  222. undo_redo->add_undo_method(this, "update_translations");
  223. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  224. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  225. undo_redo->commit_action();
  226. }
  227. void LocalizationEditor::_translation_res_option_delete(Object *p_item, int p_column, int p_button) {
  228. if (updating_translations) {
  229. return;
  230. }
  231. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  232. return;
  233. }
  234. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  235. TreeItem *k = translation_remap->get_selected();
  236. ERR_FAIL_COND(!k);
  237. TreeItem *ed = Object::cast_to<TreeItem>(p_item);
  238. ERR_FAIL_COND(!ed);
  239. String key = k->get_metadata(0);
  240. int idx = ed->get_metadata(0);
  241. ERR_FAIL_COND(!remaps.has(key));
  242. PackedStringArray r = remaps[key];
  243. ERR_FAIL_INDEX(idx, r.size());
  244. r.remove(idx);
  245. remaps[key] = r;
  246. undo_redo->create_action(TTR("Remove Resource Remap Option"));
  247. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", remaps);
  248. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translation_remaps", ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"));
  249. undo_redo->add_do_method(this, "update_translations");
  250. undo_redo->add_undo_method(this, "update_translations");
  251. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  252. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  253. undo_redo->commit_action();
  254. }
  255. void LocalizationEditor::_translation_filter_option_changed() {
  256. int sel_id = translation_locale_filter_mode->get_selected_id();
  257. TreeItem *t = translation_filter->get_edited();
  258. String locale = t->get_tooltip(0);
  259. bool checked = t->is_checked(0);
  260. Variant prev;
  261. Array f_locales_all;
  262. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  263. f_locales_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  264. prev = f_locales_all;
  265. if (f_locales_all.size() != 2) {
  266. f_locales_all.clear();
  267. f_locales_all.append(sel_id);
  268. f_locales_all.append(Array());
  269. }
  270. } else {
  271. f_locales_all.append(sel_id);
  272. f_locales_all.append(Array());
  273. }
  274. Array f_locales = f_locales_all[1];
  275. int l_idx = f_locales.find(locale);
  276. if (checked) {
  277. if (l_idx == -1) {
  278. f_locales.append(locale);
  279. }
  280. } else {
  281. if (l_idx != -1) {
  282. f_locales.remove(l_idx);
  283. }
  284. }
  285. f_locales.sort();
  286. undo_redo->create_action(TTR("Changed Locale Filter"));
  287. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", f_locales_all);
  288. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", prev);
  289. undo_redo->add_do_method(this, "update_translations");
  290. undo_redo->add_undo_method(this, "update_translations");
  291. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  292. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  293. undo_redo->commit_action();
  294. }
  295. void LocalizationEditor::_translation_filter_mode_changed(int p_mode) {
  296. int sel_id = translation_locale_filter_mode->get_selected_id();
  297. Variant prev;
  298. Array f_locales_all;
  299. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  300. f_locales_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  301. prev = f_locales_all;
  302. if (f_locales_all.size() != 2) {
  303. f_locales_all.clear();
  304. f_locales_all.append(sel_id);
  305. f_locales_all.append(Array());
  306. } else {
  307. f_locales_all[0] = sel_id;
  308. }
  309. } else {
  310. f_locales_all.append(sel_id);
  311. f_locales_all.append(Array());
  312. }
  313. undo_redo->create_action(TTR("Changed Locale Filter Mode"));
  314. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", f_locales_all);
  315. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/locale_filter", prev);
  316. undo_redo->add_do_method(this, "update_translations");
  317. undo_redo->add_undo_method(this, "update_translations");
  318. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  319. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  320. undo_redo->commit_action();
  321. }
  322. void LocalizationEditor::_pot_add(const PackedStringArray &p_paths) {
  323. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  324. for (int i = 0; i < p_paths.size(); i++) {
  325. for (int j = 0; j < pot_translations.size(); j++) {
  326. if (pot_translations[j] == p_paths[i]) {
  327. continue; //exists
  328. }
  329. }
  330. pot_translations.push_back(p_paths[i]);
  331. }
  332. undo_redo->create_action(vformat(TTR("Add %d file(s) for POT generation"), p_paths.size()));
  333. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  334. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  335. undo_redo->add_do_method(this, "update_translations");
  336. undo_redo->add_undo_method(this, "update_translations");
  337. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  338. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  339. undo_redo->commit_action();
  340. }
  341. void LocalizationEditor::_pot_delete(Object *p_item, int p_column, int p_button) {
  342. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  343. ERR_FAIL_COND(!ti);
  344. int idx = ti->get_metadata(0);
  345. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  346. ERR_FAIL_INDEX(idx, pot_translations.size());
  347. pot_translations.remove(idx);
  348. undo_redo->create_action(TTR("Remove file from POT generation"));
  349. undo_redo->add_do_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", pot_translations);
  350. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "internationalization/locale/translations_pot_files", ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files"));
  351. undo_redo->add_do_method(this, "update_translations");
  352. undo_redo->add_undo_method(this, "update_translations");
  353. undo_redo->add_do_method(this, "emit_signal", localization_changed);
  354. undo_redo->add_undo_method(this, "emit_signal", localization_changed);
  355. undo_redo->commit_action();
  356. }
  357. void LocalizationEditor::_pot_file_open() {
  358. pot_file_open_dialog->popup_file_dialog();
  359. }
  360. void LocalizationEditor::_pot_generate_open() {
  361. pot_generate_dialog->popup_file_dialog();
  362. }
  363. void LocalizationEditor::_pot_generate(const String &p_file) {
  364. POTGenerator::get_singleton()->generate_pot(p_file);
  365. }
  366. void LocalizationEditor::_update_pot_file_extensions() {
  367. pot_file_open_dialog->clear_filters();
  368. List<String> translation_parse_file_extensions;
  369. EditorTranslationParser::get_singleton()->get_recognized_extensions(&translation_parse_file_extensions);
  370. for (List<String>::Element *E = translation_parse_file_extensions.front(); E; E = E->next()) {
  371. pot_file_open_dialog->add_filter("*." + E->get());
  372. }
  373. }
  374. void LocalizationEditor::update_translations() {
  375. if (updating_translations) {
  376. return;
  377. }
  378. updating_translations = true;
  379. translation_list->clear();
  380. TreeItem *root = translation_list->create_item(nullptr);
  381. translation_list->set_hide_root(true);
  382. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations")) {
  383. PackedStringArray translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations");
  384. for (int i = 0; i < translations.size(); i++) {
  385. TreeItem *t = translation_list->create_item(root);
  386. t->set_editable(0, false);
  387. t->set_text(0, translations[i].replace_first("res://", ""));
  388. t->set_tooltip(0, translations[i]);
  389. t->set_metadata(0, i);
  390. t->add_button(0, get_theme_icon("Remove", "EditorIcons"), 0, false, TTR("Remove"));
  391. }
  392. }
  393. Vector<String> langs = TranslationServer::get_all_locales();
  394. Vector<String> names = TranslationServer::get_all_locale_names();
  395. // Update filter tab
  396. Array l_filter_all;
  397. bool is_arr_empty = true;
  398. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/locale_filter")) {
  399. l_filter_all = ProjectSettings::get_singleton()->get("internationalization/locale/locale_filter");
  400. if (l_filter_all.size() == 2) {
  401. translation_locale_filter_mode->select(l_filter_all[0]);
  402. is_arr_empty = false;
  403. }
  404. }
  405. if (is_arr_empty) {
  406. l_filter_all.append(0);
  407. l_filter_all.append(Array());
  408. translation_locale_filter_mode->select(0);
  409. }
  410. int filter_mode = l_filter_all[0];
  411. Array l_filter = l_filter_all[1];
  412. int s = names.size();
  413. bool is_short_list_when_show_all_selected = filter_mode == SHOW_ALL_LOCALES && translation_filter_treeitems.size() < s;
  414. bool is_full_list_when_show_only_selected = filter_mode == SHOW_ONLY_SELECTED_LOCALES && translation_filter_treeitems.size() == s;
  415. bool should_recreate_locales_list = is_short_list_when_show_all_selected || is_full_list_when_show_only_selected;
  416. if (!translation_locales_list_created || should_recreate_locales_list) {
  417. translation_locales_list_created = true;
  418. translation_filter->clear();
  419. root = translation_filter->create_item(nullptr);
  420. translation_filter->set_hide_root(true);
  421. translation_filter_treeitems.clear();
  422. for (int i = 0; i < s; i++) {
  423. String n = names[i];
  424. String l = langs[i];
  425. bool is_checked = l_filter.has(l);
  426. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && !is_checked) {
  427. continue;
  428. }
  429. TreeItem *t = translation_filter->create_item(root);
  430. t->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  431. t->set_text(0, n);
  432. t->set_editable(0, true);
  433. t->set_tooltip(0, l);
  434. t->set_checked(0, is_checked);
  435. translation_filter_treeitems.push_back(t);
  436. }
  437. } else {
  438. for (int i = 0; i < translation_filter_treeitems.size(); i++) {
  439. TreeItem *t = translation_filter_treeitems[i];
  440. t->set_checked(0, l_filter.has(t->get_tooltip(0)));
  441. }
  442. }
  443. // Update translation remaps.
  444. String remap_selected;
  445. if (translation_remap->get_selected()) {
  446. remap_selected = translation_remap->get_selected()->get_metadata(0);
  447. }
  448. translation_remap->clear();
  449. translation_remap_options->clear();
  450. root = translation_remap->create_item(nullptr);
  451. TreeItem *root2 = translation_remap_options->create_item(nullptr);
  452. translation_remap->set_hide_root(true);
  453. translation_remap_options->set_hide_root(true);
  454. translation_res_option_add_button->set_disabled(true);
  455. translation_locales_idxs_remap.clear();
  456. translation_locales_idxs_remap.resize(l_filter.size());
  457. int fl_idx_count = translation_locales_idxs_remap.size();
  458. String langnames = "";
  459. int l_idx = 0;
  460. for (int i = 0; i < names.size(); i++) {
  461. if (filter_mode == SHOW_ONLY_SELECTED_LOCALES && fl_idx_count != 0) {
  462. if (l_filter.size() > 0) {
  463. if (l_filter.find(langs[i]) != -1) {
  464. if (langnames.length() > 0) {
  465. langnames += ",";
  466. }
  467. langnames += names[i];
  468. translation_locales_idxs_remap.write[l_idx] = i;
  469. l_idx++;
  470. }
  471. }
  472. } else {
  473. if (i > 0) {
  474. langnames += ",";
  475. }
  476. langnames += names[i];
  477. }
  478. }
  479. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  480. Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
  481. List<Variant> rk;
  482. remaps.get_key_list(&rk);
  483. Vector<String> keys;
  484. for (List<Variant>::Element *E = rk.front(); E; E = E->next()) {
  485. keys.push_back(E->get());
  486. }
  487. keys.sort();
  488. for (int i = 0; i < keys.size(); i++) {
  489. TreeItem *t = translation_remap->create_item(root);
  490. t->set_editable(0, false);
  491. t->set_text(0, keys[i].replace_first("res://", ""));
  492. t->set_tooltip(0, keys[i]);
  493. t->set_metadata(0, keys[i]);
  494. t->add_button(0, get_theme_icon("Remove", "EditorIcons"), 0, false, TTR("Remove"));
  495. if (keys[i] == remap_selected) {
  496. t->select(0);
  497. translation_res_option_add_button->set_disabled(false);
  498. PackedStringArray selected = remaps[keys[i]];
  499. for (int j = 0; j < selected.size(); j++) {
  500. String s2 = selected[j];
  501. int qp = s2.rfind(":");
  502. String path = s2.substr(0, qp);
  503. String locale = s2.substr(qp + 1, s2.length());
  504. TreeItem *t2 = translation_remap_options->create_item(root2);
  505. t2->set_editable(0, false);
  506. t2->set_text(0, path.replace_first("res://", ""));
  507. t2->set_tooltip(0, path);
  508. t2->set_metadata(0, j);
  509. t2->add_button(0, get_theme_icon("Remove", "EditorIcons"), 0, false, TTR("Remove"));
  510. t2->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  511. t2->set_text(1, langnames);
  512. t2->set_editable(1, true);
  513. t2->set_metadata(1, path);
  514. int idx = langs.find(locale);
  515. if (idx < 0) {
  516. idx = 0;
  517. }
  518. int f_idx = translation_locales_idxs_remap.find(idx);
  519. if (f_idx != -1 && fl_idx_count > 0 && filter_mode == SHOW_ONLY_SELECTED_LOCALES) {
  520. t2->set_range(1, f_idx);
  521. } else {
  522. t2->set_range(1, idx);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. // Update translation POT files.
  529. translation_pot_list->clear();
  530. root = translation_pot_list->create_item(nullptr);
  531. translation_pot_list->set_hide_root(true);
  532. if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations_pot_files")) {
  533. PackedStringArray pot_translations = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  534. for (int i = 0; i < pot_translations.size(); i++) {
  535. TreeItem *t = translation_pot_list->create_item(root);
  536. t->set_editable(0, false);
  537. t->set_text(0, pot_translations[i].replace_first("res://", ""));
  538. t->set_tooltip(0, pot_translations[i]);
  539. t->set_metadata(0, i);
  540. t->add_button(0, get_theme_icon("Remove", "EditorIcons"), 0, false, TTR("Remove"));
  541. }
  542. }
  543. // New translation parser plugin might extend possible file extensions in POT generation.
  544. _update_pot_file_extensions();
  545. updating_translations = false;
  546. }
  547. void LocalizationEditor::_install_ts_data() {
  548. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  549. TS->save_support_data("res://" + TS->get_support_data_filename());
  550. }
  551. FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  552. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  553. if (file_check->file_exists("res://" + TS->get_support_data_filename())) {
  554. ts_data_status->set_text(TTR("Support data: ") + TTR("Installed"));
  555. ts_install->set_disabled(true);
  556. } else {
  557. ts_data_status->set_text(TTR("Support data: ") + TTR("Not installed"));
  558. ts_install->set_disabled(false);
  559. }
  560. } else {
  561. ts_data_status->set_text(TTR("Support data: ") + TTR("Not supported"));
  562. ts_install->set_disabled(false);
  563. }
  564. }
  565. void LocalizationEditor::_bind_methods() {
  566. ClassDB::bind_method(D_METHOD("update_translations"), &LocalizationEditor::update_translations);
  567. ADD_SIGNAL(MethodInfo("localization_changed"));
  568. }
  569. LocalizationEditor::LocalizationEditor() {
  570. undo_redo = EditorNode::get_undo_redo();
  571. updating_translations = false;
  572. localization_changed = "localization_changed";
  573. translation_locales_idxs_remap = Vector<int>();
  574. translation_locales_list_created = false;
  575. TabContainer *translations = memnew(TabContainer);
  576. translations->set_tab_align(TabContainer::ALIGN_LEFT);
  577. translations->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  578. add_child(translations);
  579. {
  580. VBoxContainer *tvb = memnew(VBoxContainer);
  581. tvb->set_name(TTR("Translations"));
  582. translations->add_child(tvb);
  583. HBoxContainer *thb = memnew(HBoxContainer);
  584. thb->add_child(memnew(Label(TTR("Translations:"))));
  585. thb->add_spacer();
  586. tvb->add_child(thb);
  587. Button *addtr = memnew(Button(TTR("Add...")));
  588. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_file_open));
  589. thb->add_child(addtr);
  590. VBoxContainer *tmc = memnew(VBoxContainer);
  591. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  592. tvb->add_child(tmc);
  593. translation_list = memnew(Tree);
  594. translation_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  595. tmc->add_child(translation_list);
  596. translation_file_open = memnew(EditorFileDialog);
  597. translation_file_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  598. translation_file_open->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_add));
  599. add_child(translation_file_open);
  600. }
  601. {
  602. VBoxContainer *tvb = memnew(VBoxContainer);
  603. tvb->set_name(TTR("Remaps"));
  604. translations->add_child(tvb);
  605. HBoxContainer *thb = memnew(HBoxContainer);
  606. thb->add_child(memnew(Label(TTR("Resources:"))));
  607. thb->add_spacer();
  608. tvb->add_child(thb);
  609. Button *addtr = memnew(Button(TTR("Add...")));
  610. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_file_open));
  611. thb->add_child(addtr);
  612. VBoxContainer *tmc = memnew(VBoxContainer);
  613. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  614. tvb->add_child(tmc);
  615. translation_remap = memnew(Tree);
  616. translation_remap->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  617. translation_remap->connect("cell_selected", callable_mp(this, &LocalizationEditor::_translation_res_select));
  618. translation_remap->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_delete));
  619. tmc->add_child(translation_remap);
  620. translation_res_file_open_dialog = memnew(EditorFileDialog);
  621. translation_res_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  622. translation_res_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_add));
  623. add_child(translation_res_file_open_dialog);
  624. thb = memnew(HBoxContainer);
  625. thb->add_child(memnew(Label(TTR("Remaps by Locale:"))));
  626. thb->add_spacer();
  627. tvb->add_child(thb);
  628. addtr = memnew(Button(TTR("Add...")));
  629. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_file_open));
  630. translation_res_option_add_button = addtr;
  631. thb->add_child(addtr);
  632. tmc = memnew(VBoxContainer);
  633. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  634. tvb->add_child(tmc);
  635. translation_remap_options = memnew(Tree);
  636. translation_remap_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  637. translation_remap_options->set_columns(2);
  638. translation_remap_options->set_column_title(0, TTR("Path"));
  639. translation_remap_options->set_column_title(1, TTR("Locale"));
  640. translation_remap_options->set_column_titles_visible(true);
  641. translation_remap_options->set_column_expand(0, true);
  642. translation_remap_options->set_column_expand(1, false);
  643. translation_remap_options->set_column_min_width(1, 200);
  644. translation_remap_options->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_res_option_changed));
  645. translation_remap_options->connect("button_pressed", callable_mp(this, &LocalizationEditor::_translation_res_option_delete));
  646. tmc->add_child(translation_remap_options);
  647. translation_res_option_file_open_dialog = memnew(EditorFileDialog);
  648. translation_res_option_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  649. translation_res_option_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_translation_res_option_add));
  650. add_child(translation_res_option_file_open_dialog);
  651. }
  652. {
  653. VBoxContainer *tvb = memnew(VBoxContainer);
  654. tvb->set_name(TTR("Locales Filter"));
  655. translations->add_child(tvb);
  656. VBoxContainer *tmc = memnew(VBoxContainer);
  657. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  658. tvb->add_child(tmc);
  659. translation_locale_filter_mode = memnew(OptionButton);
  660. translation_locale_filter_mode->add_item(TTR("Show All Locales"), SHOW_ALL_LOCALES);
  661. translation_locale_filter_mode->add_item(TTR("Show Selected Locales Only"), SHOW_ONLY_SELECTED_LOCALES);
  662. translation_locale_filter_mode->select(0);
  663. translation_locale_filter_mode->connect("item_selected", callable_mp(this, &LocalizationEditor::_translation_filter_mode_changed));
  664. tmc->add_margin_child(TTR("Filter mode:"), translation_locale_filter_mode);
  665. tmc->add_child(memnew(Label(TTR("Locales:"))));
  666. translation_filter = memnew(Tree);
  667. translation_filter->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  668. translation_filter->set_columns(1);
  669. translation_filter->connect("item_edited", callable_mp(this, &LocalizationEditor::_translation_filter_option_changed));
  670. tmc->add_child(translation_filter);
  671. }
  672. {
  673. VBoxContainer *tvb = memnew(VBoxContainer);
  674. tvb->set_name(TTR("POT Generation"));
  675. translations->add_child(tvb);
  676. HBoxContainer *thb = memnew(HBoxContainer);
  677. thb->add_child(memnew(Label(TTR("Files with translation strings:"))));
  678. thb->add_spacer();
  679. tvb->add_child(thb);
  680. Button *addtr = memnew(Button(TTR("Add...")));
  681. addtr->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_file_open));
  682. thb->add_child(addtr);
  683. Button *generate = memnew(Button(TTR("Generate POT")));
  684. generate->connect("pressed", callable_mp(this, &LocalizationEditor::_pot_generate_open));
  685. thb->add_child(generate);
  686. VBoxContainer *tmc = memnew(VBoxContainer);
  687. tmc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  688. tvb->add_child(tmc);
  689. translation_pot_list = memnew(Tree);
  690. translation_pot_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  691. tmc->add_child(translation_pot_list);
  692. pot_generate_dialog = memnew(EditorFileDialog);
  693. pot_generate_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  694. pot_generate_dialog->connect("file_selected", callable_mp(this, &LocalizationEditor::_pot_generate));
  695. add_child(pot_generate_dialog);
  696. pot_file_open_dialog = memnew(EditorFileDialog);
  697. pot_file_open_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  698. pot_file_open_dialog->connect("files_selected", callable_mp(this, &LocalizationEditor::_pot_add));
  699. add_child(pot_file_open_dialog);
  700. }
  701. {
  702. VBoxContainer *tvb = memnew(VBoxContainer);
  703. tvb->set_name(TTR("Text Server Data"));
  704. translations->add_child(tvb);
  705. ts_name = memnew(Label(TTR("Text server: ") + TS->get_name()));
  706. tvb->add_child(ts_name);
  707. ts_data_status = memnew(Label(TTR("Support data: ")));
  708. tvb->add_child(ts_data_status);
  709. ts_data_info = memnew(Label(TTR("Info: ") + TS->get_support_data_info()));
  710. tvb->add_child(ts_data_info);
  711. ts_install = memnew(Button(TTR("Install support data...")));
  712. ts_install->connect("pressed", callable_mp(this, &LocalizationEditor::_install_ts_data));
  713. tvb->add_child(ts_install);
  714. FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  715. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  716. if (file_check->file_exists("res://" + TS->get_support_data_filename())) {
  717. ts_data_status->set_text(TTR("Support data: ") + TTR("Installed"));
  718. ts_install->set_disabled(true);
  719. } else {
  720. ts_data_status->set_text(TTR("Support data: ") + TTR("Not installed"));
  721. ts_install->set_disabled(false);
  722. }
  723. } else {
  724. ts_data_status->set_text(TTR("Support data: ") + TTR("Not supported"));
  725. ts_install->set_disabled(false);
  726. }
  727. }
  728. }