From 65dc4b42d32e2afba3e0f4e7e32b46162d13cb9e Mon Sep 17 00:00:00 2001 From: luk3yx Date: Fri, 29 Jul 2022 23:12:22 +1200 Subject: [PATCH] Mobile: add change language dropdown and reset settings button (#75) --- builtin/mainmenu/async_event.lua | 3 +- builtin/mainmenu/tab_settings_simple.lua | 129 ++++++++++++++++++++--- src/main.cpp | 13 +++ 3 files changed, 130 insertions(+), 15 deletions(-) diff --git a/builtin/mainmenu/async_event.lua b/builtin/mainmenu/async_event.lua index 1cb657ff5..ec4755a70 100644 --- a/builtin/mainmenu/async_event.lua +++ b/builtin/mainmenu/async_event.lua @@ -1,4 +1,5 @@ -core.async_jobs = {} +-- The main menu may be reloaded when the language is changed +core.async_jobs = core.async_jobs or {} local function handle_job(jobid, serialized_retval) local retval = core.deserialize(serialized_retval) diff --git a/builtin/mainmenu/tab_settings_simple.lua b/builtin/mainmenu/tab_settings_simple.lua index 082c9d04b..8a5f3893d 100644 --- a/builtin/mainmenu/tab_settings_simple.lua +++ b/builtin/mainmenu/tab_settings_simple.lua @@ -16,6 +16,39 @@ --with this program; if not, write to the Free Software Foundation, Inc., --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +local function create_confirm_reset_dlg() + return dialog_create("reset_all_settings", + function() + return + "image_button[2,1;8,3;" .. core.formspec_escape(defaulttexturedir .. + "blank.png") .. ";;" .. fgettext("Reset all settings?") .. + ";true;false;]" .. + "style[reset_confirm;bgcolor=red]" .. + "button[3,4.8;3,0.5;reset_confirm;" .. fgettext("Reset") .. "]" .. + "button[6,4.8;3,0.5;reset_cancel;" .. fgettext("Cancel") .. "]" + end, + function(this, fields) + if fields["reset_confirm"] then + for _, setting_name in ipairs(core.settings:get_names()) do + if not setting_name:find(".", 1, true) and + setting_name ~= "maintab_LAST" then + core.settings:remove(setting_name) + end + end + + -- Reload the entire main menu + dofile(core.get_builtin_path() .. "init.lua") + return true + end + + if fields["reset_cancel"] then + this:delete() + return true + end + end, + nil, true) +end + -------------------------------------------------------------------------------- local labels = { @@ -43,36 +76,77 @@ local getSettingIndex = { end } +-- Get a list of languages and language names +local path_locale = core.get_builtin_path() .. ".." .. DIR_DELIM .. "locale" +local languages = core.get_dir_list(path_locale, true) +local language_names = {} +for i = #languages, 1, -1 do + local language = languages[i] + local f = io.open(path_locale .. DIR_DELIM .. language .. DIR_DELIM .. + "LC_MESSAGES" .. DIR_DELIM .. "minetest.mo") + if f then + -- HACK + local name = f:read("*a"):match("\nLanguage%-Team: ([^\\\n\"]+) get("language"), 0, nullptr); + g_client_translations->clear(); +} +#endif + static bool init_common(const Settings &cmd_args, int argc, char *argv[]) { startup_message(); @@ -511,6 +521,9 @@ static bool init_common(const Settings &cmd_args, int argc, char *argv[]) init_gettext(porting::path_locale.c_str(), g_settings->get("language"), argc, argv); +#if !defined(_MSC_VER) && !defined(SERVER) + g_settings->registerChangedCallback("language", language_setting_changed, nullptr); +#endif return true; }