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"))
103 : localename(
cfg[
"locale"])
106 : localename(
cfg[
"windows_locale"].str(
"C"))
109 , language(
cfg[
"name"].t_str())
110 , sort_name(
cfg[
"sort_name"].str(language))
111 , rtl(
cfg[
"dir"] ==
"rtl")
112 , percent(
cfg[
"percent"].to_int())
121 known_languages.clear();
122 known_languages.emplace_back();
125 known_languages.emplace_back(lang);
130 }
catch(
const utils::bad_optional_access&) {
141 std::sort(known_languages.begin(), known_languages.end());
143 if(all || min_translation_percent == 0) {
144 LOG_G <<
"Found " << known_languages.size() <<
" known languages";
145 return known_languages;
148 std::vector<language_def> result;
149 std::copy_if(known_languages.begin(), known_languages.end(), std::back_inserter(result),
150 [](
const language_def& lang) { return lang.percent >= min_translation_percent; });
152 LOG_G <<
"Found " << result.size() <<
" sufficiently translated languages";
158 return min_translation_percent;
162 min_translation_percent = percent;
167 std::vector<std::string>
const *alternates)
169 std::string locale = slocale;
180 std::vector<std::string>::const_iterator
i;
181 if (alternates)
i = alternates->begin();
185 std::string lang = locale, extra;
186 std::string::size_type pos = locale.find(
'@');
187 if (pos != std::string::npos) {
189 extra = locale.substr(pos);
196 char const *encoding[] {
".utf-8",
".UTF-8",
"" };
197 for (
int j = 0; j != 3; ++j)
199 locale = lang + encoding[j] + extra;
200 res = std::setlocale(category, locale.c_str());
202 LOG_G <<
"Set locale to '" << locale <<
"' result: '" << res <<
"'.";
207 if (!alternates ||
i == alternates->end())
break;
212 WRN_G <<
"setlocale() failed for '" << slocale <<
"'.";
214 if (category == LC_TIME) {
227 DBG_G <<
"Numeric locale: " << std::setlocale(LC_NUMERIC,
nullptr);
228 DBG_G <<
"Full locale: " << std::setlocale(LC_ALL,
nullptr);
235 std::string locale_lc;
239 current_language = locale;
250 if(complain && languages.empty()) {
251 PLAIN_LOG <<
"No [language] block found";
254 for(
const config& lang : languages) {
255 for(
const auto& [key, value] : lang.attribute_range()) {
256 strings_[key] = value.t_str();
267 assert(!known_languages.empty());
269 const std::string& prefs_locale =
prefs::get().locale();
270 if(prefs_locale.empty() ==
false) {
273 if(prefs_locale == def.localename) {
277 LOG_G <<
"'" << prefs_locale <<
"' locale not found in known array; defaulting to system locale";
278 return known_languages[0];
281 LOG_G <<
"locale could not be determined; defaulting to system locale";
282 return known_languages[0];
289 const std::string &name =
t[
"name"];
290 const std::string &
path =
t[
"path"];
298 WRN_G <<
"no location found for '" <<
path <<
"', skipping textdomain";
307 languages.push_back(l);
A config object defines a single node in a WML file, with access to child nodes.
child_itors child_range(std::string_view key)
A class grating read only view to a vector of config objects, viewed as one config with all children ...
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.