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 /**
21 * Country code in ISO 3166-1 numeric.
22 */
23 int m_CountryCode;
24 std::vector<std::string> m_vLanguageCodes;
25
26 bool operator<(const CLanguage &Other) const { return m_Name < Other.m_Name; }
27};
28
29class CLocalizationDatabase
30{
31 class CString
32 {
33 public:
34 unsigned m_Hash;
35 unsigned m_ContextHash;
36 const char *m_pReplacement;
37
38 CString() = default;
39 CString(unsigned Hash, unsigned ContextHash, const char *pReplacement) :
40 m_Hash(Hash), m_ContextHash(ContextHash), m_pReplacement(pReplacement)
41 {
42 }
43
44 bool operator<(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash < Other.m_ContextHash); }
45 bool operator<=(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash <= Other.m_ContextHash); }
46 bool operator==(const CString &Other) const { return m_Hash == Other.m_Hash && m_ContextHash == Other.m_ContextHash; }
47 };
48
49 std::vector<CLanguage> m_vLanguages;
50 std::vector<CString> m_vStrings;
51 CHeap m_StringsHeap;
52
53public:
54 void LoadIndexfile(class IStorage *pStorage, class IConsole *pConsole);
55 const std::vector<CLanguage> &Languages() const { return m_vLanguages; }
56 void SelectDefaultLanguage(class IConsole *pConsole, char *pFilename, size_t Length) const;
57
58 bool Load(const char *pFilename, class IStorage *pStorage, class IConsole *pConsole);
59
60 void AddString(const char *pOrgStr, const char *pNewStr, const char *pContext);
61 const char *FindString(unsigned Hash, unsigned ContextHash) const;
62};
63
64extern CLocalizationDatabase g_Localization;
65
66[[gnu::format_arg(1)]] extern const char *Localize(const char *pStr, const char *pContext = "");
67
68#endif
69