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;
46 std::vector<language_def> known_languages;
48 int min_translation_percent = 80;
60 static bool result =
true;
66 const utils::string_map::const_iterator
i = strings_.find(key);
67 if(
i != strings_.end()) {
75 empty_string =
"UNTLB " + key;
82 return (*
this)[std::string(key)];
87 return strings_.find(key);
92 return strings_.end();
96 : language(
t_string(
N_(
"System default language"),
"wesnoth"))
102 : localename(cfg[
"locale"])
104 , language(cfg[
"name"].t_str())
105 , sort_name(cfg[
"sort_name"].str(language))
106 , rtl(cfg[
"dir"] ==
"rtl")
107 , percent(cfg[
"percent"].to_int())
115 known_languages.clear();
116 known_languages.emplace_back();
119 known_languages.emplace_back(lang);
124 }
catch(
const utils::bad_optional_access&) {
134 std::sort(known_languages.begin(), known_languages.end());
136 if(all || min_translation_percent == 0) {
137 return known_languages;
140 std::vector<language_def> result;
141 std::copy_if(known_languages.begin(), known_languages.end(), std::back_inserter(result),
142 [](
const language_def& lang) { return lang.percent >= min_translation_percent; });
149 return min_translation_percent;
153 min_translation_percent = percent;
158 static const std::map<std::string, std::string> win32_locales_map = {
159 {
"af",
"Afrikaans" },
162 {
"bg",
"Bulgarian" },
171 {
"et",
"Estonian" },
176 {
"ga",
"Irish_Ireland" },
177 {
"gl",
"Galician" },
179 {
"hr",
"Croatian" },
180 {
"hu",
"Hungarian" },
181 {
"id",
"Indonesian" },
182 {
"is",
"Icelandic" },
184 {
"ja",
"Japanese" },
187 {
"lt",
"Lithuanian" },
189 {
"mk",
"Macedonian" },
191 {
"nb",
"Norwegian" },
194 {
"pt",
"Portuguese" },
196 {
"ro",
"Romanian" },
199 {
"sl",
"Slovenian" },
202 {
"tl",
"Filipino" },
204 {
"uk",
"Ukrainian" },
205 {
"vi",
"Vietnamese" },
209 static const std::string& posix_locale_to_win32(
const std::string& posix)
211 auto it = win32_locales_map.find(posix);
212 return it != win32_locales_map.end() ? it->second : posix;
218 std::vector<std::string>
const *alternates)
220 std::string locale = slocale;
230 unsetenv (
"LANGUAGE");
239 std::string lang_code{locale, 0, locale.find_first_of(
"_@.")};
240 locale = posix_locale_to_win32(lang_code);
244 std::vector<std::string>::const_iterator
i;
245 if (alternates)
i = alternates->begin();
249 std::string lang = locale, extra;
250 std::string::size_type pos = locale.find(
'@');
251 if (pos != std::string::npos) {
253 extra = locale.substr(pos);
260 char const *encoding[] {
".utf-8",
".UTF-8",
"" };
261 for (
int j = 0; j != 3; ++j)
263 locale = lang + encoding[j] + extra;
264 res = std::setlocale(category, locale.c_str());
266 LOG_G <<
"Set locale to '" << locale <<
"' result: '" << res <<
"'.";
271 if (!alternates ||
i == alternates->end())
break;
276 WRN_G <<
"setlocale() failed for '" << slocale <<
"'.";
278 if (category == LC_TIME) {
291 DBG_G <<
"Numeric locale: " << std::setlocale(LC_NUMERIC,
nullptr);
292 DBG_G <<
"Full locale: " << std::setlocale(LC_ALL,
nullptr);
299 std::string locale_lc;
303 current_language = locale;
314 if(complain && languages.empty()) {
315 PLAIN_LOG <<
"No [language] block found";
318 for(
const config& lang : languages) {
319 for(
const auto& [key, value] : lang.attribute_range()) {
320 strings_[key] = value;
333 assert(!known_languages.empty());
335 const std::string& prefs_locale =
prefs::get().locale();
336 if(prefs_locale.empty() ==
false) {
339 if(prefs_locale == def.localename) {
343 LOG_G <<
"'" << prefs_locale <<
"' locale not found in known array; defaulting to system locale";
344 return known_languages[0];
348 const char*
const locale = getenv(
"LANG");
350 return posix_locale_to_win32(locale);
352 if(locale !=
nullptr && strlen(locale) >= 2) {
356 std::string res(2,
'z');
357 res[0] = tolower(locale[0]);
358 res[1] = tolower(locale[1]);
363 LOG_G <<
"locale could not be determined; defaulting to system locale";
364 return known_languages[0];
371 const std::string &name =
t[
"name"];
372 const std::string &
path =
t[
"path"];
380 WRN_G <<
"no location found for '" <<
path <<
"', skipping textdomain";
389 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()
std::vector< language_def > get_languages(bool all)
Return a list of available translations.
static void wesnoth_setlocale(int category, const std::string &slocale, std::vector< std::string > const *alternates)
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)
void set_min_translation_percent(int percent)
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.
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()
config read(std::istream &in, abstract_validator *validator)
void set_language(const std::string &language, const std::vector< std::string > *)
std::map< std::string, t_string > string_map
std::vector< std::string > split(const config_attribute_value &val)
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
bool operator==(const language_def &) const
std::vector< std::string > alternates
language_def()
Creates the 'System Default Language' definition.
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.