-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
language.cpp
326 lines (275 loc) · 8.63 KB
/
language.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
/*
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 "filesystem.hpp"
#include "gettext.hpp"
#include "language.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "serialization/parser.hpp"
#include "serialization/preprocessor.hpp"
#include <stdexcept>
#include <clocale>
#include <boost/scoped_array.hpp>
#include <boost/foreach.hpp>
#ifdef _WIN32
#include <windows.h>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
extern "C" int _putenv(const char*);
#endif
#endif
#ifdef __APPLE__
#include <cerrno>
#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 {
language_def current_language;
std::vector<config> languages_;
utils::string_map strings_;
}
static language_list known_languages;
bool load_strings(bool complain);
bool current_language_rtl()
{
return get_language().rtl;
}
bool language_def::operator== (const language_def& a) const
{
return ((language == a.language) /* && (localename == a.localename) */ );
}
symbol_table string_table;
bool& time_locale_correct()
{
static bool result = true;
return result;
}
const t_string& symbol_table::operator[](const std::string& key) const
{
const utils::string_map::const_iterator i = strings_.find(key);
if(i != strings_.end()) {
return i->second;
} else {
static t_string empty_string;
// Let's do it the painful way (untlb means untranslatABLE).
// It will cause problem if somebody stores more than one reference at once
// but I don't really care since this path is an error path and it should
// not have been taken in the first place. -- silene
empty_string = "UNTLB " + key;
return empty_string;
}
}
const t_string& symbol_table::operator[](const char* key) const
{
return (*this)[std::string(key)];
}
bool load_language_list()
{
config cfg;
try {
filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location("hardwired/language.cfg"));
read(cfg, *stream);
} catch(config::error &) {
return false;
}
known_languages.clear();
known_languages.push_back(
language_def("", t_string(N_("System default language"), "wesnoth"), "ltr", "", "A"));
BOOST_FOREACH(const config &lang, cfg.child_range("locale"))
{
known_languages.push_back(
language_def(lang["locale"], lang["name"], lang["dir"],
lang["alternates"], lang["sort_name"]));
}
return true;
}
language_list get_languages()
{
// We sort every time, the local might have changed which can modify the
// sort order.
std::sort(known_languages.begin(), known_languages.end());
return known_languages;
}
static void wesnoth_setlocale(int category, std::string const &slocale,
std::vector<std::string> const *alternates)
{
std::string locale = slocale;
// FIXME: ideally we should check LANGUAGE and on first invocation
// use that value, so someone with es would get the game in Spanish
// instead of en_US the first time round
// LANGUAGE overrides other settings, so for now just get rid of it
// FIXME: add configure check for unsetenv
//category is never LC_MESSAGES since that case was moved to gettext.cpp to remove the dependency to libintl.h in this file
//that's why code like if (category == LC_MESSAGES) is outcommented here.
#ifndef _WIN32
unsetenv ("LANGUAGE"); // void so no return value to check
#endif
#ifdef __APPLE__
//if (category == LC_MESSAGES && setenv("LANG", locale.c_str(), 1) == -1) {
// ERR_G << "setenv LANG failed: " << strerror(errno);
//}
#endif
#ifdef _WIN32
std::string win_locale(locale, 0, 2);
#include "language_win32.ii"
//if(category == LC_MESSAGES) {
// SetEnvironmentVariableA("LANG", win_locale.c_str());
// std::string env = "LANGUAGE=" + locale;
// _putenv(env.c_str());
// return;
//}
locale = win_locale;
#endif
char *res = NULL;
std::vector<std::string>::const_iterator i;
if (alternates) i = alternates->begin();
for (;;)
{
std::string lang = locale, extra;
std::string::size_type pos = locale.find('@');
if (pos != std::string::npos) {
lang.erase(pos);
extra = locale.substr(pos);
}
/*
* The "" is the last item to work-around a problem in glibc picking
* the non utf8 locale instead an utf8 version if available.
*/
char const *encoding[] = { ".utf-8", ".UTF-8", "" };
for (int j = 0; j != 3; ++j)
{
locale = lang + encoding[j] + extra;
res = std::setlocale(category, locale.c_str());
if (res) {
LOG_G << "Set locale to '" << locale << "' result: '" << res << "'.\n";
goto done;
}
}
if (!alternates || i == alternates->end()) break;
locale = *i;
++i;
}
WRN_G << "setlocale() failed for '" << slocale << "'." << std::endl;
if (category == LC_TIME) {
time_locale_correct() = false;
}
#ifndef _WIN32
//if(category == LC_MESSAGES) {
// WRN_G << "Setting LANGUAGE to '" << slocale << "'." << std::endl;
// setenv("LANGUAGE", slocale.c_str(), 1);
// std::setlocale(LC_MESSAGES, "");
//}
#endif
done:
DBG_G << "Numeric locale: " << std::setlocale(LC_NUMERIC, NULL) << '\n';
DBG_G << "Full locale: " << std::setlocale(LC_ALL, NULL) << '\n';
}
void set_language(const language_def& locale)
{
strings_.clear();
std::string locale_lc;
locale_lc.resize(locale.localename.size());
std::transform(locale.localename.begin(),locale.localename.end(),locale_lc.begin(),tolower);
current_language = locale;
time_locale_correct() = true;
wesnoth_setlocale(LC_COLLATE, locale.localename, &locale.alternates);
wesnoth_setlocale(LC_TIME, locale.localename, &locale.alternates);
translation::set_language(locale.localename, &locale.alternates);
load_strings(false);
}
bool load_strings(bool complain)
{
DBG_G << "Loading strings\n";
config cfg;
LOG_G << "There are " << languages_.size() << " [language] blocks\n";
if (complain && languages_.empty()) {
std::cerr << "No [language] block found\n";
return false;
}
BOOST_FOREACH(const config &lang, languages_) {
DBG_G << "[language]\n";
BOOST_FOREACH(const config::attribute &j, lang.attribute_range()) {
DBG_G << j.first << "=\"" << j.second << "\"\n";
strings_[j.first] = j.second;
}
DBG_G << "[/language]\n";
}
DBG_G << "done\n";
return true;
}
const language_def& get_language() { return current_language; }
const language_def& get_locale()
{
//TODO: Add in support for querying the locale on Windows
assert(!known_languages.empty());
const std::string& prefs_locale = preferences::language();
if(prefs_locale.empty() == false) {
translation::set_language(prefs_locale, NULL);
for(language_list::const_iterator i = known_languages.begin();
i != known_languages.end(); ++i) {
if (prefs_locale == i->localename)
return *i;
}
LOG_G << "'" << prefs_locale << "' locale not found in known array; defaulting to system locale\n";
return known_languages[0];
}
#if 0
const char* const locale = getenv("LANG");
#ifdef _WIN32
std::string win_locale = locale
#include "language_win32.ii"
return win_locale;
#endif
if(locale != NULL && strlen(locale) >= 2) {
//we can't pass pointers into the string to the std::string
//constructor because some STL implementations don't support
//it (*cough* MSVC++6)
std::string res(2,'z');
res[0] = tolower(locale[0]);
res[1] = tolower(locale[1]);
return res;
}
#endif
LOG_G << "locale could not be determined; defaulting to system locale\n";
return known_languages[0];
}
void init_textdomains(const config& cfg)
{
BOOST_FOREACH(const config &t, cfg.child_range("textdomain"))
{
const std::string &name = t["name"];
const std::string &path = t["path"];
if(path.empty()) {
t_string::add_textdomain(name, filesystem::get_intl_dir());
} else {
std::string location = filesystem::get_binary_dir_location("", path);
if (location.empty()) {
//if location is empty, this causes a crash on Windows, so we
//disallow adding empty domains
WRN_G << "no location found for '" << path << "', skipping textdomain" << std::endl;
} else {
t_string::add_textdomain(name, location);
}
}
}
}
bool init_strings(const config& cfg)
{
languages_.clear();
BOOST_FOREACH(const config &l, cfg.child_range("language")) {
languages_.push_back(l);
}
return load_strings(true);
}
/* vim:set encoding=utf-8: */