-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization.cpp
More file actions
69 lines (58 loc) · 2.08 KB
/
localization.cpp
File metadata and controls
69 lines (58 loc) · 2.08 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
#include <wx/fileconf.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/log.h>
#include <wx/filefn.h>
// ЛОКАЛИЗАЦИЯ НА НУЖНЫЙ ЯЗЫК
bool InitializeLocalization()
{
// Определение каталога с переводами и файла настроек
wxString configFile = wxGetCwd() + wxFILE_SEP_PATH + "settings.ini";
wxString langDirPath = wxGetCwd() + wxFILE_SEP_PATH + "lang";
// Создание файла настроек, если его нет
if (!wxFileExists(configFile)) {
wxFileConfig createConfig("MyApp", wxEmptyString, configFile, wxEmptyString,
wxCONFIG_USE_LOCAL_FILE);
createConfig.Write("General/language", "ru");
createConfig.Flush();
}
// Создание папки lang, если она отсутствует
if (!wxDirExists(langDirPath)) {
wxMkdir(langDirPath);
}
// Читаем выбранный язык из конфига
wxFileConfig config("MyApp", wxEmptyString, configFile, wxEmptyString, wxCONFIG_USE_LOCAL_FILE);
wxString langStr;
config.Read("General/language", &langStr, "ru");
// Определяем код языка
const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo(langStr);
int languageCode = langInfo ? langInfo->Language : wxLANGUAGE_RUSSIAN;
// Создаём и инициализируем локаль
wxLocale* locale = new wxLocale;
if (!locale->Init(languageCode)) {
if (!locale->Init(wxLANGUAGE_ENGLISH)) {
delete locale;
return false;
}
}
// Сканирование папки с локализациями
wxLocale::AddCatalogLookupPathPrefix(langDirPath);
wxDir dir(langDirPath);
if (dir.IsOpened()) {
wxString fileName;
// Ищем подкаталоги
bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
while (cont) {
locale->AddCatalog(fileName);
cont = dir.GetNext(&fileName);
}
// Ищем .mo файлы
cont = dir.GetFirst(&fileName, "*.mo", wxDIR_FILES);
while (cont) {
wxFileName fn(fileName);
locale->AddCatalog(fn.GetName());
cont = dir.GetNext(&fileName);
}
}
return true;
}