resource_importer_csv_translation.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* resource_importer_csv_translation.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "resource_importer_csv_translation.h"
  31. #include "core/compressed_translation.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/os/file_access.h"
  34. #include "core/translation.h"
  35. String ResourceImporterCSVTranslation::get_importer_name() const {
  36. return "csv_translation";
  37. }
  38. String ResourceImporterCSVTranslation::get_visible_name() const {
  39. return "CSV Translation";
  40. }
  41. void ResourceImporterCSVTranslation::get_recognized_extensions(List<String> *p_extensions) const {
  42. p_extensions->push_back("csv");
  43. }
  44. String ResourceImporterCSVTranslation::get_save_extension() const {
  45. return ""; //does not save a single resource
  46. }
  47. String ResourceImporterCSVTranslation::get_resource_type() const {
  48. return "Translation";
  49. }
  50. bool ResourceImporterCSVTranslation::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  51. return true;
  52. }
  53. int ResourceImporterCSVTranslation::get_preset_count() const {
  54. return 0;
  55. }
  56. String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const {
  57. return "";
  58. }
  59. void ResourceImporterCSVTranslation::get_import_options(List<ImportOption> *r_options, int p_preset) const {
  60. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  61. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0));
  62. }
  63. Error ResourceImporterCSVTranslation::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
  64. bool compress = p_options["compress"];
  65. String delimiter;
  66. switch ((int)p_options["delimiter"]) {
  67. case 0: delimiter = ","; break;
  68. case 1: delimiter = ";"; break;
  69. case 2: delimiter = "\t"; break;
  70. }
  71. FileAccessRef f = FileAccess::open(p_source_file, FileAccess::READ);
  72. ERR_FAIL_COND_V(!f, ERR_INVALID_PARAMETER);
  73. Vector<String> line = f->get_csv_line(delimiter);
  74. ERR_FAIL_COND_V(line.size() <= 1, ERR_PARSE_ERROR);
  75. Vector<String> locales;
  76. Vector<Ref<Translation> > translations;
  77. for (int i = 1; i < line.size(); i++) {
  78. String locale = line[i];
  79. ERR_EXPLAIN("Error importing CSV translation: '" + locale + "' is not a valid locale");
  80. ERR_FAIL_COND_V(!TranslationServer::is_locale_valid(locale), ERR_PARSE_ERROR);
  81. locales.push_back(locale);
  82. Ref<Translation> translation;
  83. translation.instance();
  84. translation->set_locale(locale);
  85. translations.push_back(translation);
  86. }
  87. line = f->get_csv_line(delimiter);
  88. while (line.size() == locales.size() + 1) {
  89. String key = line[0];
  90. if (key != "") {
  91. for (int i = 1; i < line.size(); i++) {
  92. translations.write[i - 1]->add_message(key, line[i]);
  93. }
  94. }
  95. line = f->get_csv_line(delimiter);
  96. }
  97. for (int i = 0; i < translations.size(); i++) {
  98. Ref<Translation> xlt = translations[i];
  99. if (compress) {
  100. Ref<PHashTranslation> cxl = memnew(PHashTranslation);
  101. cxl->generate(xlt);
  102. xlt = cxl;
  103. }
  104. String save_path = p_source_file.get_basename() + "." + translations[i]->get_locale() + ".translation";
  105. ResourceSaver::save(save_path, xlt);
  106. if (r_gen_files) {
  107. r_gen_files->push_back(save_path);
  108. }
  109. }
  110. return OK;
  111. }
  112. ResourceImporterCSVTranslation::ResourceImporterCSVTranslation() {
  113. }