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 <base/system.h> // GNUC_ATTRIBUTE |
7 | |
8 | #include <engine/shared/memheap.h> |
9 | |
10 | #include <string> |
11 | #include <vector> |
12 | |
13 | class CLanguage |
14 | { |
15 | public: |
16 | CLanguage() = default; |
17 | CLanguage(const char *pName, const char *pFileName, int Code, const std::vector<std::string> &vLanguageCodes) : |
18 | m_Name(pName), m_FileName(pFileName), m_CountryCode(Code), m_vLanguageCodes(vLanguageCodes) {} |
19 | |
20 | std::string m_Name; |
21 | std::string m_FileName; |
22 | int m_CountryCode; |
23 | std::vector<std::string> m_vLanguageCodes; |
24 | |
25 | bool operator<(const CLanguage &Other) const { return m_Name < Other.m_Name; } |
26 | }; |
27 | |
28 | class CLocalizationDatabase |
29 | { |
30 | class CString |
31 | { |
32 | public: |
33 | unsigned m_Hash; |
34 | unsigned m_ContextHash; |
35 | const char *m_pReplacement; |
36 | |
37 | CString() {} |
38 | CString(unsigned Hash, unsigned ContextHash, const char *pReplacement) : |
39 | m_Hash(Hash), m_ContextHash(ContextHash), m_pReplacement(pReplacement) |
40 | { |
41 | } |
42 | |
43 | bool operator<(const CString &Other) const { return m_Hash < Other.m_Hash || (m_Hash == Other.m_Hash && m_ContextHash < Other.m_ContextHash); } |
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_ContextHash == Other.m_ContextHash; } |
46 | }; |
47 | |
48 | std::vector<CLanguage> m_vLanguages; |
49 | std::vector<CString> m_vStrings; |
50 | CHeap m_StringsHeap; |
51 | |
52 | public: |
53 | void LoadIndexfile(class IStorage *pStorage, class IConsole *pConsole); |
54 | const std::vector<CLanguage> &Languages() const { return m_vLanguages; } |
55 | void SelectDefaultLanguage(class IConsole *pConsole, char *pFilename, size_t Length) const; |
56 | |
57 | bool Load(const char *pFilename, class IStorage *pStorage, class IConsole *pConsole); |
58 | |
59 | void AddString(const char *pOrgStr, const char *pNewStr, const char *pContext); |
60 | const char *FindString(unsigned Hash, unsigned ContextHash) const; |
61 | }; |
62 | |
63 | extern CLocalizationDatabase g_Localization; |
64 | |
65 | extern const char *Localize(const char *pStr, const char *pContext = "" ) |
66 | GNUC_ATTRIBUTE((format_arg(1))); |
67 | #endif |
68 | |