2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2009-06-13 16:54:04 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "localization.h"
|
2009-06-13 16:54:04 +00:00
|
|
|
|
2010-08-17 22:06:00 +00:00
|
|
|
#include <engine/console.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/shared/linereader.h>
|
2010-10-06 21:07:35 +00:00
|
|
|
#include <engine/storage.h>
|
2009-06-13 17:18:06 +00:00
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
const char *Localize(const char *pStr, const char *pContext)
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2020-09-13 20:25:37 +00:00
|
|
|
const char *pNewStr = g_Localization.FindString(str_quickhash(pStr), str_quickhash(pContext));
|
2010-05-29 07:25:38 +00:00
|
|
|
return pNewStr ? pNewStr : pStr;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
CLocConstString::CLocConstString(const char *pStr, const char *pContext)
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pDefaultStr = pStr;
|
|
|
|
m_Hash = str_quickhash(m_pDefaultStr);
|
|
|
|
m_Version = -1;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLocConstString::Reload()
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_Version = g_Localization.Version();
|
2020-09-13 20:25:37 +00:00
|
|
|
const char *pNewStr = g_Localization.FindString(m_Hash, m_ContextHash);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pCurrentStr = pNewStr;
|
|
|
|
if(!m_pCurrentStr)
|
|
|
|
m_pCurrentStr = m_pDefaultStr;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CLocalizationDatabase::CLocalizationDatabase()
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2010-11-03 01:15:39 +00:00
|
|
|
m_VersionCounter = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_CurrentVersion = 0;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 20:06:53 +00:00
|
|
|
void CLocalizationDatabase::LoadIndexfile(IStorage *pStorage, IConsole *pConsole)
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2023-03-20 20:06:53 +00:00
|
|
|
m_vLanguages.clear();
|
2023-03-20 20:45:43 +00:00
|
|
|
|
|
|
|
const std::vector<std::string> vEnglishLanguageCodes = {"en"};
|
|
|
|
m_vLanguages.emplace_back("English", "", 826, vEnglishLanguageCodes);
|
2023-03-20 20:06:53 +00:00
|
|
|
|
|
|
|
const char *pFilename = "languages/index.txt";
|
|
|
|
IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
|
|
|
|
if(!File)
|
|
|
|
{
|
2023-03-20 20:45:43 +00:00
|
|
|
char aBuf[64 + IO_MAX_PATH_LENGTH];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Couldn't open index file '%s'", pFilename);
|
2023-03-20 20:06:53 +00:00
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLineReader LineReader;
|
|
|
|
LineReader.Init(File);
|
2023-03-20 20:45:43 +00:00
|
|
|
|
|
|
|
const char *pLine;
|
2023-03-20 20:06:53 +00:00
|
|
|
while((pLine = LineReader.Get()))
|
|
|
|
{
|
|
|
|
if(!str_length(pLine) || pLine[0] == '#') // skip empty lines and comments
|
|
|
|
continue;
|
|
|
|
|
2023-03-20 20:45:43 +00:00
|
|
|
char aEnglishName[128];
|
|
|
|
str_copy(aEnglishName, pLine);
|
2023-03-20 20:06:53 +00:00
|
|
|
|
|
|
|
pLine = LineReader.Get();
|
|
|
|
if(!pLine)
|
|
|
|
{
|
2023-03-20 20:45:43 +00:00
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Unexpected end of index file after language '%s'", aEnglishName);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
2023-03-20 20:06:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-03-20 20:45:43 +00:00
|
|
|
if(!str_startswith(pLine, "== "))
|
2023-03-20 20:06:53 +00:00
|
|
|
{
|
2023-03-20 20:45:43 +00:00
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Missing native name for language '%s'", aEnglishName);
|
2023-03-20 20:06:53 +00:00
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
(void)LineReader.Get();
|
2023-03-20 20:45:43 +00:00
|
|
|
(void)LineReader.Get();
|
2023-03-20 20:06:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-20 20:45:43 +00:00
|
|
|
char aNativeName[128];
|
|
|
|
str_copy(aNativeName, pLine + 3);
|
2023-03-20 20:06:53 +00:00
|
|
|
|
|
|
|
pLine = LineReader.Get();
|
|
|
|
if(!pLine)
|
|
|
|
{
|
2023-03-20 20:45:43 +00:00
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Unexpected end of index file after language '%s'", aEnglishName);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
2023-03-20 20:06:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-03-20 20:45:43 +00:00
|
|
|
if(!str_startswith(pLine, "== "))
|
|
|
|
{
|
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Missing country code for language '%s'", aEnglishName);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
(void)LineReader.Get();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char aCountryCode[128];
|
|
|
|
str_copy(aCountryCode, pLine + 3);
|
2023-03-20 20:06:53 +00:00
|
|
|
|
2023-03-20 20:45:43 +00:00
|
|
|
pLine = LineReader.Get();
|
|
|
|
if(!pLine)
|
|
|
|
{
|
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Unexpected end of index file after language '%s'", aEnglishName);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!str_startswith(pLine, "== "))
|
|
|
|
{
|
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "Missing language codes for language '%s'", aEnglishName);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char *pLanguageCodes = pLine + 3;
|
|
|
|
char aLanguageCode[256];
|
|
|
|
std::vector<std::string> vLanguageCodes;
|
|
|
|
while((pLanguageCodes = str_next_token(pLanguageCodes, ";", aLanguageCode, sizeof(aLanguageCode))))
|
|
|
|
{
|
|
|
|
if(aLanguageCode[0])
|
|
|
|
{
|
|
|
|
vLanguageCodes.emplace_back(aLanguageCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(vLanguageCodes.empty())
|
2023-03-20 20:06:53 +00:00
|
|
|
{
|
2023-03-20 20:45:43 +00:00
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "At least one language code required for language '%s'", aEnglishName);
|
2023-03-20 20:06:53 +00:00
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char aFileName[IO_MAX_PATH_LENGTH];
|
2023-03-20 20:45:43 +00:00
|
|
|
str_format(aFileName, sizeof(aFileName), "languages/%s.txt", aEnglishName);
|
|
|
|
m_vLanguages.emplace_back(aNativeName, aFileName, str_toint(aCountryCode), vLanguageCodes);
|
2023-03-20 20:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io_close(File);
|
|
|
|
|
|
|
|
std::sort(m_vLanguages.begin(), m_vLanguages.end());
|
2009-06-13 17:18:06 +00:00
|
|
|
}
|
|
|
|
|
2010-10-06 21:07:35 +00:00
|
|
|
bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, IConsole *pConsole)
|
2009-06-13 17:18:06 +00:00
|
|
|
{
|
2009-06-15 08:15:53 +00:00
|
|
|
// empty string means unload
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pFilename[0] == 0)
|
2009-06-15 08:15:53 +00:00
|
|
|
{
|
2022-06-11 19:38:18 +00:00
|
|
|
m_vStrings.clear();
|
2022-03-06 15:35:40 +00:00
|
|
|
m_StringsHeap.Reset();
|
2010-05-29 07:25:38 +00:00
|
|
|
m_CurrentVersion = 0;
|
2009-06-15 08:15:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2021-12-17 20:58:28 +00:00
|
|
|
IOHANDLE IoHandle = pStorage->OpenFile(pFilename, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!IoHandle)
|
2009-06-13 17:18:06 +00:00
|
|
|
return false;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-08-17 22:06:00 +00:00
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "loaded '%s'", pFilename);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
2022-06-11 19:38:18 +00:00
|
|
|
m_vStrings.clear();
|
2022-03-06 15:35:40 +00:00
|
|
|
m_StringsHeap.Reset();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
char aContext[512];
|
2011-02-10 11:07:00 +00:00
|
|
|
char aOrigin[512];
|
2010-05-29 07:25:38 +00:00
|
|
|
CLineReader LineReader;
|
|
|
|
LineReader.Init(IoHandle);
|
|
|
|
char *pLine;
|
2020-06-26 16:42:56 +00:00
|
|
|
int Line = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
while((pLine = LineReader.Get()))
|
2009-06-13 17:18:06 +00:00
|
|
|
{
|
2020-06-26 16:42:56 +00:00
|
|
|
Line++;
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!str_length(pLine))
|
2009-06-13 17:18:06 +00:00
|
|
|
continue;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pLine[0] == '#') // skip comments
|
2009-06-13 17:18:06 +00:00
|
|
|
continue;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
if(pLine[0] == '[') // context
|
|
|
|
{
|
|
|
|
size_t Len = str_length(pLine);
|
|
|
|
if(Len < 1 || pLine[Len - 1] != ']')
|
|
|
|
{
|
|
|
|
str_format(aBuf, sizeof(aBuf), "malform context line (%d): %s", Line, pLine);
|
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
str_copy(aContext, pLine + 1, Len - 1);
|
|
|
|
pLine = LineReader.Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aContext[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2011-02-10 11:07:00 +00:00
|
|
|
str_copy(aOrigin, pLine, sizeof(aOrigin));
|
2010-05-29 07:25:38 +00:00
|
|
|
char *pReplacement = LineReader.Get();
|
2020-06-26 16:42:56 +00:00
|
|
|
Line++;
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!pReplacement)
|
2009-06-13 17:18:06 +00:00
|
|
|
{
|
2010-08-17 22:06:00 +00:00
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "unexpected end of file");
|
2009-06-13 17:18:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pReplacement[0] != '=' || pReplacement[1] != '=' || pReplacement[2] != ' ')
|
2009-06-13 17:18:06 +00:00
|
|
|
{
|
2020-06-26 16:42:56 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "malform replacement line (%d) for '%s'", Line, aOrigin);
|
2010-08-17 22:06:00 +00:00
|
|
|
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
2009-06-13 17:18:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pReplacement += 3;
|
2020-09-13 20:25:37 +00:00
|
|
|
AddString(aOrigin, pReplacement, aContext);
|
2009-06-13 17:18:06 +00:00
|
|
|
}
|
2011-01-19 21:14:40 +00:00
|
|
|
io_close(IoHandle);
|
2022-06-11 19:38:18 +00:00
|
|
|
std::sort(m_vStrings.begin(), m_vStrings.end());
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-11-03 01:15:39 +00:00
|
|
|
m_CurrentVersion = ++m_VersionCounter;
|
2009-06-13 17:18:06 +00:00
|
|
|
return true;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 20:06:53 +00:00
|
|
|
void CLocalizationDatabase::AddString(const char *pOrgStr, const char *pNewStr, const char *pContext)
|
|
|
|
{
|
|
|
|
m_vStrings.emplace_back(str_quickhash(pOrgStr), str_quickhash(pContext), m_StringsHeap.StoreString(*pNewStr ? pNewStr : pOrgStr));
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:24:58 +00:00
|
|
|
const char *CLocalizationDatabase::FindString(unsigned Hash, unsigned ContextHash) const
|
2009-06-13 16:54:04 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
CString String;
|
|
|
|
String.m_Hash = Hash;
|
2020-09-13 20:25:37 +00:00
|
|
|
String.m_ContextHash = ContextHash;
|
2022-03-06 15:35:40 +00:00
|
|
|
String.m_pReplacement = 0x0;
|
2022-06-11 19:38:18 +00:00
|
|
|
auto Range1 = std::equal_range(m_vStrings.begin(), m_vStrings.end(), String);
|
2022-05-23 18:57:43 +00:00
|
|
|
if(std::distance(Range1.first, Range1.second) == 1)
|
|
|
|
return Range1.first->m_pReplacement;
|
2020-09-13 20:25:37 +00:00
|
|
|
|
2022-05-23 18:57:43 +00:00
|
|
|
const unsigned DefaultHash = str_quickhash("");
|
|
|
|
if(ContextHash != DefaultHash)
|
2020-09-13 20:25:37 +00:00
|
|
|
{
|
2022-05-23 18:57:43 +00:00
|
|
|
// Do another lookup with the default context hash
|
|
|
|
String.m_ContextHash = DefaultHash;
|
2022-06-11 19:38:18 +00:00
|
|
|
auto Range2 = std::equal_range(m_vStrings.begin(), m_vStrings.end(), String);
|
2022-05-23 18:57:43 +00:00
|
|
|
if(std::distance(Range2.first, Range2.second) == 1)
|
|
|
|
return Range2.first->m_pReplacement;
|
2020-09-13 20:25:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 18:57:43 +00:00
|
|
|
return nullptr;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
2009-06-15 06:45:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CLocalizationDatabase g_Localization;
|