Implement context for translations (largely taken from Teeworlds)

Missing: scripts/languages doesn't work yet, but the context can be
added manually, see german.txt for an example.
This commit is contained in:
def 2020-09-13 22:25:37 +02:00
parent 393f3deed4
commit 7af517009c
4 changed files with 56 additions and 18 deletions

View file

@ -324,6 +324,10 @@ Ping
Pistol
== Pistole
Play
== Spielen
[Demo browser]
Play
== Abspielen

View file

@ -1245,7 +1245,7 @@ void CMenus::RenderDemoList(CUIRect MainView)
}
static int s_PlayButton = 0;
if(DoButton_Menu(&s_PlayButton, m_DemolistSelectedIsDir?Localize("Open"):Localize("Play"), 0, &PlayRect) || Activated || Input()->KeyPress(KEY_P))
if(DoButton_Menu(&s_PlayButton, m_DemolistSelectedIsDir ? Localize("Open") : Localize("Play", "Demo browser"), 0, &PlayRect) || Activated || Input()->KeyPress(KEY_P))
{
if(m_DemolistSelectedIndex >= 0)
{

View file

@ -8,13 +8,13 @@
#include <engine/console.h>
#include <engine/storage.h>
const char *Localize(const char *pStr)
const char *Localize(const char *pStr, const char *pContext)
{
const char *pNewStr = g_Localization.FindString(str_quickhash(pStr));
const char *pNewStr = g_Localization.FindString(str_quickhash(pStr), str_quickhash(pContext));
return pNewStr ? pNewStr : pStr;
}
CLocConstString::CLocConstString(const char *pStr)
CLocConstString::CLocConstString(const char *pStr, const char *pContext)
{
m_pDefaultStr = pStr;
m_Hash = str_quickhash(m_pDefaultStr);
@ -24,7 +24,7 @@ CLocConstString::CLocConstString(const char *pStr)
void CLocConstString::Reload()
{
m_Version = g_Localization.Version();
const char *pNewStr = g_Localization.FindString(m_Hash);
const char *pNewStr = g_Localization.FindString(m_Hash, m_ContextHash);
m_pCurrentStr = pNewStr;
if(!m_pCurrentStr)
m_pCurrentStr = m_pDefaultStr;
@ -36,10 +36,11 @@ CLocalizationDatabase::CLocalizationDatabase()
m_CurrentVersion = 0;
}
void CLocalizationDatabase::AddString(const char *pOrgStr, const char *pNewStr)
void CLocalizationDatabase::AddString(const char *pOrgStr, const char *pNewStr, const char *pContext)
{
CString s;
s.m_Hash = str_quickhash(pOrgStr);
s.m_ContextHash = str_quickhash(pContext);
s.m_Replacement = *pNewStr ? pNewStr : pOrgStr;
m_Strings.add(s);
}
@ -63,6 +64,7 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
m_Strings.clear();
char aContext[512];
char aOrigin[512];
CLineReader LineReader;
LineReader.Init(IoHandle);
@ -77,6 +79,23 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
if(pLine[0] == '#') // skip comments
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));
char *pReplacement = LineReader.Get();
Line++;
@ -94,7 +113,7 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
}
pReplacement += 3;
AddString(aOrigin, pReplacement);
AddString(aOrigin, pReplacement, aContext);
}
io_close(IoHandle);
@ -102,14 +121,27 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
return true;
}
const char *CLocalizationDatabase::FindString(unsigned Hash)
const char *CLocalizationDatabase::FindString(unsigned Hash, unsigned ContextHash)
{
CString String;
String.m_Hash = Hash;
String.m_ContextHash = ContextHash;
sorted_array<CString>::range r = ::find_binary(m_Strings.all(), String);
if(r.empty())
return 0;
return r.front().m_Replacement;
unsigned DefaultHash = str_quickhash("");
unsigned DefaultIndex = 0;
for(unsigned i = 0; i < r.size() && r.index(i).m_Hash == Hash; ++i)
{
const CString &rStr = r.index(i);
if(rStr.m_ContextHash == ContextHash)
return rStr.m_Replacement;
else if(rStr.m_ContextHash == DefaultHash)
DefaultIndex = i;
}
return r.index(DefaultIndex).m_Replacement;
}
CLocalizationDatabase g_Localization;

View file

@ -11,13 +11,14 @@ class CLocalizationDatabase
{
public:
unsigned m_Hash;
unsigned m_ContextHash;
// TODO: do this as an const char * and put everything on a incremental heap
string m_Replacement;
bool operator <(const CString &Other) const { return m_Hash < Other.m_Hash; }
bool operator <=(const CString &Other) const { return m_Hash <= Other.m_Hash; }
bool operator ==(const CString &Other) const { return m_Hash == Other.m_Hash; }
bool operator<(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash < Other.m_ContextHash); }
bool operator<=(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash <= Other.m_ContextHash); }
bool operator==(const CString &Other) const { return m_Hash == Other.m_Hash && m_ContextHash == Other.m_ContextHash; }
};
sorted_array<CString> m_Strings;
@ -31,8 +32,8 @@ public:
int Version() { return m_CurrentVersion; }
void AddString(const char *pOrgStr, const char *pNewStr);
const char *FindString(unsigned Hash);
void AddString(const char *pOrgStr, const char *pNewStr, const char *pContext);
const char *FindString(unsigned Hash, unsigned ContextHash);
};
extern CLocalizationDatabase g_Localization;
@ -42,9 +43,11 @@ class CLocConstString
const char *m_pDefaultStr;
const char *m_pCurrentStr;
unsigned m_Hash;
unsigned m_ContextHash;
int m_Version;
public:
CLocConstString(const char *pStr);
CLocConstString(const char *pStr, const char *pContext = "");
void Reload();
inline operator const char *()
@ -55,7 +58,6 @@ public:
}
};
extern const char *Localize(const char *pStr)
GNUC_ATTRIBUTE((format_arg(1)));
extern const char *Localize(const char *pStr, const char *pContext = "")
GNUC_ATTRIBUTE((format_arg(1)));
#endif