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.
This commit is contained in:
parent
0fcb68ffa1
commit
95242b7faf
2 changed files with 44 additions and 18 deletions
|
@ -734,35 +734,58 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem
|
||||||
|
|
||||||
String new_path = p_path;
|
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();
|
String locale = TranslationServer::get_singleton()->get_locale();
|
||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
if (r_translation_remapped) {
|
||||||
*r_translation_remapped = true;
|
*r_translation_remapped = true;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < v.size(); i++) {
|
|
||||||
|
|
||||||
int split = v[i].find_last(":");
|
|
||||||
if (split == -1)
|
|
||||||
continue;
|
|
||||||
String l = v[i].right(split + 1).strip_edges();
|
|
||||||
if (l == String())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (l.begins_with(locale)) {
|
|
||||||
new_path = v[i].left(split);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path_remaps.has(new_path)) {
|
if (path_remaps.has(new_path)) {
|
||||||
new_path = path_remaps[new_path];
|
new_path = path_remaps[new_path];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (new_path == p_path) { //did not remap
|
if (new_path == p_path) { // Did not remap.
|
||||||
//try file remap
|
// Try file remap.
|
||||||
Error err;
|
Error err;
|
||||||
FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err);
|
FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err);
|
||||||
|
|
||||||
|
|
|
@ -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
|
// form. If not found, we fall back to a near match (another locale with
|
||||||
// same language code).
|
// 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;
|
StringName res;
|
||||||
String lang = get_language_code(locale);
|
String lang = get_language_code(locale);
|
||||||
bool near_match = false;
|
bool near_match = false;
|
||||||
|
|
Loading…
Reference in a new issue