29 #if !defined(_MSC_VER) && !defined(__MINGW32__)
30 extern "C" int _putenv(
const char*);
39 #include <SDL2/SDL_system.h>
43 #define DBG_G LOG_STREAM(debug, lg::general())
44 #define LOG_G LOG_STREAM(info, lg::general())
45 #define WRN_G LOG_STREAM(warn, lg::general())
46 #define ERR_G LOG_STREAM(err, lg::general())
50 std::vector<config> languages;
51 std::vector<language_def> known_languages;
53 int min_translation_percent = 80;
56 std::map<std::string, std::string> translation_names;
79 static bool result =
true;
85 const utils::string_map::const_iterator
i = strings_.find(key);
86 if(
i != strings_.end()) {
94 empty_string =
"UNTLB " + key;
101 return (*
this)[std::string(key)];
106 return strings_.find(key);
111 return strings_.end();
115 : language(
t_string(
N_(
"System default language"),
"wesnoth"))
122 : localename(
cfg[
"locale"])
125 : localename(
cfg[
"windows_locale"].str(
"C"))
128 , language(
cfg[
"name"].t_str())
129 , sort_name(
cfg[
"sort_name"].str(language))
130 , rtl(
cfg[
"dir"] ==
"rtl")
131 , percent(
cfg[
"percent"].to_int())
134 translation_names.emplace(localename, language);
136 for(
const auto& alternate : alternates) {
137 translation_names.emplace(alternate, language);
146 known_languages.clear();
147 known_languages.emplace_back();
150 known_languages.emplace_back(lang);
155 }
catch(
const utils::bad_optional_access&) {
166 std::sort(known_languages.begin(), known_languages.end());
168 if(all || min_translation_percent == 0) {
169 LOG_G <<
"Found " << known_languages.size() <<
" known languages";
170 return known_languages;
173 std::vector<language_def> result;
174 std::copy_if(known_languages.begin(), known_languages.end(), std::back_inserter(result),
175 [](
const language_def& lang) { return lang.percent >= min_translation_percent; });
177 LOG_G <<
"Found " << result.size() <<
" sufficiently translated languages";
183 auto itor = translation_names.find(locale_id);
184 return itor != translation_names.end() ? itor->second :
"";
189 return min_translation_percent;
193 min_translation_percent = percent;
198 std::vector<std::string>
const *alternates)
200 std::string locale = slocale;
211 std::vector<std::string>::const_iterator
i;
212 if (alternates)
i = alternates->begin();
216 std::string lang = locale, extra;
217 std::string::size_type pos = locale.find(
'@');
218 if (pos != std::string::npos) {
220 extra = locale.substr(pos);
227 char const *encoding[] {
".utf-8",
".UTF-8",
"" };
228 for (
int j = 0; j != 3; ++j)
230 locale = lang + encoding[j] + extra;
231 res = std::setlocale(category, locale.c_str());
233 LOG_G <<
"Set locale to '" << locale <<
"' result: '" << res <<
"'.";
238 if (!alternates ||
i == alternates->end())
break;
243 WRN_G <<
"setlocale() failed for '" << slocale <<
"'.";
245 if (category == LC_TIME) {
258 DBG_G <<
"Numeric locale: " << std::setlocale(LC_NUMERIC,
nullptr);
259 DBG_G <<
"Full locale: " << std::setlocale(LC_ALL,
nullptr);
266 current_language = locale;
273 JNIEnv* env =
reinterpret_cast<JNIEnv*
>(SDL_AndroidGetJNIEnv());
274 jobject wesnoth_instance =
reinterpret_cast<jobject
>(SDL_AndroidGetActivity());
275 jclass wesnoth_activity(env->GetObjectClass(wesnoth_instance));
276 jmethodID locale = env->GetMethodID(wesnoth_activity,
"getLocaleCode",
"()Ljava/lang/String;");
277 jstring lcode =
reinterpret_cast<jstring
>(env->CallObjectMethod(wesnoth_instance, locale));
278 localename = env->GetStringUTFChars(lcode,
nullptr);
280 if(env->ExceptionCheck() == JNI_TRUE) {
281 env->ExceptionDescribe();
282 env->ExceptionClear();
285 env->DeleteLocalRef(wesnoth_instance);
286 env->DeleteLocalRef(wesnoth_activity);
298 if(complain && languages.empty()) {
299 PLAIN_LOG <<
"No [language] block found";
302 for(
const config& lang : languages) {
303 for(
const auto& [key, value] : lang.attribute_range()) {
304 strings_[key] = value.t_str();
315 assert(!known_languages.empty());
317 const std::string& prefs_locale =
prefs::get().locale();
318 if(prefs_locale.empty() ==
false) {
321 if(prefs_locale == def.localename) {
325 LOG_G <<
"'" << prefs_locale <<
"' locale not found in known array; defaulting to system locale";
326 return known_languages[0];
329 LOG_G <<
"locale could not be determined; defaulting to system locale";
330 return known_languages[0];
337 const std::string &name =
t[
"name"];
338 const std::string &
path =
t[
"path"];
346 WRN_G <<
"no location found for '" <<
path <<
"', skipping textdomain";
355 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()
std::string get_translation_name(const std::string &locale_id)
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::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 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::string short_localename() 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.