29 #if !defined(_MSC_VER) && !defined(__MINGW32__)
30 extern "C" int _putenv(
const char*);
38 #define DBG_G LOG_STREAM(debug, lg::general())
39 #define LOG_G LOG_STREAM(info, lg::general())
40 #define WRN_G LOG_STREAM(warn, lg::general())
41 #define ERR_G LOG_STREAM(err, lg::general())
45 std::vector<config> languages_;
47 int min_translation_percent = 80;
68 static bool result =
true;
74 const utils::string_map::const_iterator
i = strings_.find(key);
75 if(
i != strings_.end()) {
83 empty_string =
"UNTLB " + key;
90 return (*
this)[std::string(key)];
95 return strings_.find(key);
100 return strings_.end();
119 lang[
"locale"], lang[
"name"], lang[
"dir"],
120 lang[
"alternates"], lang[
"sort_name"], lang[
"percent"]);
132 if(all || min_translation_percent == 0) {
138 [](
const language_def& lang) { return lang.percent >= min_translation_percent; });
144 min_translation_percent = percent;
149 static const std::map<std::string, std::string> win32_locales_map = {
150 {
"af",
"Afrikaans" },
153 {
"bg",
"Bulgarian" },
162 {
"et",
"Estonian" },
167 {
"ga",
"Irish_Ireland" },
168 {
"gl",
"Galician" },
170 {
"hr",
"Croatian" },
171 {
"hu",
"Hungarian" },
172 {
"id",
"Indonesian" },
173 {
"is",
"Icelandic" },
175 {
"ja",
"Japanese" },
178 {
"lt",
"Lithuanian" },
180 {
"mk",
"Macedonian" },
182 {
"nb",
"Norwegian" },
185 {
"pt",
"Portuguese" },
187 {
"ro",
"Romanian" },
190 {
"sl",
"Slovenian" },
193 {
"tl",
"Filipino" },
195 {
"uk",
"Ukrainian" },
196 {
"vi",
"Vietnamese" },
200 static const std::string& posix_locale_to_win32(
const std::string& posix)
202 auto it = win32_locales_map.find(posix);
203 return it != win32_locales_map.end() ? it->second : posix;
209 std::vector<std::string>
const *alternates)
211 std::string locale = slocale;
221 unsetenv (
"LANGUAGE");
230 std::string lang_code{locale, 0, locale.find_first_of(
"_@.")};
231 locale = posix_locale_to_win32(lang_code);
235 std::vector<std::string>::const_iterator
i;
236 if (alternates)
i = alternates->begin();
240 std::string lang = locale, extra;
241 std::string::size_type pos = locale.find(
'@');
242 if (pos != std::string::npos) {
244 extra = locale.substr(pos);
251 char const *encoding[] {
".utf-8",
".UTF-8",
"" };
252 for (
int j = 0; j != 3; ++j)
254 locale = lang + encoding[j] + extra;
255 res = std::setlocale(category, locale.c_str());
257 LOG_G <<
"Set locale to '" << locale <<
"' result: '" << res <<
"'.";
262 if (!alternates ||
i == alternates->end())
break;
267 WRN_G <<
"setlocale() failed for '" << slocale <<
"'.";
269 if (category == LC_TIME) {
282 DBG_G <<
"Numeric locale: " << std::setlocale(LC_NUMERIC,
nullptr);
283 DBG_G <<
"Full locale: " << std::setlocale(LC_ALL,
nullptr);
290 std::string locale_lc;
294 current_language = locale;
305 DBG_G <<
"Loading strings";
308 LOG_G <<
"There are " << languages_.size() <<
" [language] blocks";
309 if (complain && languages_.empty()) {
310 PLAIN_LOG <<
"No [language] block found";
313 for (
const config &lang : languages_) {
314 DBG_G <<
"[language]";
316 DBG_G << j.first <<
"=\"" << j.second <<
"\"";
317 strings_[j.first] = j.second;
319 DBG_G <<
"[/language]";
335 if(prefs_locale.empty() ==
false) {
339 if (prefs_locale ==
i->localename)
342 LOG_G <<
"'" << prefs_locale <<
"' locale not found in known array; defaulting to system locale";
347 const char*
const locale = getenv(
"LANG");
349 return posix_locale_to_win32(locale);
351 if(locale !=
nullptr && strlen(locale) >= 2) {
355 std::string res(2,
'z');
356 res[0] = tolower(locale[0]);
357 res[1] = tolower(locale[1]);
362 LOG_G <<
"locale could not be determined; defaulting to system locale";
370 const std::string &name =
t[
"name"];
371 const std::string &
path =
t[
"path"];
378 if (location.empty()) {
381 WRN_G <<
"no location found for '" <<
path <<
"', skipping textdomain";
393 languages_.push_back(l);
A config object defines a single node in a WML file, with access to child nodes.
child_itors child_range(config_key_type key)
attribute_map::value_type attribute
A class grating read only view to a vector of config objects, viewed as one config with all children ...
config_array_view child_range(config_key_type key) const
static void add_textdomain(const std::string &name, const std::string &path)
Declarations for File-IO.
const language_def & get_locale()
static void wesnoth_setlocale(int category, const std::string &slocale, std::vector< std::string > const *alternates)
bool current_language_rtl()
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
bool load_strings(bool complain)
bool & time_locale_correct()
const language_def & get_language()
bool load_language_list()
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
void set_language(const language_def &locale)
static language_list known_languages
language_list get_languages(bool all)
Return a list of available translations.
symbol_table string_table
void set_min_translation_percent(int percent)
std::vector< language_def > language_list
Standard logging facilities (interface).
std::string get_wml_location(const std::string &filename, const std::string ¤t_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
std::string get_binary_dir_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual directory of a given type or an empty string if the directory i...
std::unique_ptr< std::istream > scoped_istream
std::string get_intl_dir()
void set_language(const std::string &language, const std::vector< std::string > *)
std::map< std::string, t_string > string_map
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
void read(config &cfg, std::istream &in, abstract_validator *validator)
bool operator==(const language_def &) const
std::vector< std::string > alternates
utils::string_map::const_iterator end() const
const t_string & operator[](const std::string &key) const
Look up the string mappings given in [language] tags.
utils::string_map::const_iterator find(const std::string &key) const
Look up the string mappings given in [language] tags.