Path: blob/master/editor/import/resource_importer_csv_translation.cpp
9896 views
/**************************************************************************/1/* resource_importer_csv_translation.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_importer_csv_translation.h"3132#include "core/io/file_access.h"33#include "core/io/resource_saver.h"34#include "core/string/optimized_translation.h"35#include "core/string/translation_server.h"3637String ResourceImporterCSVTranslation::get_importer_name() const {38return "csv_translation";39}4041String ResourceImporterCSVTranslation::get_visible_name() const {42return "CSV Translation";43}4445void ResourceImporterCSVTranslation::get_recognized_extensions(List<String> *p_extensions) const {46p_extensions->push_back("csv");47}4849String ResourceImporterCSVTranslation::get_save_extension() const {50return ""; //does not save a single resource51}5253String ResourceImporterCSVTranslation::get_resource_type() const {54return "Translation";55}5657bool ResourceImporterCSVTranslation::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {58return true;59}6061int ResourceImporterCSVTranslation::get_preset_count() const {62return 0;63}6465String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const {66return "";67}6869void ResourceImporterCSVTranslation::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {70r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));71r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0));72}7374Error 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) {75bool compress = p_options["compress"];7677String delimiter;78switch ((int)p_options["delimiter"]) {79case 0:80delimiter = ",";81break;82case 1:83delimiter = ";";84break;85case 2:86delimiter = "\t";87break;88}8990Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ);91ERR_FAIL_COND_V_MSG(f.is_null(), ERR_INVALID_PARAMETER, "Cannot open file from path '" + p_source_file + "'.");9293Vector<String> line = f->get_csv_line(delimiter);94ERR_FAIL_COND_V(line.size() <= 1, ERR_PARSE_ERROR);9596Vector<String> locales;97Vector<Ref<Translation>> translations;98HashSet<int> skipped_locales;99100for (int i = 1; i < line.size(); i++) {101String locale = TranslationServer::get_singleton()->standardize_locale(line[i]);102103if (line[i].left(1) == "_") {104skipped_locales.insert(i);105continue;106} else if (locale.is_empty()) {107skipped_locales.insert(i);108ERR_CONTINUE_MSG(true, vformat("Error importing CSV translation: Invalid locale format '%s', should be 'language_Script_COUNTRY_VARIANT@extra'. This column will be ignored.", line[i]));109}110111locales.push_back(locale);112Ref<Translation> translation;113translation.instantiate();114translation->set_locale(locale);115translations.push_back(translation);116}117118do {119line = f->get_csv_line(delimiter);120String key = line[0];121if (!key.is_empty()) {122ERR_CONTINUE_MSG(line.size() != locales.size() + (int)skipped_locales.size() + 1, vformat("Error importing CSV translation: expected %d locale(s), but the '%s' key has %d locale(s).", locales.size(), key, line.size() - 1));123124int write_index = 0; // Keep track of translations written in case some locales are skipped.125for (int i = 1; i < line.size(); i++) {126if (skipped_locales.has(i)) {127continue;128}129translations.write[write_index++]->add_message(key, line[i].c_unescape());130}131}132} while (!f->eof_reached());133134for (int i = 0; i < translations.size(); i++) {135Ref<Translation> xlt = translations[i];136137if (compress) {138Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation);139cxl->generate(xlt);140xlt = cxl;141}142143String save_path = p_source_file.get_basename() + "." + translations[i]->get_locale() + ".translation";144ResourceUID::ID save_id = hash64_murmur3_64(translations[i]->get_locale().hash64(), p_source_id) & 0x7FFFFFFFFFFFFFFF;145bool uid_already_exists = ResourceUID::get_singleton()->has_id(save_id);146if (uid_already_exists) {147// Avoid creating a new file with a duplicate UID.148// Always use this UID, even if the user has moved it to a different path.149save_path = ResourceUID::get_singleton()->get_id_path(save_id);150}151152ResourceSaver::save(xlt, save_path);153if (r_gen_files) {154r_gen_files->push_back(save_path);155}156if (!uid_already_exists) {157// No need to call set_uid if save_path already refers to save_id.158ResourceSaver::set_uid(save_path, save_id);159}160}161162return OK;163}164165166