1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include <fstream> |
---|
29 | |
---|
30 | #include "CoreIncludes.h" |
---|
31 | #include "Language.h" |
---|
32 | |
---|
33 | namespace orxonox |
---|
34 | { |
---|
35 | // ############################### |
---|
36 | // ### LanguageEntry ### |
---|
37 | // ############################### |
---|
38 | LanguageEntry::LanguageEntry(const std::string& fallbackEntry) |
---|
39 | { |
---|
40 | RegisterRootObject(LanguageEntry); |
---|
41 | |
---|
42 | this->fallbackEntry_ = fallbackEntry; |
---|
43 | this->translatedEntry_ = fallbackEntry; |
---|
44 | } |
---|
45 | |
---|
46 | void LanguageEntry::setTranslation(const std::string& translation) |
---|
47 | { |
---|
48 | if (translation.compare("") != 0) |
---|
49 | this->translatedEntry_ = translation; |
---|
50 | else |
---|
51 | this->translatedEntry_ = this->fallbackEntry_; |
---|
52 | } |
---|
53 | |
---|
54 | void LanguageEntry::setDefault(const std::string& fallbackEntry) |
---|
55 | { |
---|
56 | if (this->translatedEntry_.compare(this->fallbackEntry_) == 0) |
---|
57 | this->translatedEntry_ = fallbackEntry; |
---|
58 | |
---|
59 | this->fallbackEntry_ = fallbackEntry; |
---|
60 | } |
---|
61 | |
---|
62 | // ############################### |
---|
63 | // ### Language ### |
---|
64 | // ############################### |
---|
65 | Language::Language() |
---|
66 | { |
---|
67 | RegisterRootObject(Language); |
---|
68 | this->defaultLanguage_ = "default"; |
---|
69 | this->defaultTranslation_ = "ERROR: LANGUAGE ENTRY DOESN'T EXIST!"; |
---|
70 | this->readDefaultLanguageFile(); |
---|
71 | } |
---|
72 | |
---|
73 | void Language::setConfigValues() |
---|
74 | { |
---|
75 | SetConfigValue(language_, this->defaultLanguage_).description("The language of the ingame text"); |
---|
76 | this->readTranslatedLanguageFile(); |
---|
77 | } |
---|
78 | |
---|
79 | Language& Language::getLanguage() |
---|
80 | { |
---|
81 | static Language theOnlyLanguageObject = Language(); |
---|
82 | static bool bCreatingTheOnlyLanguageObject = true; |
---|
83 | |
---|
84 | if (bCreatingTheOnlyLanguageObject) |
---|
85 | { |
---|
86 | bCreatingTheOnlyLanguageObject = false; |
---|
87 | theOnlyLanguageObject.setConfigValues(); |
---|
88 | } |
---|
89 | |
---|
90 | return theOnlyLanguageObject; |
---|
91 | } |
---|
92 | |
---|
93 | void Language::createEntry(const LanguageEntryName& name, const std::string& entry) |
---|
94 | { |
---|
95 | if (!this->languageEntries_[name]) |
---|
96 | { |
---|
97 | LanguageEntry* newEntry = new LanguageEntry(entry); |
---|
98 | newEntry->setName(name); |
---|
99 | this->languageEntries_[name] = newEntry; |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | COUT(2) << "Warning: Language entry " << name << " is duplicate in " << getFileName(this->defaultLanguage_) << "!" << std::endl; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | void Language::addEntry(const LanguageEntryName& name, const std::string& entry) |
---|
108 | { |
---|
109 | std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(name); |
---|
110 | if (!it->second) |
---|
111 | this->createEntry(name, entry); |
---|
112 | else if (it->second->getDefault().compare(entry) == 0) |
---|
113 | return; |
---|
114 | else |
---|
115 | it->second->setDefault(entry); |
---|
116 | |
---|
117 | this->writeDefaultLanguageFile(); |
---|
118 | } |
---|
119 | |
---|
120 | const std::string& Language::getTranslation(const LanguageEntryName& name) const |
---|
121 | { |
---|
122 | std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(name); |
---|
123 | if (it->second) |
---|
124 | return it->second->getTranslation(); |
---|
125 | else |
---|
126 | { |
---|
127 | COUT(2) << "Error: Language entry \"" << name << "\" not found!" << std::endl; |
---|
128 | return this->defaultTranslation_; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | const std::string Language::getFileName(const std::string& language) |
---|
133 | { |
---|
134 | return std::string("translation_" + language + ".lang"); |
---|
135 | } |
---|
136 | |
---|
137 | void Language::readDefaultLanguageFile() |
---|
138 | { |
---|
139 | COUT(4) << "Read default language file." << std::endl; |
---|
140 | |
---|
141 | // This creates the file if it's not existing |
---|
142 | std::ofstream createFile; |
---|
143 | createFile.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::app); |
---|
144 | createFile.close(); |
---|
145 | |
---|
146 | // Open the file |
---|
147 | std::ifstream file; |
---|
148 | file.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::in); |
---|
149 | |
---|
150 | if (!file.is_open()) |
---|
151 | { |
---|
152 | COUT(1) << "Error: Couldn't open file " << getFileName(this->defaultLanguage_) << " to read the default language entries!" << std::endl; |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | char line[1024]; |
---|
157 | |
---|
158 | // Iterate through the file and create the LanguageEntries |
---|
159 | while (file.good() && !file.eof()) |
---|
160 | { |
---|
161 | file.getline(line, 1024); |
---|
162 | std::string lineString = std::string(line); |
---|
163 | if (lineString.compare("") != 0) |
---|
164 | { |
---|
165 | unsigned int pos = lineString.find('='); |
---|
166 | if (pos < lineString.size() && lineString.size() >= 3) |
---|
167 | this->createEntry(lineString.substr(0, pos), lineString.substr(pos + 1)); |
---|
168 | else |
---|
169 | COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFileName(this->defaultLanguage_) << std::endl; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | file.close(); |
---|
174 | } |
---|
175 | |
---|
176 | void Language::readTranslatedLanguageFile() |
---|
177 | { |
---|
178 | COUT(4) << "Read translated language file (" << this->language_ << ")." << std::endl; |
---|
179 | |
---|
180 | // Open the file |
---|
181 | std::ifstream file; |
---|
182 | file.open(getFileName(this->language_).c_str(), std::fstream::in); |
---|
183 | |
---|
184 | if (!file.is_open()) |
---|
185 | { |
---|
186 | COUT(1) << "Error: Couldn't open file " << getFileName(this->language_) << " to read the translated language entries!" << std::endl; |
---|
187 | ResetConfigValue(language_); |
---|
188 | COUT(3) << "Info: Reset language to " << this->defaultLanguage_ << "." << std::endl; |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | char line[1024]; |
---|
193 | |
---|
194 | // Iterate through the file and create the LanguageEntries |
---|
195 | while (file.good() && !file.eof()) |
---|
196 | { |
---|
197 | file.getline(line, 1024); |
---|
198 | std::string lineString = std::string(line); |
---|
199 | if (lineString.compare("") != 0) |
---|
200 | { |
---|
201 | unsigned int pos = lineString.find('='); |
---|
202 | if (pos < lineString.size() && lineString.size() >= 3) |
---|
203 | { |
---|
204 | std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(lineString.substr(0, pos)); |
---|
205 | if (it->second) |
---|
206 | it->second->setTranslation(lineString.substr(pos + 1)); |
---|
207 | else |
---|
208 | COUT(2) << "Warning: Unknown language entry \"" << lineString.substr(0, pos) << "\" in " << getFileName(this->language_) << std::endl; |
---|
209 | } |
---|
210 | else |
---|
211 | COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFileName(this->language_) << std::endl; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | file.close(); |
---|
216 | } |
---|
217 | |
---|
218 | void Language::writeDefaultLanguageFile() const |
---|
219 | { |
---|
220 | COUT(4) << "Write default language file." << std::endl; |
---|
221 | |
---|
222 | // Open the file |
---|
223 | std::ofstream file; |
---|
224 | file.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::out); |
---|
225 | |
---|
226 | if (!file.is_open()) |
---|
227 | { |
---|
228 | COUT(1) << "Error: Couldn't open file " << getFileName(this->defaultLanguage_) << " to write the default language entries!" << std::endl; |
---|
229 | return; |
---|
230 | } |
---|
231 | |
---|
232 | // Iterate through the list an write the lines into the file |
---|
233 | for (Iterator<LanguageEntry> it = ObjectList<LanguageEntry>::start(); it; ++it) |
---|
234 | { |
---|
235 | file << it->getName() << "=" << it->getDefault() << std::endl; |
---|
236 | } |
---|
237 | |
---|
238 | file.close(); |
---|
239 | } |
---|
240 | } |
---|