Skip to content

Commit

Permalink
improve translation key guessing
Browse files Browse the repository at this point in the history
We still don't recover many/most of the keys, but we at least get some in
most projects that I've tested this with, which will enable people
to search through the project and add the rest
  • Loading branch information
nikitalita committed Dec 13, 2022
1 parent 1257853 commit 34316a3
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions utility/import_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,42 @@ Error ImportExporter::recreate_plugin_configs(const String &output_dir) {
return OK;
}

#define TEST_TR_KEY(key) \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
} \
key = key.to_upper(); \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
} \
key = key.to_lower(); \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
}

String guess_key_from_tr(String s, Ref<Translation> default_translation) {
static const Vector<String> prefixes = { "$$", "##", "TR_", "KEY_TEXT_" };
String key = s;
String test;
TEST_TR_KEY(key);
String str = s;
//remove punctuation
str = str.replace("\n", "").replace(".", "").replace("", "").replace("!", "").replace("?", "");
str = str.strip_escapes().strip_edges().replace(" ", "_");
key = str;
TEST_TR_KEY(key);
// Try adding prefixes
for (String prefix : prefixes) {
key = prefix + str;
TEST_TR_KEY(key);
}
// failed
return "";
}

Error ImportExporter::export_translation(const String &output_dir, Ref<ImportInfo> &iinfo) {
Error err;
ResourceFormatLoaderCompat rlc;
Expand Down Expand Up @@ -490,42 +526,16 @@ Error ImportExporter::export_translation(const String &output_dir, Ref<ImportInf
translations.push_back(tr);
}
// We can't recover the keys from Optimized translations, we have to guess
Vector<String> prefixes = { "$$", "##", "TR_" };
int missing_keys = 0;
if (keys.size() == 0) {
for (const StringName &s : default_messages) {
String key = s;
String test = default_translation->get_message(key);
if (test == s) {
keys.push_back(key);
continue;
}
// Try adding "$$", "##", or "TR_"
for (String prefix : prefixes) {
key = prefix + s;
test = default_translation->get_message(key);
if (test == s) {
break;
}
}
if (test == s) {
keys.push_back(key);
continue;
}
// this is another common format for popular translation exporters
String str = s;
//remove punctuation
str = str.replace("\n", "").replace(".", "").replace("", "").replace("!", "").replace("?", "").replace("-", "");
str = str.strip_escapes().strip_edges().replace(" ", "_");
key = "KEY_TEXT_" + str.to_upper();
test = default_translation->get_message(key);
if (test == s) {
String key = guess_key_from_tr(s, default_translation);
if (key.is_empty()) {
missing_keys++;
keys.push_back("<MISSING KEY " + s + ">");
} else {
keys.push_back(key);
continue;
}
// if we've hit here, we didn't find the key
missing_keys++;
keys.push_back("<MISSING KEY " + s + ">");
}
}
header += "\n";
Expand Down

0 comments on commit 34316a3

Please sign in to comment.