| 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 |  | 
|---|
| 31 | #include "resource_importer_csv_translation.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/io/file_access.h" | 
|---|
| 34 | #include "core/io/resource_saver.h" | 
|---|
| 35 | #include "core/string/optimized_translation.h" | 
|---|
| 36 | #include "core/string/translation.h" | 
|---|
| 37 |  | 
|---|
| 38 | String ResourceImporterCSVTranslation::get_importer_name() const { | 
|---|
| 39 | return "csv_translation"; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | String ResourceImporterCSVTranslation::get_visible_name() const { | 
|---|
| 43 | return "CSV Translation"; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | void ResourceImporterCSVTranslation::get_recognized_extensions(List<String> *p_extensions) const { | 
|---|
| 47 | p_extensions->push_back( "csv"); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | String ResourceImporterCSVTranslation::get_save_extension() const { | 
|---|
| 51 | return ""; //does not save a single resource | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | String ResourceImporterCSVTranslation::get_resource_type() const { | 
|---|
| 55 | return "Translation"; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | bool ResourceImporterCSVTranslation::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const { | 
|---|
| 59 | return true; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | int ResourceImporterCSVTranslation::get_preset_count() const { | 
|---|
| 63 | return 0; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const { | 
|---|
| 67 | return ""; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void ResourceImporterCSVTranslation::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const { | 
|---|
| 71 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true)); | 
|---|
| 72 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0)); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | 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) { | 
|---|
| 76 | bool compress = p_options[ "compress"]; | 
|---|
| 77 |  | 
|---|
| 78 | String delimiter; | 
|---|
| 79 | switch ((int)p_options[ "delimiter"]) { | 
|---|
| 80 | case 0: | 
|---|
| 81 | delimiter = ","; | 
|---|
| 82 | break; | 
|---|
| 83 | case 1: | 
|---|
| 84 | delimiter = ";"; | 
|---|
| 85 | break; | 
|---|
| 86 | case 2: | 
|---|
| 87 | delimiter = "\t"; | 
|---|
| 88 | break; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ); | 
|---|
| 92 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_INVALID_PARAMETER, "Cannot open file from path '"+ p_source_file + "'."); | 
|---|
| 93 |  | 
|---|
| 94 | Vector<String> line = f->get_csv_line(delimiter); | 
|---|
| 95 | ERR_FAIL_COND_V(line.size() <= 1, ERR_PARSE_ERROR); | 
|---|
| 96 |  | 
|---|
| 97 | Vector<String> locales; | 
|---|
| 98 | Vector<Ref<Translation>> translations; | 
|---|
| 99 |  | 
|---|
| 100 | for (int i = 1; i < line.size(); i++) { | 
|---|
| 101 | String locale = TranslationServer::get_singleton()->standardize_locale(line[i]); | 
|---|
| 102 |  | 
|---|
| 103 | locales.push_back(locale); | 
|---|
| 104 | Ref<Translation> translation; | 
|---|
| 105 | translation.instantiate(); | 
|---|
| 106 | translation->set_locale(locale); | 
|---|
| 107 | translations.push_back(translation); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | do { | 
|---|
| 111 | line = f->get_csv_line(delimiter); | 
|---|
| 112 | String key = line[0]; | 
|---|
| 113 | if (!key.is_empty()) { | 
|---|
| 114 | 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)); | 
|---|
| 115 |  | 
|---|
| 116 | for (int i = 1; i < line.size(); i++) { | 
|---|
| 117 | translations.write[i - 1]->add_message(key, line[i].c_unescape()); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | } while (!f->eof_reached()); | 
|---|
| 121 |  | 
|---|
| 122 | for (int i = 0; i < translations.size(); i++) { | 
|---|
| 123 | Ref<Translation> xlt = translations[i]; | 
|---|
| 124 |  | 
|---|
| 125 | if (compress) { | 
|---|
| 126 | Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation); | 
|---|
| 127 | cxl->generate(xlt); | 
|---|
| 128 | xlt = cxl; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | String save_path = p_source_file.get_basename() + "."+ translations[i]->get_locale() + ".translation"; | 
|---|
| 132 |  | 
|---|
| 133 | ResourceSaver::save(xlt, save_path); | 
|---|
| 134 | if (r_gen_files) { | 
|---|
| 135 | r_gen_files->push_back(save_path); | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | return OK; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | ResourceImporterCSVTranslation::ResourceImporterCSVTranslation() { | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|