1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_LOCALIZATION_H
4#define GAME_LOCALIZATION_H
5
6#include <engine/shared/memheap.h>
7
8#include <string>
9#include <vector>
10
11class CLanguage
12{
13public:
14 CLanguage() = default;
15 CLanguage(const char *pName, const char *pFilename, int Code, const std::vector<std::string> &vLanguageCodes) :
16 m_Name(pName), m_Filename(pFilename), m_CountryCode(Code), m_vLanguageCodes(vLanguageCodes) {}
17
18 std::string m_Name;
19 std::string m_Filename;
20 int m_CountryCode;
21 std::vector<std::string> m_vLanguageCodes;
22
23 bool operator<(const CLanguage &Other) const { return m_Name < Other.m_Name; }
24};
25
26class CLocalizationDatabase
27{
28 class CString
29 {
30 public:
31 unsigned m_Hash;
32 unsigned m_ContextHash;
33 const char *m_pReplacement;
34
35 CString() = default;
36 CString(unsigned Hash, unsigned ContextHash, const char *pReplacement) :
37 m_Hash(Hash), m_ContextHash(ContextHash), m_pReplacement(pReplacement)
38 {
39 }
40
41 bool operator<(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash < Other.m_ContextHash); }
42 bool operator<=(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash <= Other.m_ContextHash); }
43 bool operator==(const CString &Other) const { return m_Hash == Other.m_Hash && m_ContextHash == Other.m_ContextHash; }
44 };
45
46 std::vector<CLanguage> m_vLanguages;
47 std::vector<CString> m_vStrings;
48 CHeap m_StringsHeap;
49
50public:
51 void LoadIndexfile(class IStorage *pStorage, class IConsole *pConsole);
52 const std::vector<CLanguage> &Languages() const { return m_vLanguages; }
53 void SelectDefaultLanguage(class IConsole *pConsole, char *pFilename, size_t Length) const;
54
55 bool Load(const char *pFilename, class IStorage *pStorage, class IConsole *pConsole);
56
57 void AddString(const char *pOrgStr, const char *pNewStr, const char *pContext);
58 const char *FindString(unsigned Hash, unsigned ContextHash) const;
59};
60
61extern CLocalizationDatabase g_Localization;
62
63[[gnu::format_arg(1)]] extern const char *Localize(const char *pStr, const char *pContext = "");
64
65#endif
66