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; });
145 return min_translation_percent;
149 min_translation_percent = percent;
154 static const std::map<std::string, std::string> win32_locales_map = {
155 {
"af",
"Afrikaans" },
158 {
"bg",
"Bulgarian" },
167 {
"et",
"Estonian" },
172 {
"ga",
"Irish_Ireland" },
173 {
"gl",
"Galician" },
175 {
"hr",
"Croatian" },
176 {
"hu",
"Hungarian" },
177 {
"id",
"Indonesian" },
178 {
"is",
"Icelandic" },
180 {
"ja",
"Japanese" },
183 {
"lt",
"Lithuanian" },
185 {
"mk",
"Macedonian" },
187 {
"nb",
"Norwegian" },
190 {
"pt",
"Portuguese" },
192 {
"ro",
"Romanian" },
195 {
"sl",
"Slovenian" },
198 {
"tl",
"Filipino" },
200 {
"uk",
"Ukrainian" },
201 {
"vi",
"Vietnamese" },
205 static const std::string& posix_locale_to_win32(
const std::string& posix)
207 auto it = win32_locales_map.find(posix);
208 return it != win32_locales_map.end() ? it->second : posix;
214 std::vector<std::string>
const *alternates)
216 std::string locale = slocale;
226 unsetenv (
"LANGUAGE");
235 std::string lang_code{locale, 0, locale.find_first_of(
"_@.")};
236 locale = posix_locale_to_win32(lang_code);
240 std::vector<std::string>::const_iterator
i;
241 if (alternates)
i = alternates->begin();
245 std::string lang = locale, extra;
246 std::string::size_type pos = locale.find(
'@');
247 if (pos != std::string::npos) {
249 extra = locale.substr(pos);
256 char const *encoding[] {
".utf-8",
".UTF-8",
"" };
257 for (
int j = 0; j != 3; ++j)
259 locale = lang + encoding[j] + extra;
260 res = std::setlocale(category, locale.c_str());
262 LOG_G <<
"Set locale to '" << locale <<
"' result: '" << res <<
"'.";
267 if (!alternates ||
i == alternates->end())
break;
272 WRN_G <<
"setlocale() failed for '" << slocale <<
"'.";
274 if (category == LC_TIME) {
287 DBG_G <<
"Numeric locale: " << std::setlocale(LC_NUMERIC,
nullptr);
288 DBG_G <<
"Full locale: " << std::setlocale(LC_ALL,
nullptr);
295 std::string locale_lc;
299 current_language = locale;
312 if (complain && languages_.empty()) {
313 PLAIN_LOG <<
"No [language] block found";
316 for (
const config &lang : languages_) {
317 for(
const auto& [key, value] : lang.attribute_range()) {
318 strings_[key] = value;
333 const std::string& prefs_locale =
prefs::get().locale();
334 if(prefs_locale.empty() ==
false) {
338 if (prefs_locale ==
i->localename)
341 LOG_G <<
"'" << prefs_locale <<
"' locale not found in known array; defaulting to system locale";
346 const char*
const locale = getenv(
"LANG");
348 return posix_locale_to_win32(locale);
350 if(locale !=
nullptr && strlen(locale) >= 2) {
354 std::string res(2,
'z');
355 res[0] = tolower(locale[0]);
356 res[1] = tolower(locale[1]);
361 LOG_G <<
"locale could not be determined; defaulting to system locale";
369 const std::string &name =
t[
"name"];
370 const std::string &
path =
t[
"path"];
378 WRN_G <<
"no location found for '" <<
path <<
"', skipping textdomain";
387 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)
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()
int get_min_translation_percent()
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).
utils::optional< std::string > get_wml_location(const std::string &path, const utils::optional< std::string > ¤t_dir)
Returns a translated path to the actual file or directory, if it exists.
std::unique_ptr< std::istream > scoped_istream
utils::optional< 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, if it exists.
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.