-
Notifications
You must be signed in to change notification settings - Fork 12
/
Locale.cpp
212 lines (176 loc) · 4.98 KB
/
Locale.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "Locale.h"
#include "Courseplay_EditorApp.h"
#include "Settings.h"
#include <wx/dir.h>
#include <wx/filename.h>
Locale::Locale(Settings *config)
{
m_Config = config;
m_SearchPath = wxPathOnly(wxGetApp().argv[0]);
m_ConfigPath = wxT("Locale");
m_Locale = NULL;
selectedLangId = wxLANGUAGE_UNKNOWN;
shortLangName = wxT("en");
}
Locale::~Locale()
{
if(m_Locale)
{
wxDELETE(m_Locale);
}
}
void Locale::getInstalledLanguages(LangList &langList, bool addDefault)
{
langList.names.Clear();
langList.fileNames.Clear();
langList.identifiers.Clear();
wxString filename;
const wxLanguageInfo * langinfo;
if (addDefault)
{
wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT);
if(!name.IsEmpty())
{
langList.names.Add(_("Default"));
langList.fileNames.Add(wxGetApp().GetAppName());
langList.identifiers.Add(wxLANGUAGE_DEFAULT);
}
}
if(!wxDir::Exists(m_SearchPath))
return;
wxDir dir(m_SearchPath);
for(bool cont = dir.GetFirst(&filename,
#ifdef __WXMSW__
wxT("*.*"),
#else
wxT("*"),
#endif
wxDIR_DEFAULT);
cont; cont = dir.GetNext(&filename))
{
wxFileName tempFile(dir.GetName()+wxFileName::GetPathSeparator()+filename);
if (tempFile.GetExt() == wxT("mo") && tempFile.FileExists())
{
langinfo = wxLocale::FindLanguageInfo(tempFile.GetName());
if(langinfo != NULL)
{
langList.names.Add(langinfo->Description);
langList.fileNames.Add(tempFile.GetName());
langList.identifiers.Add(langinfo->Language);
}
}
}
}
bool Locale::askUserForLanguage(wxString msg, wxString caption)
{
LangList langList;
getInstalledLanguages(langList);
if (langList.names.Count() == langList.identifiers.Count() && langList.fileNames.Count() == langList.identifiers.Count())
{
long index = wxGetSingleChoiceIndex(msg, caption, langList.names);
if(index != -1)
{
if(m_Locale)
{
wxDELETE(m_Locale);
}
m_Locale = new wxLocale;
m_Locale->Init(langList.identifiers[index]);
m_Locale->AddCatalogLookupPathPrefix(m_SearchPath);
m_Locale->AddCatalog(langList.fileNames[index]);
selectedLangId = langList.identifiers[index];
return true;
}
}
return false;
}
bool Locale::loadLocale()
{
long language;
wxString regString = wxString::Format(wxT("/%s/Language"), m_ConfigPath.c_str());
if(!m_Config->Read(regString, &language, wxLANGUAGE_UNKNOWN))
{
language = wxLANGUAGE_UNKNOWN;
}
if(language == wxLANGUAGE_UNKNOWN)
{
return false;
}
LangList langList;
getInstalledLanguages(langList);
for(size_t i = 0; i < langList.identifiers.Count(); i++)
{
if(langList.identifiers[i] == language)
{
if(m_Locale) wxDELETE(m_Locale);
m_Locale = new wxLocale;
m_Locale->Init(langList.identifiers[i]);
m_Locale->AddCatalogLookupPathPrefix(m_SearchPath);
m_Locale->AddCatalog(langList.fileNames[i]);
shortLangName = langList.fileNames[i];
selectedLangId = langList.identifiers[i];
return true;
}
}
return false;
}
bool Locale::loadDefaultLocaleIfExists()
{
LangList langList;
getInstalledLanguages(langList);
long defaultLangID = wxLocale::GetSystemLanguage();
if (defaultLangID != wxLANGUAGE_UNKNOWN)
{
for(size_t i = 0; i < langList.identifiers.Count(); i++)
{
if(langList.identifiers[i] == defaultLangID)
{
if(m_Locale) wxDELETE(m_Locale);
m_Locale = new wxLocale;
m_Locale->Init(langList.identifiers[i]);
m_Locale->AddCatalogLookupPathPrefix(m_SearchPath);
m_Locale->AddCatalog(langList.fileNames[i]);
shortLangName = langList.fileNames[i];
selectedLangId = langList.identifiers[i];
return true;
}
}
}
return false;
}
void Locale::saveLocale(bool bReset)
{
long language = wxLANGUAGE_UNKNOWN;
if(!bReset)
{
if(m_Locale)
{
language = m_Locale->GetLanguage();
}
}
wxString regString = wxString::Format(wxT("/%s/Language"), m_ConfigPath.c_str());
m_Config->Write(regString, language);
m_Config->Flush();
}
void Locale::setLocaleSearchPath(wxString langPath)
{
if(langPath.IsEmpty())
m_SearchPath = wxPathOnly(wxGetApp().argv[0]);
else
{
wxFileName appFileName(wxGetApp().argv[0]);
appFileName.Normalize(wxPATH_NORM_DOTS|wxPATH_NORM_ABSOLUTE|wxPATH_NORM_TILDE);
m_SearchPath = appFileName.GetPath() + appFileName.GetPathSeparator() + langPath;
}
}
void Locale::setLocaleConfigPath(wxString value)
{
if (!value.IsEmpty())
m_ConfigPath = value;
}
void Locale::setSelectedLangId(wxLanguage lang)
{
selectedLangId = lang;
wxString regString = wxString::Format(wxT("/%s/Language"), m_ConfigPath.c_str());
m_Config->Write(regString, selectedLangId);
}