Browse Source

Interleave non-regional fallback locales after last regional variation

pull/1524/head
ephphatha 4 years ago committed by Anders Jenbo
parent
commit
a1652bd5d6
  1. 30
      Source/options.cpp
  2. 3
      Source/utils/language.cpp

30
Source/options.cpp

@ -986,6 +986,17 @@ void OptionEntryLanguageCode::LoadFromIni(string_view category)
locales.emplace_back(std::locale("").name().substr(0, 5));
#endif
// Insert non-regional locale codes after the last regional variation so we fallback to neutral translations if no regional translation exists that meets user preferences.
for (auto localeIter = locales.rbegin(); localeIter != locales.rend(); localeIter++) {
auto regionSeparator = localeIter->find('_');
if (regionSeparator != std::string::npos) {
std::string neutralLocale = localeIter->substr(0, regionSeparator);
if (std::find(locales.rbegin(), localeIter, neutralLocale) == localeIter) {
localeIter = std::make_reverse_iterator(locales.insert(localeIter.base(), neutralLocale));
}
}
}
LogVerbose("Found {} user preferred locales", locales);
for (const auto &locale : locales) {
@ -997,25 +1008,6 @@ void OptionEntryLanguageCode::LoadFromIni(string_view category)
}
}
std::vector<std::string> fallbackLocales;
fallbackLocales.reserve(locales.size());
for (const auto &locale : locales) {
std::string neutralLocale = locale.substr(0, locale.find('_'));
if (std::find(fallbackLocales.cbegin(), fallbackLocales.cend(), neutralLocale) == fallbackLocales.cend()) {
fallbackLocales.push_back(neutralLocale);
}
}
for (const auto &fallbackLocale : fallbackLocales) {
LogVerbose("Trying to load fallback translation: {}", fallbackLocale);
if (HasTranslation(fallbackLocale)) {
LogVerbose("Found matching fallback locale: {}", fallbackLocale);
CopyUtf8(szCode, fallbackLocale, sizeof(szCode));
return;
}
}
LogVerbose("No suitable translation found");
strcpy(szCode, "en");
}

3
Source/utils/language.cpp

@ -270,7 +270,8 @@ const std::string &LanguageTranslate(const char *key)
bool HasTranslation(const std::string &locale)
{
if (locale == "en") {
if (locale == "en" || locale == "en_US") {
// the base translation is en_US. No translation file will be present for these codes but we want the check to succeed to prevent further searches.
return true;
}

Loading…
Cancel
Save