Add unescape import options for CSV Translation

This commit is contained in:
Haoyu Qiu 2024-10-19 16:38:09 +08:00
parent b3bcb2dc14
commit 57ed90f59c
2 changed files with 13 additions and 3 deletions

View file

@ -24,5 +24,11 @@
<member name="delimiter" type="int" setter="" getter="" default="0">
The delimiter to use in the CSV file. The default value matches the common CSV convention. Tab-separated values are sometimes called TSV files.
</member>
<member name="unescape_keys" type="bool" setter="" getter="" default="false">
If [code]true[/code], message keys in the CSV file are unescaped using [method String.c_unescape] during the import process.
</member>
<member name="unescape_translations" type="bool" setter="" getter="" default="true">
If [code]true[/code], message translations in the CSV file are unescaped using [method String.c_unescape] during the import process.
</member>
</members>
</class>

View file

@ -70,10 +70,14 @@ String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const {
void ResourceImporterCSVTranslation::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_keys"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_translations"), true));
}
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) {
bool compress = p_options["compress"];
const bool compress = p_options["compress"];
const bool unescape_keys = p_options.has("unescape_keys") ? bool(p_options["unescape_keys"]) : false;
const bool unescape_translations = p_options.has("unescape_translations") ? bool(p_options["unescape_translations"]) : true;
String delimiter;
switch ((int)p_options["delimiter"]) {
@ -118,7 +122,7 @@ Error ResourceImporterCSVTranslation::import(const String &p_source_file, const
do {
line = f->get_csv_line(delimiter);
String key = line[0];
const String key = unescape_keys ? line[0].c_unescape() : line[0];
if (!key.is_empty()) {
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));
@ -127,7 +131,7 @@ Error ResourceImporterCSVTranslation::import(const String &p_source_file, const
if (skipped_locales.has(i)) {
continue;
}
translations.write[write_index++]->add_message(key, line[i].c_unescape());
translations.write[write_index++]->add_message(key, unescape_translations ? line[i].c_unescape() : line[i]);
}
}
} while (!f->eof_reached());