Allow cascaded fallback languages

This commit is contained in:
Gymnasiast 2023-01-03 22:21:16 +01:00
parent 08270d7d31
commit 43988aa50f
No known key found for this signature in database
GPG Key ID: DBFFF47AB2CA3EDD
3 changed files with 43 additions and 45 deletions

View File

@ -100,6 +100,7 @@ void LocalisationService::OpenLanguage(int32_t id)
if (preferredLanguage != nullptr)
{
_currentLanguage = id;
_languageOrder.emplace_back(id);
_loadedLanguages.emplace_back(std::move(preferredLanguage));
TryLoadFonts(*this);
}
@ -108,15 +109,21 @@ void LocalisationService::OpenLanguage(int32_t id)
throw std::runtime_error("Unable to open language " + std::to_string(id));
}
const auto fallback = LanguagesDescriptors[id].fallback;
if (fallback != LANGUAGE_UNDEFINED)
auto checkLanguage = LanguagesDescriptors[id].fallback;
while (true)
{
filename = GetLanguagePath(fallback);
_loadedLanguages.emplace_back(LanguagePackFactory::FromFile(fallback, filename.c_str()));
if (checkLanguage == LANGUAGE_UNDEFINED)
break;
_languageOrder.emplace_back(checkLanguage);
filename = GetLanguagePath(checkLanguage);
_loadedLanguages.emplace_back(LanguagePackFactory::FromFile(checkLanguage, filename.c_str()));
checkLanguage = LanguagesDescriptors[checkLanguage].fallback;
}
if (id != LANGUAGE_ENGLISH_UK)
{
_languageOrder.emplace_back(LANGUAGE_ENGLISH_UK);
filename = GetLanguagePath(LANGUAGE_ENGLISH_UK);
_loadedLanguages.emplace_back(LanguagePackFactory::FromFile(LANGUAGE_ENGLISH_UK, filename.c_str()));
}
@ -128,6 +135,7 @@ void LocalisationService::CloseLanguages()
{
language = nullptr;
}
_languageOrder.clear();
_loadedLanguages.clear();
_currentLanguage = LANGUAGE_UNDEFINED;
}
@ -183,6 +191,11 @@ void LocalisationService::FreeObjectString(StringId stringId)
}
}
const std::vector<int32_t>& LocalisationService::GetLanguageOrder() const
{
return _languageOrder;
}
int32_t LocalisationService_GetCurrentLanguage()
{
const auto& localisationService = GetContext()->GetLocalisationService();

View File

@ -34,6 +34,7 @@ namespace OpenRCT2::Localisation
const std::shared_ptr<IPlatformEnvironment> _env;
int32_t _currentLanguage{};
bool _useTrueTypeFont{};
std::vector<int32_t> _languageOrder;
std::vector<std::unique_ptr<ILanguagePack>> _loadedLanguages;
std::stack<StringId> _availableObjectStringIds;
std::vector<std::string> _objectStrings;
@ -65,6 +66,7 @@ namespace OpenRCT2::Localisation
void CloseLanguages();
StringId AllocateObjectString(const std::string& target);
void FreeObjectString(StringId stringId);
const std::vector<int32_t>& GetLanguageOrder() const;
};
} // namespace OpenRCT2::Localisation

View File

@ -9,6 +9,7 @@
#include "StringTable.h"
#include "../Context.h"
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../core/String.hpp"
@ -154,47 +155,29 @@ void StringTable::SetString(ObjectStringID id, uint8_t language, const std::stri
void StringTable::Sort()
{
auto targetLanguage = LocalisationService_GetCurrentLanguage();
auto fallbackLanguage = LanguagesDescriptors[targetLanguage].fallback;
std::sort(
_strings.begin(), _strings.end(),
[targetLanguage, fallbackLanguage](const StringTableEntry& a, const StringTableEntry& b) -> bool {
if (a.Id == b.Id)
const auto& languageOrder = OpenRCT2::GetContext()->GetLocalisationService().GetLanguageOrder();
std::sort(_strings.begin(), _strings.end(), [languageOrder](const StringTableEntry& a, const StringTableEntry& b) -> bool {
if (a.Id == b.Id)
{
if (a.LanguageId == b.LanguageId)
{
if (a.LanguageId == b.LanguageId)
{
return String::Compare(a.Text, b.Text, true) < 0;
}
if (a.LanguageId == targetLanguage)
{
return true;
}
if (b.LanguageId == targetLanguage)
{
return false;
}
if (fallbackLanguage != LANGUAGE_UNDEFINED)
{
if (a.LanguageId == fallbackLanguage)
return true;
if (b.LanguageId == fallbackLanguage)
return false;
}
if (a.LanguageId == LANGUAGE_ENGLISH_UK)
{
return true;
}
if (b.LanguageId == LANGUAGE_ENGLISH_UK)
{
return false;
}
return a.LanguageId < b.LanguageId;
return String::Compare(a.Text, b.Text, true) < 0;
}
return a.Id < b.Id;
});
for (const auto& language : languageOrder)
{
if (a.LanguageId == language)
{
return true;
}
if (b.LanguageId == language)
{
return false;
}
}
return a.LanguageId < b.LanguageId;
}
return a.Id < b.Id;
});
}