Skip to content

Commit

Permalink
Merge pull request #61878 from bruvzg/backport_locale_select
Browse files Browse the repository at this point in the history
[3.x] Backport locale selection improvements.
  • Loading branch information
akien-mga authored Aug 5, 2022
2 parents 14c9325 + a8eb779 commit 0dccbcd
Show file tree
Hide file tree
Showing 19 changed files with 2,419 additions and 1,203 deletions.
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 @@ -734,38 +734,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

0 comments on commit 0dccbcd

Please sign in to comment.