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

242 lines
11 KiB
C++
Raw Normal View History

2014-08-14 23:46:36 +01:00
/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
2014-08-14 23:46:36 +01:00
*
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
2014-08-14 23:46:36 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-08-14 23:46:36 +01:00
*****************************************************************************/
#include "../Context.h"
2016-10-13 12:34:22 +01:00
#include "../core/Path.hpp"
#include "../core/String.hpp"
2017-10-26 23:23:24 +02:00
#include "../interface/FontFamilies.h"
2018-06-22 23:01:56 +02:00
#include "../interface/Fonts.h"
2016-12-01 15:05:00 +01:00
#include "../object/ObjectManager.h"
2017-02-09 21:14:48 +00:00
#include "../platform/platform.h"
2018-04-24 13:18:05 +01:00
#include "LanguagePack.h"
2018-01-06 18:32:25 +01:00
#include "Localisation.h"
2018-04-24 13:18:05 +01:00
#include "LocalisationService.h"
2015-06-08 17:16:18 +01:00
2018-06-22 23:01:56 +02:00
#include <stack>
2018-01-10 16:14:36 +01:00
// clang-format off
2017-10-26 23:23:24 +02:00
const language_descriptor LanguagesDescriptors[LANGUAGE_COUNT] =
{
{ "", "", "", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_UNDEFINED
{ "ar-EG", "Arabic (experimental)", "Arabic (experimental)", FAMILY(&TTFFamilySansSerif), true }, // LANGUAGE_ARABIC
{ "ca-ES", "Catalan", u8"Català", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_CATALAN
{ "zh-CN", "Chinese (Simplified)", "Chinese (Simplified)", FAMILY(&TTFFamilyChineseSimplified), false }, // LANGUAGE_CHINESE_SIMPLIFIED
{ "zh-TW", "Chinese (Traditional)", "Chinese (Traditional)", FAMILY(&TTFFamilyChineseTraditional), false }, // LANGUAGE_CHINESE_TRADITIONAL
2019-03-25 17:03:12 +01:00
{ "cs-CZ", "Czech", u8"Čeština", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_CZECH
{ "da-DK", "Danish", "Dansk", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_DANISH
{ "de-DE", "German", "Deutsch", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_GERMAN
{ "en-GB", "English (UK)", "English (UK)", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_ENGLISH_UK
{ "en-US", "English (US)", "English (US)", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_ENGLISH_US
{ "es-ES", "Spanish", u8"Español", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_SPANISH
{ "fr-FR", "French", u8"Français", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_FRENCH
{ "it-IT", "Italian", "Italiano", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_ITALIAN
{ "ja-JP", "Japanese", "Japanese", FAMILY(&TTFFamilyJapanese), false }, // LANGUAGE_JAPANESE
{ "ko-KR", "Korean", "Korean", FAMILY(&TTFFamilyKorean), false }, // LANGUAGE_KOREAN
{ "hu-HU", "Hungarian", "Magyar", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_HUNGARIAN
{ "nl-NL", "Dutch", "Nederlands", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_DUTCH
{ "nb-NO", "Norwegian", "Norsk", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_NORWEGIAN
{ "pl-PL", "Polish", "Polski", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_POLISH
{ "pt-BR", "Portuguese (BR)", u8"Português (BR)", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_PORTUGUESE_BR
{ "ru-RU", "Russian", u8"Русский", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_RUSSIAN
{ "fi-FI", "Finnish", "Suomi", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_FINNISH
{ "sv-SE", "Swedish", "Svenska", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_SWEDISH
{ "tr-TR", "Turkish", "Türkçe", FAMILY_OPENRCT2_SPRITE, false }, // LANGUAGE_TURKISH
2017-10-09 10:53:09 +02:00
};
2018-01-10 16:14:36 +01:00
// clang-format on
2017-10-09 10:53:09 +02:00
2018-01-10 16:14:36 +01:00
// clang-format off
2020-04-22 17:09:29 +02:00
const utf8 BlackUpArrowString[] = { static_cast<utf8>(static_cast<uint8_t>(0xC2)), static_cast<utf8>(static_cast<uint8_t>(0x8E)), static_cast<utf8>(static_cast<uint8_t>(0xE2)), static_cast<utf8>(static_cast<uint8_t>(0x96)), static_cast<utf8>(static_cast<uint8_t>(0xB2)), static_cast<utf8>(static_cast<uint8_t>(0x00)) };
const utf8 BlackDownArrowString[] = { static_cast<utf8>(static_cast<uint8_t>(0xC2)), static_cast<utf8>(static_cast<uint8_t>(0x8E)), static_cast<utf8>(static_cast<uint8_t>(0xE2)), static_cast<utf8>(static_cast<uint8_t>(0x96)), static_cast<utf8>(static_cast<uint8_t>(0xBC)), static_cast<utf8>(static_cast<uint8_t>(0x00)) };
const utf8 BlackLeftArrowString[] = { static_cast<utf8>(static_cast<uint8_t>(0xC2)), static_cast<utf8>(static_cast<uint8_t>(0x8E)), static_cast<utf8>(static_cast<uint8_t>(0xE2)), static_cast<utf8>(static_cast<uint8_t>(0x97)), static_cast<utf8>(static_cast<uint8_t>(0x80)), static_cast<utf8>(static_cast<uint8_t>(0x00)) };
const utf8 BlackRightArrowString[] = { static_cast<utf8>(static_cast<uint8_t>(0xC2)), static_cast<utf8>(static_cast<uint8_t>(0x8E)), static_cast<utf8>(static_cast<uint8_t>(0xE2)), static_cast<utf8>(static_cast<uint8_t>(0x96)), static_cast<utf8>(static_cast<uint8_t>(0xB6)), static_cast<utf8>(static_cast<uint8_t>(0x00)) };
const utf8 CheckBoxMarkString[] = { static_cast<utf8>(static_cast<uint8_t>(0xE2)), static_cast<utf8>(static_cast<uint8_t>(0x9C)), static_cast<utf8>(static_cast<uint8_t>(0x93)), static_cast<utf8>(static_cast<uint8_t>(0x00)) };
2018-01-10 16:14:36 +01:00
// clang-format on
2014-08-14 23:46:36 +01:00
2018-06-22 23:01:56 +02:00
void utf8_remove_format_codes(utf8* text, bool allowcolours)
2015-07-29 14:36:21 +01:00
{
2018-06-22 23:01:56 +02:00
const utf8* ch = text;
utf8* dstCh = text;
int32_t codepoint;
2016-10-13 12:56:01 +01:00
while ((codepoint = String::GetNextCodepoint(ch, &ch)) != 0)
2016-10-13 12:13:00 +01:00
{
if (!utf8_is_format_code(codepoint) || (allowcolours && utf8_is_colour_code(codepoint)))
{
2016-10-13 12:56:01 +01:00
dstCh = String::WriteCodepoint(dstCh, codepoint);
2016-10-13 12:05:49 +01:00
}
}
*dstCh = 0;
2015-07-29 14:36:21 +01:00
}
2018-06-22 23:01:56 +02:00
uint8_t language_get_id_from_locale(const char* locale)
2017-12-11 12:30:08 +00:00
{
uint8_t i = 0;
2018-06-22 23:01:56 +02:00
for (const auto& langDesc : LanguagesDescriptors)
2017-12-11 12:30:08 +00:00
{
if (String::Equals(locale, langDesc.locale))
{
return i;
}
i++;
}
return LANGUAGE_UNDEFINED;
}
2018-06-22 23:01:56 +02:00
const char* language_get_string(rct_string_id id)
2014-08-14 23:46:36 +01:00
{
2018-04-24 13:18:05 +01:00
const auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
return localisationService.GetString(id);
2016-10-13 12:34:22 +01:00
}
bool language_open(int32_t id)
2014-08-14 23:46:36 +01:00
{
2018-04-24 13:18:05 +01:00
auto context = OpenRCT2::GetContext();
auto& localisationService = context->GetLocalisationService();
2018-08-29 00:52:10 +02:00
auto& objectManager = context->GetObjectManager();
2018-04-24 13:18:05 +01:00
try
{
2018-08-29 00:52:10 +02:00
localisationService.OpenLanguage(id, objectManager);
2018-04-24 13:18:05 +01:00
return true;
}
catch (const std::exception&)
2016-10-13 12:13:00 +01:00
{
2016-10-13 12:05:49 +01:00
return false;
2016-10-13 12:13:00 +01:00
}
2014-08-14 23:46:36 +01:00
}
2018-06-22 23:01:56 +02:00
bool language_get_localised_scenario_strings(const utf8* scenarioFilename, rct_string_id* outStringIds)
2015-09-08 18:46:22 +01:00
{
2018-04-24 13:18:05 +01:00
const auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
auto result = localisationService.GetLocalisedScenarioStrings(scenarioFilename);
2018-04-24 13:18:05 +01:00
outStringIds[0] = std::get<0>(result);
outStringIds[1] = std::get<1>(result);
outStringIds[2] = std::get<2>(result);
2018-06-22 23:01:56 +02:00
return outStringIds[0] != STR_NONE || outStringIds[1] != STR_NONE || outStringIds[2] != STR_NONE;
2015-09-08 18:46:22 +01:00
}
2016-06-25 16:34:35 +01:00
void language_free_object_string(rct_string_id stringId)
{
auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
localisationService.FreeObjectString(stringId);
2016-06-25 16:34:35 +01:00
}
2018-06-22 23:01:56 +02:00
rct_string_id language_allocate_object_string(const std::string& target)
2017-12-05 12:55:33 +00:00
{
auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
return localisationService.AllocateObjectString(target);
2017-12-05 12:55:33 +00:00
}
2020-02-27 22:25:26 +00:00
std::string language_convert_string_to_tokens(const std::string_view& s)
{
std::string result;
result.reserve(s.size() * 4);
std::string input = std::string(s);
auto readPtr = input.c_str();
while (true)
{
char32_t code = utf8_get_next(readPtr, const_cast<const utf8**>(&readPtr));
2020-02-27 22:25:26 +00:00
if (code == 0)
{
break;
}
else if (code == '\n')
{
result.push_back('\n');
}
else if (utf8_is_format_code(code))
{
auto token = format_get_token(code);
result.push_back('{');
result.append(token);
result.push_back('}');
}
else
{
char buffer[8]{};
utf8_write_codepoint(buffer, code);
result.append(buffer);
}
}
result.shrink_to_fit();
return result;
}
std::string language_convert_string(const std::string_view& s)
{
enum class PARSE_STATE
{
DEFAULT,
CR,
TOKEN,
};
std::string result;
std::string token;
PARSE_STATE state{};
token.reserve(64);
result.reserve(s.size() * 2);
for (char c : s)
{
switch (state)
{
case PARSE_STATE::CR:
result.push_back(FORMAT_NEWLINE);
state = PARSE_STATE::DEFAULT;
[[fallthrough]];
case PARSE_STATE::DEFAULT:
switch (c)
{
case '\r':
state = PARSE_STATE::CR;
break;
case '\n':
result.push_back(FORMAT_NEWLINE);
break;
case '{':
token.clear();
state = PARSE_STATE::TOKEN;
break;
default:
2020-04-30 20:32:39 +01:00
if (static_cast<uint8_t>(c) >= 32)
{
result.push_back(c);
}
break;
}
break;
case PARSE_STATE::TOKEN:
if (c == '}')
{
auto code = format_get_code(token.c_str());
if (code == 0)
{
int32_t number{};
if (sscanf(token.c_str(), "%d", &number) == 1)
{
auto b = static_cast<uint8_t>(std::clamp(number, 0, 255));
token.push_back(b);
}
}
else
{
char buffer[8]{};
utf8_write_codepoint(buffer, code);
result.append(buffer);
}
state = PARSE_STATE::DEFAULT;
}
else
{
token.push_back(c);
}
break;
}
}
result.shrink_to_fit();
return result;
}