/
Localisation.cpp
344 lines (268 loc) · 9.23 KB
/
Localisation.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2011-2019 Arx Libertatis Team (see the AUTHORS file)
*
* This file is part of Arx Libertatis.
*
* Arx Libertatis is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arx Libertatis is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Arx Libertatis. If not, see <http://www.gnu.org/licenses/>.
*/
#include "core/Localisation.h"
#include <map>
#include <stddef.h>
#include <sstream>
#include <cstdlib>
#include <iterator>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/foreach.hpp>
#include "core/Config.h"
#include "io/resource/ResourcePath.h"
#include "io/resource/PakReader.h"
#include "io/IniReader.h"
#include "io/fs/FileStream.h"
#include "io/fs/SystemPaths.h"
#include "io/log/Logger.h"
#include "platform/Environment.h"
#include "platform/Platform.h"
#include "util/Unicode.h"
namespace {
IniReader g_localisation;
static Language getLanguageInfo(const std::string & id) {
std::istringstream iss(g_resources->read("localisation/languages/" + id + ".ini"));
IniReader reader;
reader.read(iss);
Language result;
result.name = reader.getKey("language", "name", id);
result.locale = reader.getKey("language", "locale", id);
boost::to_lower(result.locale);
return result;
}
std::string selectPreferredLanguage(const Languages & languages) {
std::vector<std::string> locales = platform::getPreferredLocales();
// Select language based on system language
BOOST_FOREACH(std::string & locale, locales) {
BOOST_FOREACH(const Languages::value_type & language, languages) {
if(language.second.locale == locale) {
return language.first;
}
}
BOOST_FOREACH(const Languages::value_type & language, languages) {
if(boost::starts_with(language.second.locale, locale)) {
return language.first;
}
}
}
return std::string();
}
std::string selectDefaultLanguage(const Languages & languages) {
// Select language from AF config file
// For AF 1.22 this will match language selected by the user in the Steam / GOG / Bethesda launcher
const char * cfgFiles[] = { "cfg.ini", "cfg_default.ini" };
BOOST_FOREACH(const fs::path & datadir, fs::getDataDirs()) {
BOOST_FOREACH(const char * cfgFile, cfgFiles) {
fs::ifstream ifs(datadir / cfgFile);
if(ifs.is_open()) {
IniReader cfg;
cfg.read(ifs);
std::string language = cfg.getKey("language", "string", config.language);
boost::to_lower(language);
Languages::const_iterator it = languages.find(language);
if(it != languages.end()) {
return it->first;
}
}
}
}
return std::string();
}
void autodetectTextLanguage() {
Languages languages = getAvailableTextLanguages();
if(languages.empty()) {
LogCritical << "Could not find any localisation file. (localisation/utext_*.ini)";
config.language = std::string();
return;
}
if(languages.size() == 1) {
// Only one language available so just use that
config.language = languages.begin()->first;
return;
}
std::ostringstream list;
BOOST_FOREACH(const Languages::value_type & language, languages) {
if(list.tellp()) {
list << ", ";
}
list << language.first;
}
LogInfo << "Multiple text languages available: " << list.str();
config.language = selectPreferredLanguage(languages);
if(!config.language.empty()) {
return;
}
config.language = selectDefaultLanguage(languages);
if(!config.language.empty()) {
return;
}
// Otherwise, prefer english if english audio is available
Languages::iterator english = languages.find("english");
if(english != languages.end() && g_resources->getDirectory(res::path("speech") / english->first)) {
config.language = english->first;
return;
}
// Otherwise, prefer the first language with audio available
BOOST_FOREACH(const Languages::value_type & language, languages) {
if(g_resources->getDirectory(res::path("speech") / language.first)) {
config.language = language.first;
return;
}
}
// Otherwise, prefer english
if(english != languages.end()) {
config.language = english->first;
return;
}
// Finally just select the first available language
config.language = languages.begin()->first;
}
void loadLocalisation(PakDirectory * dir, const std::string & name) {
PakFile * file = dir->getFile(name);
arx_assert(file);
std::string buffer = file->read();
if(buffer.empty()) {
LogWarning << "Error reading localisation file localisation/" << name;
return;
}
if(buffer.size() >= 2 && buffer[0] == '\xFF' && buffer[1] == '\xFE') {
LogWarning << "UTF16le character encoding is unsupported for new localizations "
"please use UTF8 for file localisation/" << name;
return;
}
LogInfo << "Loading: " << name;
std::istringstream iss(buffer);
if(!g_localisation.read(iss, true)) {
LogWarning << "Error parsing localisation file localisation/" << name;
}
}
void loadLocalisations() {
const std::string suffix = ".ini";
const std::string fallbackPrefix = "xtext_english_";
const std::string localizedPrefix = "xtext_" + config.language + "_";
PakDirectory * dir = g_resources->getDirectory("localisation");
if(!dir) {
return;
}
typedef std::map<std::string, bool> LocalizationFiles;
LocalizationFiles localizationFiles;
PakDirectory::files_iterator fileIter = dir->files_begin();
for(; fileIter != dir->files_end(); ++fileIter) {
const std::string & name = fileIter->first;
if(boost::ends_with(name, suffix)) {
if(boost::starts_with(name, fallbackPrefix)) {
localizationFiles[name.substr(fallbackPrefix.length())];
}
if(boost::starts_with(name, localizedPrefix)) {
localizationFiles[name.substr(localizedPrefix.length())] = true;
}
}
}
BOOST_FOREACH(const LocalizationFiles::value_type & i, localizationFiles) {
loadLocalisation(dir, fallbackPrefix + i.first);
if(i.second && localizedPrefix != fallbackPrefix) {
loadLocalisation(dir, localizedPrefix + i.first);
}
}
}
} // anonymous namespace
Languages getAvailableTextLanguages() {
Languages result;
PakDirectory * localisation = g_resources->getDirectory("localisation");
if(!localisation) {
LogCritical << "Missing 'localisation' directory. Is 'loc.pak' present?";
return result;
}
PakDirectory::files_iterator file = localisation->files_begin();
for(; file != localisation->files_end(); ++file) {
const std::string & name = file->first;
const std::string prefix = "utext_";
const std::string suffix = ".ini";
if(!boost::starts_with(name, prefix) || !boost::ends_with(name, suffix)) {
// Not a localisation file.
continue;
}
if(name.length() <= prefix.length() + suffix.length()) {
// Missing language name.
continue;
}
// Extract the language name.
size_t length = name.length() - prefix.length() - suffix.length();
std::string id = name.substr(prefix.length(), length);
if(id.find_first_not_of("abcdefghijklmnopqrstuvwxyz_") != std::string::npos) {
LogWarning << "Ignoring localisation/" << name;
continue;
}
result.insert(Languages::value_type(id, getLanguageInfo(id)));
}
return result;
}
bool initLocalisation() {
LogDebug("Starting localization");
g_localisation.clear();
PakFile * file = NULL;
if(!config.language.empty()) {
LogInfo << "Using text language from config: " << config.language;
std::string filename = "localisation/utext_" + config.language + ".ini";
file = g_resources->getFile(filename);
if(!file) {
LogWarning << "Localisation file " << filename << " not found";
}
}
if(!file) {
autodetectTextLanguage();
if(!config.language.empty()) {
LogInfo << "Autodetected text language: " << config.language;
file = g_resources->getFile("localisation/utext_" + config.language + ".ini");
}
}
if(!file) {
return false;
}
arx_assert(!config.language.empty());
std::string buffer = file->read();
if(buffer.empty()) {
return false;
}
LogDebug("Loaded localisation file of size " << buffer.size());
buffer = util::convert<util::UTF16LE, util::UTF8>(buffer);
LogDebug("Converted to UTF8 string of length " << buffer.size());
if(!buffer.empty()) {
LogDebug("Preparing to parse localisation file");
std::istringstream iss(buffer);
if(!g_localisation.read(iss)) {
LogWarning << "Error parsing localisation file localisation/utext_"
<< config.language << ".ini";
}
}
loadLocalisations();
return true;
}
long getLocalisedKeyCount(const std::string & sectionname) {
return g_localisation.getKeyCount(sectionname);
}
std::string getLocalised(const std::string & name) {
arx_assert(name.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ[]") == std::string::npos);
return g_localisation.getKey(name, std::string(), name);
}
std::string getLocalised(const std::string & name, const std::string & default_value) {
arx_assert(name.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ[]") == std::string::npos);
return g_localisation.getKey(name, std::string(), default_value);
}