-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageManager.cpp
More file actions
108 lines (87 loc) · 3.04 KB
/
LanguageManager.cpp
File metadata and controls
108 lines (87 loc) · 3.04 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "LanguageManager.h"
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include <wx/log.h>
#ifdef __WINDOWS__
#include <wx/msw/private.h>
#endif
// For simple JSON parsing without external dependencies,
// since we know the structure is simple.
LanguageManager& LanguageManager::Get() {
static LanguageManager instance;
return instance;
}
bool LanguageManager::LoadLanguage(const wxString& langCode) {
wxString content;
bool loaded = false;
#ifdef __WINDOWS__
// Try to load from resources on Windows
wxString resName = "ID_I18N_" + langCode.Upper();
HRSRC hRes = FindResource(wxGetInstance(), resName.t_str(), RT_RCDATA);
if (hRes) {
HGLOBAL hData = LoadResource(wxGetInstance(), hRes);
if (hData) {
DWORD size = SizeofResource(wxGetInstance(), hRes);
const char* ptr = (const char*)LockResource(hData);
if (ptr && size > 0) {
content = wxString::FromUTF8(ptr, size);
loaded = !content.IsEmpty();
}
}
}
#endif
if (!loaded) {
wxString exePath = wxStandardPaths::Get().GetExecutablePath();
wxFileName fn(exePath);
wxString resPath = fn.GetPath();
#ifdef __WXOSX__
// On macOS, resources are in Contents/Resources
if (resPath.Contains(".app/Contents/MacOS")) {
resPath = resPath.BeforeLast('/') + "/Resources";
}
#endif
wxString filePath = resPath + "/i18n/" + langCode + ".json";
// Fallback for development (if i18n is in the current directory)
if (!wxFile::Exists(filePath)) {
filePath = "i18n/" + langCode + ".json";
}
if (wxFile::Exists(filePath)) {
wxFile file(filePath);
file.ReadAll(&content);
loaded = !content.IsEmpty();
}
}
if (!loaded) {
wxLogError("Language file not found or empty for: %s", langCode);
return false;
}
m_strings.clear();
m_currentLang = langCode;
// Very basic JSON parser for "key": "value" pairs
size_t pos = 0;
while ((pos = content.find('"', pos)) != wxString::npos) {
size_t keyStart = pos + 1;
size_t keyEnd = content.find('"', keyStart);
if (keyEnd == wxString::npos) break;
wxString key = content.Mid(keyStart, keyEnd - keyStart);
pos = content.find(':', keyEnd);
if (pos == wxString::npos) break;
pos = content.find('"', pos);
if (pos == wxString::npos) break;
size_t valStart = pos + 1;
size_t valEnd = content.find('"', valStart);
if (valEnd == wxString::npos) break;
wxString val = content.Mid(valStart, valEnd - valStart);
m_strings[key] = val;
pos = valEnd + 1;
}
return !m_strings.empty();
}
wxString LanguageManager::GetString(const wxString& key) const {
auto it = m_strings.find(key);
if (it != m_strings.end()) {
return it->second;
}
return key;
}