Files
OpenRCT2-Web/src/openrct2/localisation/LocalisationService.cpp
T

191 lines
5.4 KiB
C++
Raw Normal View History

2018-04-24 13:18:05 +01:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-04-24 13:18:05 +01:00
2018-06-22 23:01:56 +02:00
#include "LocalisationService.h"
2018-04-24 13:18:05 +01:00
#include "../Context.h"
2018-06-22 23:01:56 +02:00
#include "../PlatformEnvironment.h"
2018-04-24 13:18:05 +01:00
#include "../core/Path.hpp"
#include "../interface/Fonts.h"
#include "Language.h"
#include "LanguagePack.h"
2018-06-22 23:01:56 +02:00
#include <stdexcept>
2018-04-24 13:18:05 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Localisation;
2025-01-19 16:59:45 +01:00
static constexpr uint16_t kBaseObjectStringID = 0x2000;
static constexpr uint16_t kMaxObjectCachedStrings = 0x5000 - kBaseObjectStringID;
2018-04-24 13:18:05 +01:00
LocalisationService::LocalisationService(IPlatformEnvironment& env)
2018-04-24 13:18:05 +01:00
: _env(env)
{
2025-01-19 16:59:45 +01:00
for (StringId stringId = kBaseObjectStringID + kMaxObjectCachedStrings; stringId >= kBaseObjectStringID; stringId--)
2018-04-24 13:18:05 +01:00
{
_availableObjectStringIds.push(stringId);
}
}
2018-04-26 23:48:25 +01:00
// Define implementation here to avoid including LanguagePack.h in header
LocalisationService::~LocalisationService() = default;
2018-04-26 23:48:25 +01:00
2022-07-31 14:22:58 +02:00
const char* LocalisationService::GetString(StringId id) const
2018-04-24 13:18:05 +01:00
{
2025-01-19 16:59:45 +01:00
if (id == kStringIdEmpty)
2018-04-24 13:18:05 +01:00
{
2023-01-02 21:17:04 +01:00
return "";
2018-04-24 13:18:05 +01:00
}
2025-01-19 16:59:45 +01:00
else if (id >= kBaseObjectStringID && id < kBaseObjectStringID + kMaxObjectCachedStrings)
2021-10-27 14:21:14 +02:00
{
2025-01-19 16:59:45 +01:00
size_t index = id - kBaseObjectStringID;
2021-10-27 14:21:14 +02:00
if (index < _objectStrings.size())
{
return _objectStrings[index].c_str();
}
2023-01-02 21:17:04 +01:00
return "(unallocated string)";
2021-10-27 14:21:14 +02:00
}
2025-01-19 16:59:45 +01:00
else if (id != kStringIdNone)
2018-04-24 13:18:05 +01:00
{
2023-01-02 21:17:04 +01:00
for (const auto& language : _loadedLanguages)
2018-04-24 13:18:05 +01:00
{
2023-01-02 21:17:04 +01:00
const auto result = language->GetString(id);
if (result != nullptr)
return result;
2018-04-24 13:18:05 +01:00
}
2023-01-02 21:17:04 +01:00
return "(undefined string)";
2018-04-24 13:18:05 +01:00
}
2023-01-02 21:17:04 +01:00
return nullptr;
2018-04-24 13:18:05 +01:00
}
std::string LocalisationService::GetLanguagePath(uint32_t languageId) const
2018-04-24 13:18:05 +01:00
{
auto locale = std::string(LanguagesDescriptors[languageId].locale);
auto languageDirectory = _env.GetDirectoryPath(DirBase::openrct2, DirId::languages);
2022-02-27 22:23:35 +01:00
auto languagePath = Path::Combine(languageDirectory, locale + u8".txt");
2018-04-24 13:18:05 +01:00
return languagePath;
}
2022-09-18 16:15:42 +01:00
std::string_view LocalisationService::GetCurrentLanguageLocale() const
{
if (_currentLanguage >= 0 && static_cast<size_t>(_currentLanguage) < std::size(LanguagesDescriptors))
{
return LanguagesDescriptors[_currentLanguage].locale;
}
return {};
}
void LocalisationService::OpenLanguage(int32_t id)
2018-04-24 13:18:05 +01:00
{
CloseLanguages();
if (id == LANGUAGE_UNDEFINED)
{
throw std::invalid_argument("id was undefined");
}
2023-01-04 19:39:14 +01:00
auto preferredLanguage = LanguagePackFactory::FromLanguageId(id);
2023-01-02 21:17:04 +01:00
if (preferredLanguage != nullptr)
2018-04-24 13:18:05 +01:00
{
_currentLanguage = id;
2023-01-03 22:21:16 +01:00
_languageOrder.emplace_back(id);
2023-01-02 21:17:04 +01:00
_loadedLanguages.emplace_back(std::move(preferredLanguage));
2018-04-26 23:03:02 +01:00
TryLoadFonts(*this);
2018-04-24 13:18:05 +01:00
}
2018-04-26 23:48:25 +01:00
else
{
throw std::runtime_error("Unable to open language " + std::to_string(id));
}
2023-01-02 21:17:04 +01:00
2023-01-03 22:21:16 +01:00
auto checkLanguage = LanguagesDescriptors[id].fallback;
2023-01-04 19:39:14 +01:00
while (checkLanguage != LANGUAGE_UNDEFINED)
{
2023-01-03 22:21:16 +01:00
_languageOrder.emplace_back(checkLanguage);
2023-01-04 19:39:14 +01:00
auto fallbackLanguagePack = LanguagePackFactory::FromLanguageId(checkLanguage);
if (fallbackLanguagePack != nullptr)
{
_loadedLanguages.emplace_back(std::move(fallbackLanguagePack));
}
2023-01-03 22:21:16 +01:00
checkLanguage = LanguagesDescriptors[checkLanguage].fallback;
}
2023-01-02 21:17:04 +01:00
if (id != LANGUAGE_ENGLISH_UK)
{
2023-01-03 22:21:16 +01:00
_languageOrder.emplace_back(LANGUAGE_ENGLISH_UK);
2023-01-04 19:39:14 +01:00
auto englishLanguagePack = LanguagePackFactory::FromLanguageId(LANGUAGE_ENGLISH_UK);
if (englishLanguagePack != nullptr)
{
_loadedLanguages.emplace_back(std::move(englishLanguagePack));
}
else
{
throw std::runtime_error("Unable to open the English language file!");
}
2023-01-02 21:17:04 +01:00
}
2018-04-24 13:18:05 +01:00
}
void LocalisationService::CloseLanguages()
{
2023-01-03 22:21:16 +01:00
_languageOrder.clear();
2023-01-02 21:17:04 +01:00
_loadedLanguages.clear();
2018-04-24 13:18:05 +01:00
_currentLanguage = LANGUAGE_UNDEFINED;
}
2022-07-31 14:22:58 +02:00
StringId LocalisationService::AllocateObjectString(const std::string& target)
2018-04-24 13:18:05 +01:00
{
2021-10-27 14:21:14 +02:00
if (_availableObjectStringIds.empty())
{
2025-01-19 16:59:45 +01:00
return kStringIdEmpty;
2021-10-27 14:21:14 +02:00
}
2018-04-24 13:18:05 +01:00
auto stringId = _availableObjectStringIds.top();
_availableObjectStringIds.pop();
2021-10-27 14:21:14 +02:00
2025-01-19 16:59:45 +01:00
size_t index = stringId - kBaseObjectStringID;
2021-10-27 14:21:14 +02:00
if (index >= _objectStrings.size())
{
_objectStrings.resize(index + 1);
}
_objectStrings[index] = target;
2018-04-24 13:18:05 +01:00
return stringId;
}
2022-07-31 14:22:58 +02:00
void LocalisationService::FreeObjectString(StringId stringId)
2018-04-24 13:18:05 +01:00
{
2025-01-19 16:59:45 +01:00
if (stringId != kStringIdEmpty)
2018-04-24 13:18:05 +01:00
{
2025-01-19 16:59:45 +01:00
size_t index = stringId - kBaseObjectStringID;
2021-10-27 14:21:14 +02:00
if (index < _objectStrings.size())
2018-04-24 13:18:05 +01:00
{
2021-10-27 14:21:14 +02:00
_objectStrings[index] = {};
2018-04-24 13:18:05 +01:00
}
_availableObjectStringIds.push(stringId);
}
}
2023-01-03 22:21:16 +01:00
const std::vector<int32_t>& LocalisationService::GetLanguageOrder() const
{
return _languageOrder;
}
int32_t LocalisationService_GetCurrentLanguage()
2018-04-24 13:18:05 +01:00
{
const auto& localisationService = GetContext()->GetLocalisationService();
return localisationService.GetCurrentLanguage();
2018-04-24 13:18:05 +01:00
}
bool LocalisationService_UseTrueTypeFont()
{
const auto& localisationService = GetContext()->GetLocalisationService();
return localisationService.UseTrueTypeFont();
2018-04-24 13:18:05 +01:00
}