Files
OpenRCT2-Web/src/localisation/language.cpp
T

248 lines
7.4 KiB
C++
Raw Normal View History

#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
2014-08-14 23:46:36 +01:00
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
2014-08-14 23:46:36 +01:00
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
2014-08-14 23:46:36 +01:00
*****************************************************************************/
#pragma endregion
2014-08-14 23:46:36 +01:00
2016-06-26 16:55:46 +01:00
#include <stack>
2016-10-13 12:34:22 +01:00
#include "../core/Console.hpp"
#include "../core/Memory.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
2016-11-06 18:50:54 +00:00
#include "../core/StringBuilder.hpp"
2016-07-07 23:32:45 +01:00
#include "../object/ObjectManager.h"
#include "../interface/Fonts.h"
#include "LanguagePack.h"
extern "C" {
2015-06-08 17:16:18 +01:00
#include "localisation.h"
2014-08-14 23:46:36 +01:00
int gCurrentLanguage = LANGUAGE_UNDEFINED;
2015-07-26 17:50:01 +01:00
bool gUseTrueTypeFont = false;
2014-08-14 23:46:36 +01:00
2016-10-13 12:13:00 +01:00
static ILanguagePack * _languageFallback = nullptr;
static ILanguagePack * _languageCurrent = nullptr;
2014-08-14 23:46:36 +01:00
2016-10-30 01:51:09 +01:00
const utf8 BlackUpArrowString[] = { (utf8)0xC2, (utf8)0x8E, (utf8)0xE2, (utf8)0x96, (utf8)0xB2, (utf8)0x00 };
const utf8 BlackDownArrowString[] = { (utf8)0xC2, (utf8)0x8E, (utf8)0xE2, (utf8)0x96, (utf8)0xBC, (utf8)0x00 };
const utf8 BlackLeftArrowString[] = { (utf8)0xC2, (utf8)0x8E, (utf8)0xE2, (utf8)0x97, (utf8)0x80, (utf8)0x00 };
const utf8 BlackRightArrowString[] = { (utf8)0xC2, (utf8)0x8E, (utf8)0xE2, (utf8)0x96, (utf8)0xB6, (utf8)0x00 };
const utf8 CheckBoxMarkString[] = { (utf8)0xE2, (utf8)0x9C, (utf8)0x93, (utf8)0x00 };
2014-08-14 23:46:36 +01:00
2016-10-13 12:56:01 +01:00
void utf8_remove_format_codes(utf8 * text, bool allowcolours)
2015-07-29 14:36:21 +01:00
{
2016-10-13 12:56:01 +01:00
const utf8 * ch = text;
utf8 * dstCh = text;
2016-10-13 12:05:49 +01:00
int 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
}
2016-10-13 12:13:00 +01:00
const char * language_get_string(rct_string_id id)
2014-08-14 23:46:36 +01:00
{
2016-10-13 12:13:00 +01:00
const char * result = nullptr;
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;
2014-08-14 23:46:36 +01:00
}
2016-10-13 12:56:01 +01:00
static utf8 * GetLanguagePath(utf8 * buffer, size_t bufferSize, uint32 languageId)
2016-10-13 12:34:22 +01:00
{
const char * locale = LanguagesDescriptors[languageId].locale;
2016-10-31 23:12:58 +00:00
platform_get_openrct_data_path(buffer, bufferSize);
Path::Append(buffer, bufferSize, "language");
Path::Append(buffer, bufferSize, locale);
String::Append(buffer, bufferSize, ".txt");
2016-10-13 12:34:22 +01:00
return buffer;
}
bool language_open(int id)
2014-08-14 23:46:36 +01:00
{
2016-10-13 12:05:49 +01:00
char filename[MAX_PATH];
2014-08-14 23:46:36 +01:00
2016-10-13 12:05:49 +01:00
language_close_all();
if (id == LANGUAGE_UNDEFINED)
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
2016-10-13 12:13:00 +01:00
if (id != LANGUAGE_ENGLISH_UK)
{
2016-10-13 12:34:22 +01:00
GetLanguagePath(filename, sizeof(filename), LANGUAGE_ENGLISH_UK);
2016-10-13 12:05:49 +01:00
_languageFallback = LanguagePackFactory::FromFile(LANGUAGE_ENGLISH_UK, filename);
}
2015-06-08 17:16:18 +01:00
2016-10-13 12:34:22 +01:00
GetLanguagePath(filename, sizeof(filename), id);
2016-10-13 12:05:49 +01:00
_languageCurrent = LanguagePackFactory::FromFile(id, filename);
2016-10-13 12:13:00 +01:00
if (_languageCurrent != nullptr)
{
2016-10-13 12:05:49 +01:00
gCurrentLanguage = id;
2016-10-13 12:56:01 +01:00
TryLoadFonts();
2015-07-26 00:55:17 +01:00
2016-10-13 12:05:49 +01:00
// Objects and their localized strings need to be refreshed
GetObjectManager()->ResetObjects();
return true;
}
2014-08-14 23:46:36 +01:00
2016-10-13 12:05:49 +01:00
return 0;
2014-08-14 23:46:36 +01:00
}
2015-06-08 17:16:18 +01:00
void language_close_all()
{
2016-10-13 12:05:49 +01:00
SafeDelete(_languageFallback);
SafeDelete(_languageCurrent);
gCurrentLanguage = LANGUAGE_UNDEFINED;
2015-06-08 17:16:18 +01:00
}
2016-10-13 12:34:22 +01:00
constexpr rct_string_id NONSTEX_BASE_STRING_ID = 3463;
constexpr uint16 MAX_OBJECT_CACHED_STRINGS = 2048;
2015-07-27 01:09:24 +01:00
2015-07-27 15:12:36 +01:00
static wchar_t convert_specific_language_character_to_unicode(int languageId, wchar_t codepoint)
{
2016-10-13 12:05:49 +01:00
switch (languageId) {
case RCT2_LANGUAGE_ID_KOREAN:
return codepoint;
case RCT2_LANGUAGE_ID_CHINESE_TRADITIONAL:
return encoding_convert_big5_to_unicode(codepoint);
case RCT2_LANGUAGE_ID_CHINESE_SIMPLIFIED:
return encoding_convert_gb2312_to_unicode(codepoint);
default:
return codepoint;
}
2015-07-27 15:12:36 +01:00
}
2016-10-13 12:34:22 +01:00
static utf8 * convert_multibyte_charset(const char * src, int languageId)
2015-07-27 01:09:24 +01:00
{
2016-10-13 12:34:22 +01:00
constexpr char CODEPOINT_DOUBLEBYTE = (char)0xFF;
2016-11-06 18:50:54 +00:00
auto sb = StringBuilder(64);
2016-10-13 12:34:22 +01:00
for (const char * ch = src; *ch != 0;)
2016-10-13 12:13:00 +01:00
{
2016-10-13 12:34:22 +01:00
if (*ch == CODEPOINT_DOUBLEBYTE)
2016-10-13 12:13:00 +01:00
{
2016-10-13 12:05:49 +01:00
ch++;
2016-11-06 18:50:54 +00:00
uint8 a = *ch++;
uint8 b = *ch++;
wchar_t codepoint16 = (wchar_t)((a << 8) | b);
2015-07-27 14:26:22 +01:00
2016-11-06 18:50:54 +00:00
codepoint16 = convert_specific_language_character_to_unicode(languageId, codepoint16);
sb.Append(codepoint16);
2016-10-13 12:13:00 +01:00
}
else
{
2016-11-06 18:50:54 +00:00
codepoint_t codepoint = (uint8)*ch++;
sb.Append(codepoint);
2016-10-13 12:05:49 +01:00
}
}
2016-11-06 18:50:54 +00:00
return sb.StealString();
2015-07-27 01:09:24 +01:00
}
static bool rct2_language_is_multibyte_charset(int languageId)
{
2016-10-13 12:05:49 +01:00
switch (languageId) {
case RCT2_LANGUAGE_ID_KOREAN:
case RCT2_LANGUAGE_ID_CHINESE_TRADITIONAL:
case RCT2_LANGUAGE_ID_CHINESE_SIMPLIFIED:
case RCT2_LANGUAGE_ID_8:
return true;
default:
return false;
}
2015-07-27 01:09:24 +01:00
}
2016-07-04 19:03:30 +01:00
utf8 *rct2_language_string_to_utf8(const char *src, int languageId)
{
2016-10-13 12:13:00 +01:00
if (rct2_language_is_multibyte_charset(languageId))
{
2016-07-04 19:03:30 +01:00
return convert_multibyte_charset(src, languageId);
2016-10-13 12:13:00 +01:00
}
else
{
2016-07-04 19:03:30 +01:00
return win1252_to_utf8_alloc(src);
}
}
2015-09-08 18:46:22 +01:00
bool language_get_localised_scenario_strings(const utf8 *scenarioFilename, rct_string_id *outStringIds)
{
2016-10-13 12:05:49 +01:00
outStringIds[0] = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename, 0);
outStringIds[1] = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename, 1);
outStringIds[2] = _languageCurrent->GetScenarioOverrideStringId(scenarioFilename, 2);
return
outStringIds[0] != STR_NONE ||
outStringIds[1] != STR_NONE ||
outStringIds[2] != STR_NONE;
2015-09-08 18:46:22 +01:00
}
2016-10-13 12:05:49 +01:00
static bool _availableObjectStringIdsInitialised = false;
static std::stack<rct_string_id> _availableObjectStringIds;
2016-06-25 16:34:35 +01:00
rct_string_id language_allocate_object_string(const utf8 * target)
{
2016-10-13 12:05:49 +01:00
if (!_availableObjectStringIdsInitialised)
{
_availableObjectStringIdsInitialised = true;
for (rct_string_id stringId = NONSTEX_BASE_STRING_ID + MAX_OBJECT_CACHED_STRINGS; stringId >= NONSTEX_BASE_STRING_ID; stringId--)
{
_availableObjectStringIds.push(stringId);
}
}
2016-11-13 20:17:49 +01:00
2016-10-13 12:05:49 +01:00
rct_string_id stringId = _availableObjectStringIds.top();
_availableObjectStringIds.pop();
_languageCurrent->SetString(stringId, target);
return stringId;
2016-06-25 16:34:35 +01:00
}
void language_free_object_string(rct_string_id stringId)
{
2016-10-13 12:05:49 +01:00
if (stringId != 0)
{
if (_languageCurrent != nullptr)
{
_languageCurrent->SetString(stringId, nullptr);
}
_availableObjectStringIds.push(stringId);
}
2016-06-25 16:34:35 +01:00
}
2016-07-04 18:53:35 +01:00
rct_string_id language_get_object_override_string_id(const char * identifier, uint8 index)
{
2016-10-13 12:05:49 +01:00
if (_languageCurrent == nullptr)
{
return STR_NONE;
}
return _languageCurrent->GetObjectOverrideStringId(identifier, index);
2016-07-04 18:53:35 +01:00
}
}