Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/import/resource_importer_csv_translation.cpp
9896 views
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::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(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) {
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
HashSet<int> skipped_locales;
100
101
for (int i = 1; i < line.size(); i++) {
102
String locale = TranslationServer::get_singleton()->standardize_locale(line[i]);
103
104
if (line[i].left(1) == "_") {
105
skipped_locales.insert(i);
106
continue;
107
} else if (locale.is_empty()) {
108
skipped_locales.insert(i);
109
ERR_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]));
110
}
111
112
locales.push_back(locale);
113
Ref<Translation> translation;
114
translation.instantiate();
115
translation->set_locale(locale);
116
translations.push_back(translation);
117
}
118
119
do {
120
line = f->get_csv_line(delimiter);
121
String key = line[0];
122
if (!key.is_empty()) {
123
ERR_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));
124
125
int write_index = 0; // Keep track of translations written in case some locales are skipped.
126
for (int i = 1; i < line.size(); i++) {
127
if (skipped_locales.has(i)) {
128
continue;
129
}
130
translations.write[write_index++]->add_message(key, line[i].c_unescape());
131
}
132
}
133
} while (!f->eof_reached());
134
135
for (int i = 0; i < translations.size(); i++) {
136
Ref<Translation> xlt = translations[i];
137
138
if (compress) {
139
Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation);
140
cxl->generate(xlt);
141
xlt = cxl;
142
}
143
144
String save_path = p_source_file.get_basename() + "." + translations[i]->get_locale() + ".translation";
145
ResourceUID::ID save_id = hash64_murmur3_64(translations[i]->get_locale().hash64(), p_source_id) & 0x7FFFFFFFFFFFFFFF;
146
bool uid_already_exists = ResourceUID::get_singleton()->has_id(save_id);
147
if (uid_already_exists) {
148
// Avoid creating a new file with a duplicate UID.
149
// Always use this UID, even if the user has moved it to a different path.
150
save_path = ResourceUID::get_singleton()->get_id_path(save_id);
151
}
152
153
ResourceSaver::save(xlt, save_path);
154
if (r_gen_files) {
155
r_gen_files->push_back(save_path);
156
}
157
if (!uid_already_exists) {
158
// No need to call set_uid if save_path already refers to save_id.
159
ResourceSaver::set_uid(save_path, save_id);
160
}
161
}
162
163
return OK;
164
}
165
166