ddnet/src/game/localization.cpp

150 lines
3.9 KiB
C++
Raw Normal View History

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
#include <engine/console.h>
#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
const char *Localize(const char *pStr, const char *pContext)
2009-06-13 16:54:04 +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
}
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();
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
{
m_VersionCounter = 0;
2010-05-29 07:25:38 +00:00
m_CurrentVersion = 0;
2009-06-13 16:54:04 +00:00
}
void CLocalizationDatabase::AddString(const char *pOrgStr, const char *pNewStr, const char *pContext)
2009-06-13 16:54:04 +00:00
{
2010-05-29 07:25:38 +00:00
CString s;
s.m_Hash = str_quickhash(pOrgStr);
s.m_ContextHash = str_quickhash(pContext);
s.m_pReplacement = m_StringsHeap.StoreString(*pNewStr ? pNewStr : pOrgStr);
m_Strings.push_back(s);
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
{
2010-05-29 07:25:38 +00:00
m_Strings.clear();
m_StringsHeap.Reset();
2010-05-29 07:25:38 +00:00
m_CurrentVersion = 0;
2009-06-15 08:15:53 +00:00
return true;
}
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;
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "loaded '%s'", pFilename);
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
2010-05-29 07:25:38 +00:00
m_Strings.clear();
m_StringsHeap.Reset();
char aContext[512];
char aOrigin[512];
2010-05-29 07:25:38 +00:00
CLineReader LineReader;
LineReader.Init(IoHandle);
char *pLine;
int Line = 0;
2010-05-29 07:25:38 +00:00
while((pLine = LineReader.Get()))
2009-06-13 17:18:06 +00:00
{
Line++;
2010-05-29 07:25:38 +00:00
if(!str_length(pLine))
2009-06-13 17:18:06 +00:00
continue;
2010-05-29 07:25:38 +00:00
if(pLine[0] == '#') // skip comments
2009-06-13 17:18:06 +00:00
continue;
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';
}
str_copy(aOrigin, pLine, sizeof(aOrigin));
2010-05-29 07:25:38 +00:00
char *pReplacement = LineReader.Get();
Line++;
2010-05-29 07:25:38 +00:00
if(!pReplacement)
2009-06-13 17:18:06 +00:00
{
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "unexpected end of file");
2009-06-13 17:18:06 +00:00
break;
}
2010-05-29 07:25:38 +00:00
if(pReplacement[0] != '=' || pReplacement[1] != '=' || pReplacement[2] != ' ')
2009-06-13 17:18:06 +00:00
{
str_format(aBuf, sizeof(aBuf), "malform replacement line (%d) for '%s'", Line, aOrigin);
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;
AddString(aOrigin, pReplacement, aContext);
2009-06-13 17:18:06 +00:00
}
2011-01-19 21:14:40 +00:00
io_close(IoHandle);
std::sort(m_Strings.begin(), m_Strings.end());
m_CurrentVersion = ++m_VersionCounter;
2009-06-13 17:18:06 +00:00
return true;
2009-06-13 16:54:04 +00:00
}
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;
String.m_ContextHash = ContextHash;
String.m_pReplacement = 0x0;
auto Range1 = std::equal_range(m_Strings.begin(), m_Strings.end(), String);
if(std::distance(Range1.first, Range1.second) == 1)
return Range1.first->m_pReplacement;
const unsigned DefaultHash = str_quickhash("");
if(ContextHash != DefaultHash)
{
// Do another lookup with the default context hash
String.m_ContextHash = DefaultHash;
auto Range2 = std::equal_range(m_Strings.begin(), m_Strings.end(), String);
if(std::distance(Range2.first, Range2.second) == 1)
return Range2.first->m_pReplacement;
}
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;