-
Notifications
You must be signed in to change notification settings - Fork 6
/
LauGettext.cpp
113 lines (82 loc) · 2.52 KB
/
LauGettext.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
#include "LauGettext.h"
#if defined(_WINDOWS) || defined(WIN32)
#define PATH_SEPARATOR "\\"
#else
#define PATH_SEPARATOR "/"
#endif
#include <fstream>
#include <iostream>
LauGettext* LauGettext::instance_ = NULL;
LauGettext* LauGettext::instance() {
if (instance_) return instance_;
instance_ = new LauGettext();
return instance_;
}
void LauGettext::destroyInstance() {
if (instance_) delete instance_;
instance_ = NULL;
}
LauGettext::LauGettext() {
catalogueName_ = "locale";
languageCode_ = "en";
countryCode_ = "US";
locale_ = "en_US";
}
LauGettext::~LauGettext() {
}
std::string LauGettext::moFilePath() const {
std::string filePath = catalogueLocation();
filePath.append(PATH_SEPARATOR);
filePath.append(locale());
filePath.append(PATH_SEPARATOR);
filePath.append(catalogueName());
filePath.append(".mo");
std::ifstream fileTest(filePath.c_str(), std::ios::in | std::ios::binary);
if (fileTest) return filePath;
filePath = catalogueLocation();
filePath.append(PATH_SEPARATOR);
filePath.append(languageCode());
filePath.append(PATH_SEPARATOR);
filePath.append(catalogueName());
filePath.append(".mo");
std::ifstream fileTest2(filePath.c_str(), std::ios::in | std::ios::binary);
if (fileTest2) return filePath;
return "";
}
GettextMessage* LauGettext::getTranslation(const char* originalString, int originalLength) const {
if (!moParser_.ready()) {
std::string p = moFilePath();
if (p.empty()) return NULL; // File doesn't exist or cannot be opened
bool success = moParser_.parseFile(moFilePath().c_str());
if (!success) {
// The file could not be parsed
return NULL;
}
}
return moParser_.getTranslation(originalString, originalLength);
}
void LauGettext::setCatalogueName(std::string name) {
catalogueName_ = name;
}
void LauGettext::setCatalogueLocation(std::string location) {
catalogueLocation_ = location;
}
void LauGettext::setLocale(std::string localeCode) {
size_t splitIndex = localeCode.find("_");
if (splitIndex != localeCode.npos) {
languageCode_ = localeCode.substr(0, splitIndex);
countryCode_ = localeCode.substr(splitIndex + 1, 10);
} else {
languageCode_ = localeCode;
countryCode_ = "";
}
if (countryCode_.empty()) {
locale_ = languageCode_;
} else {
locale_ = languageCode_;
locale_.append("_");
locale_.append(countryCode_);
}
moParser_.clearData();
}
#undef PATH_SEPARATOR