diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2019-12-04 16:50:43 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2019-12-04 16:53:40 +0100 |
commit | 95242b7faf7f2e1552b4921a8bbdb519d3a898bd (patch) | |
tree | 0b901c6933c62b8e085750ff6af1b0481d135dfb | |
parent | 0fcb68ffa1eef21c55794e896ddd9dd2e7b74751 (diff) |
ResourceLoader: Add language code matching for localized resources
Near matching was not implemented like in TranslationServer, so a
resource remapped for 'ru' (but not 'ru_RU') would not be used as
fallback if the system locale was 'ru_RU'.
Fixes #34058.
-rw-r--r-- | core/io/resource_loader.cpp | 53 | ||||
-rw-r--r-- | core/translation.cpp | 3 |
2 files changed, 41 insertions, 15 deletions
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index f3eba44973..6f64543b3e 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -734,26 +734,49 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem String new_path = p_path; - if (translation_remaps.has(new_path)) { + if (translation_remaps.has(p_path)) { + // translation_remaps has the following format: + // { "res://path.png": PoolStringArray( "res://path-ru.png:ru", "res://path-de.png:de" ) } + + // To find the path of the remapped resource, we extract the locale name after + // the last ':' to match the project locale. + // We also fall back in case of regional locales as done in TranslationServer::translate + // (e.g. 'ru_RU' -> 'ru' if the former has no specific mapping). - Vector<String> &v = *translation_remaps.getptr(new_path); String locale = TranslationServer::get_singleton()->get_locale(); - if (r_translation_remapped) { - *r_translation_remapped = true; - } - for (int i = 0; i < v.size(); i++) { + ERR_FAIL_COND_V_MSG(locale.length() < 2, p_path, "Could not remap path '" + p_path + "' for translation as configured locale '" + locale + "' is invalid."); + String lang = TranslationServer::get_language_code(locale); - int split = v[i].find_last(":"); - if (split == -1) - continue; - String l = v[i].right(split + 1).strip_edges(); - if (l == String()) + Vector<String> &res_remaps = *translation_remaps.getptr(new_path); + bool near_match = false; + + for (int i = 0; i < res_remaps.size(); i++) { + int split = res_remaps[i].find_last(":"); + if (split == -1) { continue; + } - if (l.begins_with(locale)) { - new_path = v[i].left(split); + String l = res_remaps[i].right(split + 1).strip_edges(); + if (l == locale) { // Exact match. + new_path = res_remaps[i].left(split); break; + } else if (near_match) { + continue; // Already found near match, keep going for potential exact match. } + + // No exact match (e.g. locale 'ru_RU' but remap is 'ru'), let's look further + // for a near match (same language code, i.e. first 2 or 3 letters before + // regional code, if included). + if (TranslationServer::get_language_code(l) == lang) { + // Language code matches, that's a near match. Keep looking for exact match. + near_match = true; + new_path = res_remaps[i].left(split); + continue; + } + } + + if (r_translation_remapped) { + *r_translation_remapped = true; } } @@ -761,8 +784,8 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem new_path = path_remaps[new_path]; } - if (new_path == p_path) { //did not remap - //try file remap + if (new_path == p_path) { // Did not remap. + // Try file remap. Error err; FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err); diff --git a/core/translation.cpp b/core/translation.cpp index 125ed8ba16..4a662b2590 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -1065,6 +1065,9 @@ StringName TranslationServer::translate(const StringName &p_message) const { // form. If not found, we fall back to a near match (another locale with // same language code). + // Note: ResourceLoader::_path_remap reproduces this locale near matching + // logic, so be sure to propagate changes there when changing things here. + StringName res; String lang = get_language_code(locale); bool near_match = false; |