From 34316a355cf2f562f1d769ef8677cb51de6fffde Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:27:27 -0800 Subject: [PATCH] improve translation key guessing 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 --- utility/import_exporter.cpp | 72 +++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/utility/import_exporter.cpp b/utility/import_exporter.cpp index a14ee41b..bfc60f6a 100644 --- a/utility/import_exporter.cpp +++ b/utility/import_exporter.cpp @@ -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 default_translation) { + static const Vector 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 &iinfo) { Error err; ResourceFormatLoaderCompat rlc; @@ -490,42 +526,16 @@ Error ImportExporter::export_translation(const String &output_dir, Ref 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(""); + } else { keys.push_back(key); - continue; } - // if we've hit here, we didn't find the key - missing_keys++; - keys.push_back(""); } } header += "\n";