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. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#ifndef GAME_LOCALIZATION_H
|
|
|
|
#define GAME_LOCALIZATION_H
|
2022-03-06 15:35:40 +00:00
|
|
|
|
2022-05-23 18:57:43 +00:00
|
|
|
#include <base/system.h> // GNUC_ATTRIBUTE
|
2022-03-06 15:35:40 +00:00
|
|
|
#include <engine/shared/memheap.h>
|
2022-05-23 18:57:43 +00:00
|
|
|
#include <vector>
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
class CLocalizationDatabase
|
|
|
|
{
|
|
|
|
class CString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned m_Hash;
|
2020-09-13 20:25:37 +00:00
|
|
|
unsigned m_ContextHash;
|
2022-03-06 15:35:40 +00:00
|
|
|
const char *m_pReplacement;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-05-24 19:18:56 +00:00
|
|
|
CString() {}
|
|
|
|
CString(unsigned Hash, unsigned ContextHash, const char *pReplacement) :
|
|
|
|
m_Hash(Hash), m_ContextHash(ContextHash), m_pReplacement(pReplacement)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
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; }
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
2022-05-23 18:57:43 +00:00
|
|
|
std::vector<CString> m_Strings;
|
2022-03-06 15:35:40 +00:00
|
|
|
CHeap m_StringsHeap;
|
2010-11-03 01:15:39 +00:00
|
|
|
int m_VersionCounter;
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_CurrentVersion;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
CLocalizationDatabase();
|
|
|
|
|
2010-10-06 21:07:35 +00:00
|
|
|
bool Load(const char *pFilename, class IStorage *pStorage, class IConsole *pConsole);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-05-06 21:24:58 +00:00
|
|
|
int Version() const { return m_CurrentVersion; }
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
void AddString(const char *pOrgStr, const char *pNewStr, const char *pContext);
|
2022-05-06 21:24:58 +00:00
|
|
|
const char *FindString(unsigned Hash, unsigned ContextHash) const;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern CLocalizationDatabase g_Localization;
|
|
|
|
|
|
|
|
class CLocConstString
|
|
|
|
{
|
|
|
|
const char *m_pDefaultStr;
|
|
|
|
const char *m_pCurrentStr;
|
|
|
|
unsigned m_Hash;
|
2020-09-13 20:25:37 +00:00
|
|
|
unsigned m_ContextHash;
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_Version;
|
2020-09-13 20:25:37 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
2020-09-13 20:25:37 +00:00
|
|
|
CLocConstString(const char *pStr, const char *pContext = "");
|
2010-05-29 07:25:38 +00:00
|
|
|
void Reload();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
inline operator const char *()
|
|
|
|
{
|
|
|
|
if(m_Version != g_Localization.Version())
|
|
|
|
Reload();
|
|
|
|
return m_pCurrentStr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-13 20:25:37 +00:00
|
|
|
extern const char *Localize(const char *pStr, const char *pContext = "")
|
|
|
|
GNUC_ATTRIBUTE((format_arg(1)));
|
2010-05-29 07:25:38 +00:00
|
|
|
#endif
|