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

189 lines
5.5 KiB
C++
Raw Normal View History

2018-04-24 13:18:05 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 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 "../object/ObjectManager.h"
#include "Language.h"
#include "LanguagePack.h"
#include "StringIds.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;
2021-10-27 14:21:14 +02:00
static constexpr uint16_t BASE_OBJECT_STRING_ID = 0x2000;
static constexpr uint16_t MAX_OBJECT_CACHED_STRINGS = 0x5000 - BASE_OBJECT_STRING_ID;
2018-04-24 13:18:05 +01:00
LocalisationService::LocalisationService(const std::shared_ptr<IPlatformEnvironment>& env)
: _env(env)
{
2022-07-31 14:22:58 +02:00
for (StringId stringId = BASE_OBJECT_STRING_ID + MAX_OBJECT_CACHED_STRINGS; stringId >= BASE_OBJECT_STRING_ID; 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
{
2018-06-22 23:01:56 +02:00
const char* result = nullptr;
2018-04-24 13:18:05 +01:00
if (id == STR_EMPTY)
{
result = "";
}
2021-10-27 14:21:14 +02:00
else if (id >= BASE_OBJECT_STRING_ID && id < BASE_OBJECT_STRING_ID + MAX_OBJECT_CACHED_STRINGS)
{
size_t index = id - BASE_OBJECT_STRING_ID;
if (index < _objectStrings.size())
{
return _objectStrings[index].c_str();
}
result = "(unallocated string)";
}
2018-04-24 13:18:05 +01:00
else if (id != STR_NONE)
{
if (_languageCurrent != nullptr)
{
result = _languageCurrent->GetString(id);
}
if (result == nullptr && _languageFallback != nullptr)
{
result = _languageFallback->GetString(id);
}
if (result == nullptr)
{
result = "(undefined string)";
}
}
return result;
}
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::LANGUAGE);
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");
}
std::string filename;
if (id != LANGUAGE_ENGLISH_UK)
{
filename = GetLanguagePath(LANGUAGE_ENGLISH_UK);
_languageFallback = LanguagePackFactory::FromFile(LANGUAGE_ENGLISH_UK, filename.c_str());
2018-04-24 13:18:05 +01:00
}
filename = GetLanguagePath(id);
_languageCurrent = LanguagePackFactory::FromFile(id, filename.c_str());
2018-04-24 13:18:05 +01:00
if (_languageCurrent != nullptr)
{
_currentLanguage = id;
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));
}
2018-04-24 13:18:05 +01:00
}
void LocalisationService::CloseLanguages()
{
_languageFallback = nullptr;
_languageCurrent = nullptr;
_currentLanguage = LANGUAGE_UNDEFINED;
}
2022-07-31 14:22:58 +02:00
std::tuple<StringId, StringId, StringId> LocalisationService::GetLocalisedScenarioStrings(
const std::string& scenarioFilename) const
2018-04-24 13:18:05 +01:00
{
auto result0 = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename.c_str(), 0);
auto result1 = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename.c_str(), 1);
auto result2 = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename.c_str(), 2);
return std::make_tuple(result0, result1, result2);
}
2022-07-31 14:22:58 +02:00
StringId LocalisationService::GetObjectOverrideStringId(std::string_view legacyIdentifier, uint8_t index) const
2018-04-24 13:18:05 +01:00
{
if (_languageCurrent == nullptr)
{
return STR_NONE;
}
return _languageCurrent->GetObjectOverrideStringId(legacyIdentifier, index);
2018-04-24 13:18:05 +01:00
}
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())
{
return STR_EMPTY;
}
2018-04-24 13:18:05 +01:00
auto stringId = _availableObjectStringIds.top();
_availableObjectStringIds.pop();
2021-10-27 14:21:14 +02:00
size_t index = stringId - BASE_OBJECT_STRING_ID;
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
{
if (stringId != STR_EMPTY)
{
2021-10-27 14:21:14 +02:00
size_t index = stringId - BASE_OBJECT_STRING_ID;
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);
}
}
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
}