resource_importer_csv_translation.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "resource_importer_csv_translation.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/string/optimized_translation.h"
  34. #include "core/string/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_path, const String &p_option, const HashMap<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(const String &p_path, 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 HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  64. bool compress = p_options["compress"];
  65. String delimiter;
  66. switch ((int)p_options["delimiter"]) {
  67. case 0:
  68. delimiter = ",";
  69. break;
  70. case 1:
  71. delimiter = ";";
  72. break;
  73. case 2:
  74. delimiter = "\t";
  75. break;
  76. }
  77. Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ);
  78. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_INVALID_PARAMETER, "Cannot open file from path '" + p_source_file + "'.");
  79. Vector<String> line = f->get_csv_line(delimiter);
  80. ERR_FAIL_COND_V(line.size() <= 1, ERR_PARSE_ERROR);
  81. Vector<String> locales;
  82. Vector<Ref<Translation>> translations;
  83. for (int i = 1; i < line.size(); i++) {
  84. String locale = TranslationServer::get_singleton()->standardize_locale(line[i]);
  85. locales.push_back(locale);
  86. Ref<Translation> translation;
  87. translation.instantiate();
  88. translation->set_locale(locale);
  89. translations.push_back(translation);
  90. }
  91. do {
  92. line = f->get_csv_line(delimiter);
  93. String key = line[0];
  94. if (!key.is_empty()) {
  95. ERR_FAIL_COND_V_MSG(line.size() != locales.size() + 1, ERR_PARSE_ERROR, vformat("Error importing CSV translation: expected %d locale(s), but the '%s' key has %d locale(s).", locales.size(), key, line.size() - 1));
  96. for (int i = 1; i < line.size(); i++) {
  97. translations.write[i - 1]->add_message(key, line[i].c_unescape());
  98. }
  99. }
  100. } while (!f->eof_reached());
  101. for (int i = 0; i < translations.size(); i++) {
  102. Ref<Translation> xlt = translations[i];
  103. if (compress) {
  104. Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation);
  105. cxl->generate(xlt);
  106. xlt = cxl;
  107. }
  108. String save_path = p_source_file.get_basename() + "." + translations[i]->get_locale() + ".translation";
  109. ResourceSaver::save(xlt, save_path);
  110. if (r_gen_files) {
  111. r_gen_files->push_back(save_path);
  112. }
  113. }
  114. return OK;
  115. }
  116. ResourceImporterCSVTranslation::ResourceImporterCSVTranslation() {
  117. }