gui2/title_screen: Give the Language button its label back

This makes the Language button use one of the new title screen button
definitions so it has an actual label in addition to its icon. However,
it also makes the label dynamic to reflect the current language choice.

More specifically, it reflects the current locale's language name, which
turns out requires a surprising amount of effort on our part to obtain.
This commit is contained in:
Iris Morelle 2021-09-23 12:05:25 -03:00
parent df9f12568e
commit da7d8c6aed
No known key found for this signature in database
GPG key ID: E312033F4023A753
2 changed files with 21 additions and 1 deletions

View file

@ -480,7 +480,7 @@ where
horizontal_alignment = "right"
[button]
id = "language"
definition = "action_language"
definition = "titlescreen_language"
label = _ "Language"
tooltip = _ "Change the language"
[/button]

View file

@ -38,6 +38,7 @@
#include "gui/dialogs/preferences_dialog.hpp"
#include "gui/dialogs/screenshot_notification.hpp"
#include "gui/dialogs/simple_item_selector.hpp"
#include "language.hpp"
#include "log.hpp"
#include "preferences/game.hpp"
//#define DEBUG_TOOLTIP
@ -59,6 +60,8 @@
#include <algorithm>
#include <functional>
#include <boost/algorithm/string/erase.hpp>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define WRN_CF LOG_STREAM(warn, log_config)
@ -311,6 +314,23 @@ void title_screen::pre_show(window& win)
}
});
if(auto* lang_button = find_widget<button>(&win, "language", false, false); lang_button) {
const auto& locale = translation::get_effective_locale_info();
const auto& langs = get_languages(true);
auto lang_def = std::find_if(langs.begin(), langs.end(), [&](language_def const& lang) {
// Just assume everything is UTF-8 (it should be as long as we're called Wesnoth)
// and strip the charset from the Boost locale identifier.
const auto& boost_name = boost::algorithm::erase_first_copy(locale.name(), ".UTF-8");
// std::cerr << lang.localename << '/' << boost_name << '\n';
return lang.localename == boost_name;
});
// If somehow the locale doesn't match a known translation, use the
// locale identifier as a last resort
lang_button->set_label(lang_def != langs.end() ? lang_def->language.str() : locale.name());
}
//
// Preferences
//