-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtb_language.cpp
58 lines (50 loc) · 1.29 KB
/
tb_language.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================
#include "tb_language.h"
#include "tb_system.h"
#include "tb_node_tree.h"
namespace tb {
TBLanguage::~TBLanguage()
{
Clear();
}
bool TBLanguage::Load(const char *filename)
{
// Read the file into a node tree (even though it's only a flat list)
TBNode node;
if (!node.ReadFile(filename))
return false;
// Go through all nodes and add to the strings hash table
TBNode *n = node.GetFirstChild();
while (n)
{
const char *str = n->GetValue().GetString();
TBStr *new_str = new TBStr(str);
if (!new_str || !strings.Add(TBID(n->GetName()), new_str))
{
delete new_str;
return false;
}
n = n->GetNext();
}
return true;
}
void TBLanguage::Clear()
{
strings.DeleteAll();
}
const char *TBLanguage::GetString(const TBID &id)
{
if (TBStr *str = strings.Get(id))
return *str;
#ifdef TB_RUNTIME_DEBUG_INFO
static TBStr tmp;
tmp.SetFormatted("<TRANSLATE:%s>", id.debug_string.CStr());
return tmp;
#else
return "<TRANSLATE!>";
#endif
}
} // namespace tb