ddnet/src/game/localization.cpp

106 lines
2.3 KiB
C++
Raw Normal View History

2009-06-13 16:54:04 +00:00
2010-05-29 07:25:38 +00:00
#include "localization.h"
#include <base/tl/algorithm.h>
2009-06-13 16:54:04 +00:00
2010-05-29 07:25:38 +00:00
#include <engine/shared/linereader.h>
#include <engine/console.h>
2009-06-13 17:18:06 +00:00
2010-05-29 07:25:38 +00:00
const char *Localize(const char *pStr)
2009-06-13 16:54:04 +00:00
{
2010-05-29 07:25:38 +00:00
const char *pNewStr = g_Localization.FindString(str_quickhash(pStr));
return pNewStr ? pNewStr : pStr;
2009-06-13 16:54:04 +00:00
}
2010-05-29 07:25:38 +00:00
CLocConstString::CLocConstString(const char *pStr)
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_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-05-29 07:25:38 +00:00
m_CurrentVersion = 0;
2009-06-13 16:54:04 +00:00
}
2010-05-29 07:25:38 +00:00
void CLocalizationDatabase::AddString(const char *pOrgStr, const char *pNewStr)
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_Replacement = pNewStr;
m_Strings.add(s);
2009-06-13 17:18:06 +00:00
}
bool CLocalizationDatabase::Load(const char *pFilename, 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_CurrentVersion = 0;
2009-06-15 08:15:53 +00:00
return true;
}
2010-05-29 07:25:38 +00:00
IOHANDLE IoHandle = io_open(pFilename, IOFLAG_READ);
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();
2009-06-13 17:18:06 +00:00
2010-05-29 07:25:38 +00:00
CLineReader LineReader;
LineReader.Init(IoHandle);
char *pLine;
while((pLine = LineReader.Get()))
2009-06-13 17:18:06 +00:00
{
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;
2010-05-29 07:25:38 +00:00
char *pReplacement = LineReader.Get();
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 for '%s'", pLine);
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(pLine, pReplacement);
2009-06-13 17:18:06 +00:00
}
2009-06-15 06:45:44 +00:00
2010-05-29 07:25:38 +00:00
m_CurrentVersion++;
2009-06-13 17:18:06 +00:00
return true;
2009-06-13 16:54:04 +00:00
}
2010-05-29 07:25:38 +00:00
const char *CLocalizationDatabase::FindString(unsigned Hash)
2009-06-13 16:54:04 +00:00
{
2010-05-29 07:25:38 +00:00
CString String;
String.m_Hash = Hash;
sorted_array<CString>::range r = ::find_binary(m_Strings.all(), String);
2009-06-13 16:54:04 +00:00
if(r.empty())
return 0;
2010-05-29 07:25:38 +00:00
return r.front().m_Replacement;
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;