Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Backport locale selection improvements. #61878

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/global_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,8 @@ void register_global_constants() {
BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSY);
BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS);

BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_LOCALE_ID);

BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_USAGE_STORAGE);
BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR);
BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_USAGE_NETWORK);
Expand Down
26 changes: 7 additions & 19 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,38 +721,26 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem

// 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).

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;

int best_score = 0;
for (int i = 0; i < res_remaps.size(); i++) {
int split = res_remaps[i].rfind(":");
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;
int score = TranslationServer::get_singleton()->compare_locales(locale, l);
if (score > 0 && score >= best_score) {
new_path = res_remaps[i].left(split);
continue;
best_score = score;
if (score == 10) {
break; // Exact match, skip the rest.
}
}
}

Expand Down
Loading