gui/title_screen: Refresh Language button after changing languages

Closes #7437.
This commit is contained in:
Iris Morelle 2023-05-18 22:54:16 -04:00
parent 454d816c86
commit 75bb77f11f
No known key found for this signature in database
GPG key ID: BB9666228F278524
3 changed files with 35 additions and 24 deletions

View file

@ -11,6 +11,8 @@
* Updated translations: Arabic, British English, Czech, French, Italian, Spanish
### Units
### User interface
* Fixed main menu Language button not being refreshed after switching languages
without relaunching the game (issue #7437).
### WML Engine
* Add support for distinct sub-achievements.
### Miscellaneous and Bug Fixes

View file

@ -316,36 +316,14 @@ void title_screen::init_callbacks()
try {
if(game_.change_language()) {
on_resize();
update_language_label();
}
} catch(const std::runtime_error& e) {
gui2::show_error_message(e.what());
}
});
if(auto* lang_button = find_widget<button>(this, "language", false, false); lang_button) {
const auto& locale = translation::get_effective_locale_info();
// 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");
const auto& langs = get_languages(true);
auto lang_def = std::find_if(langs.begin(), langs.end(), [&](language_def const& lang) {
return lang.localename == boost_name;
});
if(lang_def != langs.end()) {
lang_button->set_label(lang_def->language.str());
} else if(boost_name == "c" || boost_name == "C") {
// HACK: sometimes System Default doesn't match anything on the list. If you fork
// Wesnoth and change the neutral language to something other than US English, you
// want to change this too.
lang_button->set_label("English (US)");
} else {
// If somehow the locale doesn't match a known translation, use the
// locale identifier as a last resort
lang_button->set_label(boost_name);
}
}
update_language_label();
//
// Preferences
@ -384,6 +362,34 @@ void title_screen::init_callbacks()
}
}
void title_screen::update_language_label()
{
if(auto* lang_button = find_widget<button>(this, "language", false, false); lang_button) {
const auto& locale = translation::get_effective_locale_info();
// 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");
const auto& langs = get_languages(true);
auto lang_def = std::find_if(langs.begin(), langs.end(), [&](language_def const& lang) {
return lang.localename == boost_name;
});
if(lang_def != langs.end()) {
lang_button->set_label(lang_def->language.str());
} else if(boost_name == "c" || boost_name == "C") {
// HACK: sometimes System Default doesn't match anything on the list. If you fork
// Wesnoth and change the neutral language to something other than US English, you
// want to change this too.
lang_button->set_label("English (US)");
} else {
// If somehow the locale doesn't match a known translation, use the
// locale identifier as a last resort
lang_button->set_label(boost_name);
}
}
}
void title_screen::on_resize()
{
set_retval(REDRAW_BACKGROUND);

View file

@ -103,6 +103,9 @@ private:
*/
void update_tip(const bool previous);
/** Updates the Language button label. */
void update_language_label();
/** Shows the debug clock. */
void show_debug_clock_window();