diff --git a/src/openrct2/localisation/LocalisationService.cpp b/src/openrct2/localisation/LocalisationService.cpp index 898137dd59..eee05c05e4 100644 --- a/src/openrct2/localisation/LocalisationService.cpp +++ b/src/openrct2/localisation/LocalisationService.cpp @@ -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& LocalisationService::GetLanguageOrder() const +{ + return _languageOrder; +} + int32_t LocalisationService_GetCurrentLanguage() { const auto& localisationService = GetContext()->GetLocalisationService(); diff --git a/src/openrct2/localisation/LocalisationService.h b/src/openrct2/localisation/LocalisationService.h index 2e45964459..d7b76b4c52 100644 --- a/src/openrct2/localisation/LocalisationService.h +++ b/src/openrct2/localisation/LocalisationService.h @@ -34,6 +34,7 @@ namespace OpenRCT2::Localisation const std::shared_ptr _env; int32_t _currentLanguage{}; bool _useTrueTypeFont{}; + std::vector _languageOrder; std::vector> _loadedLanguages; std::stack _availableObjectStringIds; std::vector _objectStrings; @@ -65,6 +66,7 @@ namespace OpenRCT2::Localisation void CloseLanguages(); StringId AllocateObjectString(const std::string& target); void FreeObjectString(StringId stringId); + const std::vector& GetLanguageOrder() const; }; } // namespace OpenRCT2::Localisation diff --git a/src/openrct2/object/StringTable.cpp b/src/openrct2/object/StringTable.cpp index 6e97a8dd33..ad0cda0682 100644 --- a/src/openrct2/object/StringTable.cpp +++ b/src/openrct2/object/StringTable.cpp @@ -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; + }); }