Skip to content

Commit

Permalink
added optional context to a localization string to allow multiple tra…
Browse files Browse the repository at this point in the history
…nslations. Closes #1082
  • Loading branch information
oy committed Jun 4, 2013
1 parent 4a88f93 commit acaaa97
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion data/languages
Submodule languages updated 1 files
+5 −0 brazilian_portuguese.json
2 changes: 1 addition & 1 deletion src/game/client/components/menus_demo.cpp
Expand Up @@ -474,7 +474,7 @@ void CMenus::RenderDemoList(CUIRect MainView)
BottomView.VSplitLeft(Spacing, 0, &BottomView);
BottomView.VSplitLeft(ButtonWidth, &Button, &BottomView);
static int s_PlayButton = 0;
if(DoButton_Menu(&s_PlayButton, m_DemolistSelectedIsDir?Localize("Open"):Localize("Play"), 0, &Button) || Activated)
if(DoButton_Menu(&s_PlayButton, m_DemolistSelectedIsDir?Localize("Open"):Localize("Play", "DemoBrowser"), 0, &Button) || Activated)
{
if(m_DemolistSelectedIndex >= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/gameclient.h
Expand Up @@ -305,6 +305,6 @@ inline vec3 HslToRgb(vec3 HSL)
}


extern const char *Localize(const char *Str);
extern const char *Localize(const char *Str, const char *pContext="");

#endif
30 changes: 22 additions & 8 deletions src/game/client/localization.cpp
Expand Up @@ -8,23 +8,24 @@
#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);
m_ContextHash = str_quickhash(pContext);
m_Version = -1;
}

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;
Expand All @@ -36,10 +37,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);
}
Expand Down Expand Up @@ -87,7 +89,7 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
if(rStart.type == json_array)
{
for(unsigned i = 0; i < rStart.u.array.length; ++i)
AddString((const char *)rStart[i]["or"], (const char *)rStart[i]["tr"]);
AddString((const char *)rStart[i]["or"], (const char *)rStart[i]["tr"], (const char *)rStart[i]["context"]);
}

// clean up
Expand All @@ -97,14 +99,26 @@ 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;
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(); ++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;
11 changes: 6 additions & 5 deletions src/game/client/localization.h
Expand Up @@ -11,6 +11,7 @@ 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;
Expand All @@ -31,8 +32,8 @@ class CLocalizationDatabase

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;
Expand All @@ -42,9 +43,10 @@ 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 *()
Expand All @@ -55,6 +57,5 @@ class CLocConstString
}
};


extern const char *Localize(const char *pStr);
extern const char *Localize(const char *pStr, const char *pContext);
#endif

0 comments on commit acaaa97

Please sign in to comment.