| 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_server.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::INT, "compress", PROPERTY_HINT_ENUM, "Disabled,Auto"), 1)); // Enum for compatibility with previous versions. |
| 72 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0)); |
| 73 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_keys"), false)); |
| 74 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_translations"), true)); |
| 75 | } |
| 76 | |
| 77 | Error ResourceImporterCSVTranslation::import(ResourceUID::ID p_source_id, 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) { |
| 78 | Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ); |
| 79 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_INVALID_PARAMETER, "Cannot open file from path '" + p_source_file + "'."); |
| 80 | |
| 81 | String delimiter; |
| 82 | switch ((int)p_options["delimiter"]) { |
| 83 | case 1: { |
| 84 | delimiter = ";"; |
| 85 | } break; |
| 86 | case 2: { |
| 87 | delimiter = "\t"; |
| 88 | } break; |
| 89 | default: { |
| 90 | delimiter = ","; |
| 91 | } break; |
| 92 | } |
| 93 | |
| 94 | // Parse the header row. |
| 95 | HashMap<int, Ref<Translation>> column_to_translation; |
| 96 | int context_column = -1; |
| 97 | int plural_column = -1; |
| 98 | { |
| 99 | const Vector<String> line = f->get_csv_line(delimiter); |
| 100 | for (int i = 1; i < line.size(); i++) { |
| 101 | if (line[i].left(1) == "_") { |
| 102 | continue; |
| 103 | } |
| 104 | if (line[i].to_lower() == "?context") { |
| 105 | ERR_CONTINUE_MSG(context_column != -1, "Error importing CSV translation: Multiple '?context' columns found. Only one is allowed. Subsequent ones will be ignored."); |
| 106 | context_column = i; |
| 107 | continue; |
| 108 | } |
| 109 | if (line[i].to_lower() == "?plural") { |
| 110 | ERR_CONTINUE_MSG(plural_column != -1, "Error importing CSV translation: Multiple '?plural' columns found. Only one is allowed. Subsequent ones will be ignored."); |
| 111 | plural_column = i; |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | const String locale = TranslationServer::get_singleton()->standardize_locale(line[i]); |
| 116 | ERR_CONTINUE_MSG(locale.is_empty(), vformat("Error importing CSV translation: Invalid locale format '%s', should be 'language_Script_COUNTRY_VARIANT@extra'. This column will be ignored.", line[i])); |
| 117 | |
| 118 | Ref<Translation> translation; |
| 119 | translation.instantiate(); |
| 120 | translation->set_locale(locale); |
| 121 | column_to_translation[i] = translation; |
| 122 | } |
| 123 | |
| 124 | if (column_to_translation.is_empty()) { |
| 125 | WARN_PRINT(vformat("CSV file '%s' does not contain any translation.", p_source_file)); |
| 126 | return OK; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Parse content rows. |
| 131 | bool context_used = false; |
| 132 | bool plural_used = false; |
| 133 | { |
| 134 | const bool unescape_keys = p_options.has("unescape_keys") ? bool(p_options["unescape_keys"]) : false; |
| 135 | const bool unescape_translations = p_options.has("unescape_translations") ? bool(p_options["unescape_translations"]) : true; |
| 136 | |
| 137 | bool reading_plural_rows = false; |
| 138 | String plural_msgid; |
| 139 | String plural_msgctxt; |
| 140 | HashMap<int, Vector<String>> plural_msgstrs; |
| 141 | |
| 142 | do { |
| 143 | const Vector<String> line = f->get_csv_line(delimiter); |
| 144 | |
| 145 | // Skip empty lines. |
| 146 | if (line.size() == 1 && line[0].is_empty()) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | if (line[0].to_lower() == "?pluralrule") { |
| 151 | for (int i = 1; i < line.size(); i++) { |
| 152 | if (line[i].is_empty() || !column_to_translation.has(i)) { |
| 153 | continue; |
| 154 | } |
| 155 | Ref<Translation> translation = column_to_translation[i]; |
| 156 | ERR_CONTINUE_MSG(!translation->get_plural_rules_override().is_empty(), vformat("Error importing CSV translation: Multiple '?pluralrule' definitions found for locale '%s'. Only one is allowed. Subsequent ones will be ignored.", translation->get_locale())); |
| 157 | translation->set_plural_rules_override(line[i]); |
| 158 | } |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | const String msgid = unescape_keys ? line[0].c_unescape() : line[0]; |
| 163 | if (!reading_plural_rows && msgid.is_empty()) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | // It's okay if you define context or plural columns but don't use them. |
| 168 | const String msgctxt = (context_column != -1 && context_column < line.size()) ? line[context_column] : String(); |
| 169 | if (!msgctxt.is_empty()) { |
| 170 | context_used = true; |
| 171 | } |
| 172 | const String msgid_plural = (plural_column != -1 && plural_column < line.size()) ? line[plural_column] : String(); |
| 173 | if (!msgid_plural.is_empty()) { |
| 174 | plural_used = true; |
| 175 | } |
| 176 | |
| 177 | // End of plural rows. |
| 178 | if (reading_plural_rows && (!msgid.is_empty() || !msgctxt.is_empty() || !msgid_plural.is_empty())) { |
| 179 | reading_plural_rows = false; |
| 180 | |
| 181 | for (KeyValue<int, Ref<Translation>> E : column_to_translation) { |
| 182 | Ref<Translation> translation = E.value; |
| 183 | const Vector<String> &msgstrs = plural_msgstrs[E.key]; |
| 184 | if (!msgstrs.is_empty()) { |
| 185 | translation->add_plural_message(plural_msgid, msgstrs, plural_msgctxt); |
| 186 | } |
| 187 | } |
| 188 | plural_msgstrs.clear(); |
| 189 | } |
| 190 | |
| 191 | // Start of plural rows. |
| 192 | if (!reading_plural_rows && !msgid_plural.is_empty()) { |
| 193 | reading_plural_rows = true; |
| 194 | plural_msgid = msgid; |
| 195 | plural_msgctxt = msgctxt; |
| 196 | } |
| 197 | |
| 198 | for (int i = 1; i < line.size(); i++) { |
| 199 | if (!column_to_translation.has(i)) { |
| 200 | continue; |
| 201 | } |
| 202 | const String msgstr = unescape_translations ? line[i].c_unescape() : line[i]; |
| 203 | if (msgstr.is_empty()) { |
| 204 | continue; |
| 205 | } |
| 206 | if (reading_plural_rows) { |
| 207 | plural_msgstrs[i].push_back(msgstr); |
| 208 | } else { |
| 209 | column_to_translation[i]->add_message(msgid, msgstr, msgctxt); |
| 210 | } |
| 211 | } |
| 212 | } while (!f->eof_reached()); |
| 213 | |
| 214 | if (reading_plural_rows) { |
| 215 | for (KeyValue<int, Ref<Translation>> E : column_to_translation) { |
| 216 | Ref<Translation> translation = E.value; |
| 217 | const Vector<String> &msgstrs = plural_msgstrs[E.key]; |
| 218 | if (!msgstrs.is_empty()) { |
| 219 | translation->add_plural_message(plural_msgid, msgstrs, plural_msgctxt); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | bool compress; |
| 226 | switch ((int)p_options["compress"]) { |
| 227 | case 0: { // Disabled. |
| 228 | compress = false; |
| 229 | } break; |
| 230 | default: { // Auto. |
| 231 | compress = !context_used && !plural_used; |
| 232 | } break; |
| 233 | } |
| 234 | |
| 235 | for (KeyValue<int, Ref<Translation>> E : column_to_translation) { |
| 236 | Ref<Translation> xlt = E.value; |
| 237 | |
| 238 | if (compress) { |
| 239 | Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation); |
| 240 | cxl->generate(xlt); |
| 241 | xlt = cxl; |
| 242 | } |
| 243 | |
| 244 | String save_path = p_source_file.get_basename() + "." + xlt->get_locale() + ".translation"; |
| 245 | ResourceUID::ID save_id = hash64_murmur3_64(xlt->get_locale().hash64(), p_source_id) & 0x7FFFFFFFFFFFFFFF; |
| 246 | bool uid_already_exists = ResourceUID::get_singleton()->has_id(save_id); |
| 247 | if (uid_already_exists) { |
| 248 | // Avoid creating a new file with a duplicate UID. |
| 249 | // Always use this UID, even if the user has moved it to a different path. |
| 250 | save_path = ResourceUID::get_singleton()->get_id_path(save_id); |
| 251 | } |
| 252 | |
| 253 | ResourceSaver::save(xlt, save_path); |
| 254 | if (r_gen_files) { |
| 255 | r_gen_files->push_back(save_path); |
| 256 | } |
| 257 | if (!uid_already_exists) { |
| 258 | // No need to call set_uid if save_path already refers to save_id. |
| 259 | ResourceSaver::set_uid(save_path, save_id); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return OK; |
| 264 | } |