-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
gettext_boost.cpp
440 lines (399 loc) · 12.8 KB
/
gettext_boost.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "filesystem.hpp"
#include <iostream>
#include <fstream>
#include <locale>
#include <boost/locale.hpp>
// including boost/thread fixes linking of boost locale for msvc on boost 1.60
#include <boost/thread.hpp>
#include <set>
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4714)
#endif
#include "spirit_po.hpp"
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif
#define DBG_G LOG_STREAM(debug, lg::general())
#define LOG_G LOG_STREAM(info, lg::general())
#define WRN_G LOG_STREAM(warn, lg::general())
#define ERR_G LOG_STREAM(err, lg::general())
namespace bl = boost::locale;
namespace
{
class default_utf8_locale_name
{
public:
static const std::string& name()
{
//Use pointers becasue we don't want it to be destructed at programm end.
static default_utf8_locale_name* lname = new default_utf8_locale_name();
return lname->name_;
}
private:
default_utf8_locale_name()
: name_()
{
LOG_G << "Generating default locale\n";
try
{
//NOTE: the default_locale objects needs to live as least as long as the locale_info object. Otherwise the programm will segfault.
std::locale default_locale = bl::generator().generate("");
const bl::info& locale_info = std::use_facet<bl::info>(default_locale);
name_ += locale_info.language();
if(!locale_info.country().empty())
name_ += "_" + locale_info.country();
name_ += ".UTF-8";
if(!locale_info.variant().empty())
name_ += "@" + locale_info.variant();
}
catch(const std::exception& e)
{
ERR_G << "Failed to generate default locale string. message:" << e.what() << std::endl;
}
LOG_G << "Finished generating default locale, default is now '" << name_ << "'\n";
}
std::string name_;
};
class wesnoth_message_format : public bl::message_format<char>
{
using po_catalog = spirit_po::catalog<>;
public:
wesnoth_message_format(std::locale base, const std::set<std::string>& domains, const std::set<std::string>& paths)
: base_loc_(base)
{
const bl::info& inf = std::use_facet<bl::info>(base);
if(inf.language() == "c") {
return;
}
std::string lang_name_short = inf.language();
std::string lang_name_long = lang_name_short;
if(!inf.country().empty()) {
lang_name_long += '_';
lang_name_long += inf.country();
}
if(!inf.variant().empty()) {
lang_name_long += '@';
lang_name_long += inf.variant();
lang_name_short += '@';
lang_name_short += inf.variant();
}
DBG_G << "Loading po files for language " << lang_name_long << '\n';
for(auto& domain : domains) {
DBG_G << "Searching for po files for domain " << domain << '\n';
std::string path;
for(auto base_path : paths) {
DBG_G << "Searching in dir " << base_path << '\n';
if(base_path[base_path.length()-1] != '/') {
base_path += '/';
}
base_path += domain;
base_path += '/';
path = base_path + lang_name_long + ".po";
DBG_G << " Trying path " << path << '\n';
if(filesystem::file_exists(path)) {
break;
}
path = base_path + lang_name_short + ".po";
DBG_G << " Trying path " << path << '\n';
if(filesystem::file_exists(path)) {
break;
}
}
if(!filesystem::file_exists(path)) {
continue;
}
std::ifstream po_file;
po_file.exceptions(std::ios::badbit);
LOG_G << "Loading language file from " << path << '\n';
try {
po_file.open(path);
const po_catalog& cat = po_catalog::from_istream(po_file);
extra_messages_.emplace(get_base().domain(domain), cat);
} catch(spirit_po::catalog_exception& e) {
throw_po_error(lang_name_long, domain, e.what());
} catch(std::ios::failure&) {
throw_po_error(lang_name_long, domain, strerror(errno));
}
}
}
NORETURN static void throw_po_error(const std::string& lang, const std::string& dom, const std::string& detail) {
std::ostringstream err;
err << "Error opening language file for " << lang << ", textdomain " << dom
<< ":\n " << detail << '\n';
ERR_G << err.rdbuf() << std::flush;
throw game::error(err.str());
}
const char* get(int domain_id, const char* ctx, const char* id) const override
{
auto& base = get_base();
const char* msg = base.get(domain_id, ctx, id);
if(msg == nullptr) {
auto iter = extra_messages_.find(domain_id);
if(iter == extra_messages_.end()) {
return nullptr;
}
auto& catalog = iter->second;
const char* lookup = ctx ? catalog.pgettext(ctx, id) : catalog.gettext(id);
if(lookup != id) {
// (p)gettext returns the input pointer if the string was not found
msg = lookup;
}
}
return msg;
}
const char* get(int domain_id, const char* ctx, const char* sid, int n) const override
{
auto& base = get_base();
const char* msg = base.get(domain_id, ctx, sid, n);
if(msg == nullptr) {
auto iter = extra_messages_.find(domain_id);
if(iter == extra_messages_.end()) {
return nullptr;
}
auto& catalog = iter->second;
const char* lookup = ctx ? catalog.npgettext(ctx, sid, sid, n) : catalog.ngettext(sid, sid, n);
if(lookup != sid) {
// n(p)gettext returns one of the input pointers if the string was not found
msg = lookup;
}
}
return msg;
}
int domain(const std::string& domain) const override
{
auto& base = get_base();
return base.domain(domain);
}
const char* convert(const char* msg, std::string& buffer) const override
{
auto& base = get_base();
return base.convert(msg, buffer);
}
private:
const bl::message_format<char>& get_base() const
{
return std::use_facet<bl::message_format<char>>(base_loc_);
}
std::locale base_loc_;
std::map<int, po_catalog> extra_messages_;
};
struct translation_manager
{
translation_manager()
: loaded_paths_()
, loaded_domains_()
, current_language_(default_utf8_locale_name::name())
, generator_()
, current_locale_()
, is_dirty_(true)
{
const bl::localization_backend_manager& g_mgr = bl::localization_backend_manager::global();
for(const std::string& name : g_mgr.get_all_backends())
{
LOG_G << "Found boost locale backend: '" << name << "'\n";
}
generator_.use_ansi_encoding(false);
generator_.categories(bl::message_facet | bl::information_facet | bl::collation_facet | bl::formatting_facet);
generator_.characters(bl::char_facet);
// We cannot have current_locale_ be a non boost-generated locale since it might not supply
// the bl::info facet. As soon as we add message paths, update_locale_internal might fail,
// for example because of invalid .mo files. So make sure we call it at least once before adding paths/domains
update_locale_internal();
}
void add_messages_domain(const std::string& domain)
{
if(loaded_domains_.find(domain) != loaded_domains_.end())
{
return;
}
if(domain.find('/') != std::string::npos)
{
// Forward slash has a specific meaning in Boost.Locale domain
// names, specifying the encoding. We use UTF-8 for everything
// so we can't possibly support that, and odds are it's a user
// mistake (as in bug #23839).
ERR_G << "illegal textdomain name '" << domain
<< "', skipping textdomain\n";
return;
}
generator_.add_messages_domain(domain);
loaded_domains_.insert(domain);
}
void add_messages_path(const std::string& path)
{
if(loaded_paths_.find(path) != loaded_paths_.end())
{
return;
}
generator_.add_messages_path(path);
loaded_paths_.insert(path);
}
void set_default_messages_domain(const std::string& domain)
{
generator_.set_default_messages_domain(domain);
update_locale();
}
void set_language(const std::string& language)
{
std::string::size_type at_pos = language.rfind('@');
if(language.empty())
{
current_language_ = default_utf8_locale_name::name();
}
else if(at_pos != std::string::npos)
{
current_language_ = language.substr(0, at_pos) + ".UTF-8" + language.substr(at_pos);
}
else
{
current_language_ = language + ".UTF-8";
}
update_locale();
}
void update_locale()
{
is_dirty_ = true;
}
void update_locale_internal()
{
try
{
LOG_G << "attempting to generate locale by name '" << current_language_ << "'\n";
current_locale_ = generator_.generate(current_language_);
current_locale_ = std::locale(current_locale_, new wesnoth_message_format(current_locale_, loaded_domains_, loaded_paths_));
const bl::info& info = std::use_facet<bl::info>(current_locale_);
LOG_G << "updated locale to '" << current_language_ << "' locale is now '" << current_locale_.name() << "' ( "
<< "name='" << info.name()
<< "' country='" << info.country()
<< "' language='" << info.language()
<< "' encoding='" << info.encoding()
<< "' variant='" << info.variant() << "')\n";
}
catch(const bl::conv::conversion_error&)
{
assert(std::has_facet<bl::info>(current_locale_));
const bl::info& info = std::use_facet<bl::info>(current_locale_);
ERR_G << "Failed to update locale due to conversion error, locale is now: "
<< "name='" << info.name()
<< "' country='" << info.country()
<< "' language='" << info.language()
<< "' encoding='" << info.encoding()
<< "' variant='" << info.variant()
<< "'" << std::endl;
}
is_dirty_ = false;
}
const std::locale& get_locale()
{
if(is_dirty_)
{
update_locale_internal();
}
return current_locale_;
}
private:
std::set<std::string> loaded_paths_;
std::set<std::string> loaded_domains_;
std::string current_language_;
bl::generator generator_;
std::locale current_locale_;
bool is_dirty_;
};
translation_manager& get_manager()
{
static translation_manager* mng = new translation_manager();
return *mng;
}
}
namespace translation
{
std::string dgettext(const char* domain, const char* msgid)
{
return bl::dgettext(domain, msgid, get_manager().get_locale());
}
std::string egettext(char const *msgid)
{
return msgid[0] == '\0' ? msgid : bl::gettext(msgid, get_manager().get_locale());
}
std::string dsgettext (const char * domainname, const char *msgid)
{
std::string msgval = dgettext (domainname, msgid);
if (msgval == msgid) {
const char* firsthat = std::strrchr (msgid, '^');
if (firsthat == nullptr)
msgval = msgid;
else
msgval = firsthat + 1;
}
return msgval;
}
std::string dsngettext (const char * domainname, const char *singular, const char *plural, int n)
{
std::string msgval = bl::dngettext(domainname, singular, plural, n, get_manager().get_locale());
if (msgval == singular) {
const char* firsthat = std::strrchr (singular, '^');
if (firsthat == nullptr)
msgval = singular;
else
msgval = firsthat + 1;
}
return msgval;
}
void bind_textdomain(const char* domain, const char* directory, const char* /*encoding*/)
{
LOG_G << "adding textdomain '" << domain << "' in directory '" << directory << "'\n";
get_manager().add_messages_domain(domain);
get_manager().add_messages_path(directory);
get_manager().update_locale();
}
void set_default_textdomain(const char* domain)
{
LOG_G << "set_default_textdomain: '" << domain << "'\n";
get_manager().set_default_messages_domain(domain);
}
void set_language(const std::string& language, const std::vector<std::string>* /*alternates*/)
{
// why shoudl we need alternates? which languages we support shoudl only be related
// to which languages we ship with and not which the os supports
LOG_G << "setting language to '" << language << "' \n";
get_manager().set_language(language);
}
int compare(const std::string& s1, const std::string& s2)
{
return std::use_facet<std::collate<char> >(get_manager().get_locale()).compare(s1.c_str(), s1.c_str() + s1.size(), s2.c_str(), s2.c_str() + s2.size());
}
int icompare(const std::string& s1, const std::string& s2)
{
return std::use_facet<bl::collator<char> >(get_manager().get_locale()).compare(bl::collator_base::secondary, s1, s2);
}
void init()
{
}
std::string strftime(const std::string& format, const std::tm* time)
{
std::basic_ostringstream<char> dummy;
dummy.imbue(get_manager().get_locale());
dummy << bl::as::ftime(format) << mktime(const_cast<std::tm*>(time));
return dummy.str();
}
}