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) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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/compressed_translation.h"
34 #include "core/io/resource_saver.h"
35 #include "core/os/file_access.h"
36 #include "core/translation.h"
37
get_importer_name() const38 String ResourceImporterCSVTranslation::get_importer_name() const {
39
40 return "csv_translation";
41 }
42
get_visible_name() const43 String ResourceImporterCSVTranslation::get_visible_name() const {
44
45 return "CSV Translation";
46 }
get_recognized_extensions(List<String> * p_extensions) const47 void ResourceImporterCSVTranslation::get_recognized_extensions(List<String> *p_extensions) const {
48
49 p_extensions->push_back("csv");
50 }
51
get_save_extension() const52 String ResourceImporterCSVTranslation::get_save_extension() const {
53 return ""; //does not save a single resource
54 }
55
get_resource_type() const56 String ResourceImporterCSVTranslation::get_resource_type() const {
57
58 return "Translation";
59 }
60
get_option_visibility(const String & p_option,const Map<StringName,Variant> & p_options) const61 bool ResourceImporterCSVTranslation::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
62
63 return true;
64 }
65
get_preset_count() const66 int ResourceImporterCSVTranslation::get_preset_count() const {
67 return 0;
68 }
get_preset_name(int p_idx) const69 String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const {
70
71 return "";
72 }
73
get_import_options(List<ImportOption> * r_options,int p_preset) const74 void ResourceImporterCSVTranslation::get_import_options(List<ImportOption> *r_options, int p_preset) const {
75
76 r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
77 r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0));
78 }
79
import(const String & p_source_file,const String & p_save_path,const Map<StringName,Variant> & p_options,List<String> * r_platform_variants,List<String> * r_gen_files,Variant * r_metadata)80 Error ResourceImporterCSVTranslation::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
81
82 bool compress = p_options["compress"];
83
84 String delimiter;
85 switch ((int)p_options["delimiter"]) {
86 case 0: delimiter = ","; break;
87 case 1: delimiter = ";"; break;
88 case 2: delimiter = "\t"; break;
89 }
90
91 FileAccessRef f = FileAccess::open(p_source_file, FileAccess::READ);
92
93 ERR_FAIL_COND_V_MSG(!f, ERR_INVALID_PARAMETER, "Cannot open file from path '" + p_source_file + "'.");
94
95 Vector<String> line = f->get_csv_line(delimiter);
96 ERR_FAIL_COND_V(line.size() <= 1, ERR_PARSE_ERROR);
97
98 Vector<String> locales;
99 Vector<Ref<Translation> > translations;
100
101 for (int i = 1; i < line.size(); i++) {
102
103 String locale = line[i];
104 ERR_FAIL_COND_V_MSG(!TranslationServer::is_locale_valid(locale), ERR_PARSE_ERROR, "Error importing CSV translation: '" + locale + "' is not a valid locale.");
105
106 locales.push_back(locale);
107 Ref<Translation> translation;
108 translation.instance();
109 translation->set_locale(locale);
110 translations.push_back(translation);
111 }
112
113 line = f->get_csv_line(delimiter);
114
115 while (line.size() == locales.size() + 1) {
116
117 String key = line[0];
118 if (key != "") {
119
120 for (int i = 1; i < line.size(); i++) {
121 translations.write[i - 1]->add_message(key, line[i].c_unescape());
122 }
123 }
124
125 line = f->get_csv_line(delimiter);
126 }
127
128 for (int i = 0; i < translations.size(); i++) {
129 Ref<Translation> xlt = translations[i];
130
131 if (compress) {
132 Ref<PHashTranslation> cxl = memnew(PHashTranslation);
133 cxl->generate(xlt);
134 xlt = cxl;
135 }
136
137 String save_path = p_source_file.get_basename() + "." + translations[i]->get_locale() + ".translation";
138
139 ResourceSaver::save(save_path, xlt);
140 if (r_gen_files) {
141 r_gen_files->push_back(save_path);
142 }
143 }
144
145 return OK;
146 }
147
ResourceImporterCSVTranslation()148 ResourceImporterCSVTranslation::ResourceImporterCSVTranslation() {
149 }
150