-
Notifications
You must be signed in to change notification settings - Fork 66
/
I18n.hpp
74 lines (63 loc) · 1.55 KB
/
I18n.hpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include "FnvHash.hpp"
std::unordered_map<uint32_t, const wchar_t*> hashToStrMap;
#pragma pack(push, 1)
struct YMOData
{
uint16_t len;
struct
{
uint32_t hash;
uint16_t offset;
} table[1];
};
#pragma pack(pop)
void LoadTranslateData()
{
auto hRes = FindResourceExW(g_hInst, L"YMO", MAKEINTRESOURCEW(1), GetThreadUILanguage());
if (hRes)
{
auto hResData = LoadResource(g_hInst, hRes);
if (hResData)
{
auto ymo = reinterpret_cast<const YMOData*>(LockResource(hResData));
if (ymo)
{
hashToStrMap.reserve(ymo->len);
for (int i = 0; i < ymo->len; ++i)
{
auto hash = ymo->table[i].hash;
auto offset = ymo->table[i].offset;
auto str = reinterpret_cast<const wchar_t*>(reinterpret_cast<const uint8_t*>(hResData) + offset);
hashToStrMap.emplace(hash, str);
}
}
}
}
}
const wchar_t* Translate(const wchar_t* str)
{
static std::unordered_map<const wchar_t*, const wchar_t*> ptrToStrMap;
auto translation = str;
auto i = ptrToStrMap.find(str);
if (i == ptrToStrMap.end())
{
auto hash = fnv1a_32(str, wcslen(str) * sizeof(wchar_t));
auto j = hashToStrMap.find(hash);
if (j != hashToStrMap.end())
translation = j->second;
ptrToStrMap.emplace(str, translation);
}
else
translation = i->second;
return translation;
}
const wchar_t* TranslateContext(const wchar_t* str, const wchar_t* ctxtStr)
{
auto translation = Translate(ctxtStr);
if (translation == ctxtStr)
return str;
return translation;
}
#define _(str) Translate(str)
#define C_(ctxt, str) TranslateContext(str, ctxt L"\004" str)