Change language on the fly in the advanced settings dialog (#100)
This commit is contained in:
parent
7f4a384c69
commit
48e8b205ec
@ -259,3 +259,44 @@ function menu_worldmt(selected, setting, value)
|
||||
return nil
|
||||
end
|
||||
end
|
||||
--------------------------------------------------------------------------------
|
||||
function get_language_list()
|
||||
-- Get a list of languages and language names
|
||||
local path_locale = core.get_locale_path()
|
||||
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\"]+) <https://")
|
||||
language_names[language] = name or language
|
||||
f:close()
|
||||
else
|
||||
table.remove(languages, i)
|
||||
end
|
||||
end
|
||||
|
||||
languages[#languages + 1] = "en"
|
||||
language_names.en = "English"
|
||||
|
||||
-- Sort the languages list based on their human readable name
|
||||
table.sort(languages, function(a, b)
|
||||
return language_names[a] < language_names[b]
|
||||
end)
|
||||
|
||||
local language_name_list = {}
|
||||
for i, language in ipairs(languages) do
|
||||
language_name_list[i] = core.formspec_escape(language_names[language])
|
||||
end
|
||||
local language_dropdown = table.concat(language_name_list, ",")
|
||||
|
||||
local lang_idx = table.indexof(languages, fgettext("LANG_CODE"))
|
||||
if lang_idx < 0 then
|
||||
lang_idx = table.indexof(languages, "en")
|
||||
end
|
||||
|
||||
return languages, language_dropdown, lang_idx, language_name_list
|
||||
end
|
||||
|
@ -516,6 +516,7 @@ local full_settings = parse_config_file(false, true)
|
||||
local search_string = ""
|
||||
local settings = full_settings
|
||||
local selected_setting = 1
|
||||
local languages, language_dropdown, lang_idx, language_name_list = get_language_list()
|
||||
|
||||
local function get_current_value(setting)
|
||||
local value = core.settings:get(setting.name)
|
||||
@ -579,7 +580,14 @@ local function create_change_setting_formspec(dialogdata)
|
||||
local formspec = ""
|
||||
|
||||
-- Setting-specific formspec elements
|
||||
if setting.type == "bool" then
|
||||
if setting.name == "language" then
|
||||
-- Special case for the change language setting
|
||||
-- The first option is empty (so lang_idx is offset by 1)
|
||||
formspec = "dropdown[3," .. height .. ";4,1;dd_language;,"
|
||||
.. language_dropdown .. ";" .. lang_idx + 1 .. ";true]"
|
||||
height = height + 1.25
|
||||
|
||||
elseif setting.type == "bool" then
|
||||
local selected_index = 1
|
||||
if core.is_yes(get_current_value(setting)) then
|
||||
selected_index = 2
|
||||
@ -830,10 +838,36 @@ local function create_change_setting_formspec(dialogdata)
|
||||
)
|
||||
end
|
||||
|
||||
-- Reload the main menu so that everything uses the new language
|
||||
local function reload_main_menu()
|
||||
dofile(core.get_builtin_path() .. "init.lua")
|
||||
|
||||
-- Open a new advanced settings dialog
|
||||
local maintab = ui.find_by_name("maintab")
|
||||
local adv_settings_dlg = create_adv_settings_dlg(search_string,
|
||||
settings, selected_setting)
|
||||
adv_settings_dlg:set_parent(maintab)
|
||||
maintab:hide()
|
||||
adv_settings_dlg:show()
|
||||
end
|
||||
|
||||
local function handle_change_setting_buttons(this, fields)
|
||||
local setting = settings[selected_setting]
|
||||
if fields["btn_done"] or fields["key_enter"] then
|
||||
if setting.type == "bool" then
|
||||
if setting.name == "language" then
|
||||
local new_idx = tonumber(fields["dd_language"]) - 1
|
||||
if lang_idx ~= new_idx then
|
||||
if languages[new_idx] then
|
||||
core.settings:set("language", languages[new_idx])
|
||||
else
|
||||
core.settings:remove("language")
|
||||
end
|
||||
|
||||
reload_main_menu()
|
||||
return true
|
||||
end
|
||||
|
||||
elseif setting.type == "bool" then
|
||||
local new_value = fields["dd_setting_value"]
|
||||
-- Note: new_value is the actual (translated) value shown in the dropdown
|
||||
core.settings:set_bool(setting.name, new_value == fgettext("Enabled"))
|
||||
@ -1020,6 +1054,10 @@ local function create_settings_formspec(tabview, _, tabdata)
|
||||
formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
|
||||
.. core.formspec_escape(get_current_np_group_as_string(entry)) .. ","
|
||||
|
||||
elseif entry.name == "language" then
|
||||
formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
|
||||
.. (language_name_list[lang_idx] or "") .. ","
|
||||
|
||||
else
|
||||
formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
|
||||
.. core.formspec_escape(get_current_value(entry)) .. ","
|
||||
@ -1106,7 +1144,10 @@ local function handle_settings_buttons(this, fields, tabname, tabdata)
|
||||
if setting and setting.type ~= "category" then
|
||||
core.settings:remove(setting.name)
|
||||
core.settings:write()
|
||||
core.update_formspec(this:get_formspec())
|
||||
|
||||
if setting.name == "language" then
|
||||
reload_main_menu()
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
@ -1126,7 +1167,11 @@ local function handle_settings_buttons(this, fields, tabname, tabdata)
|
||||
return false
|
||||
end
|
||||
|
||||
function create_adv_settings_dlg()
|
||||
function create_adv_settings_dlg(new_search_string, new_settings, new_selected_setting)
|
||||
search_string = new_search_string or search_string
|
||||
settings = new_settings or settings
|
||||
selected_setting = new_selected_setting or selected_setting
|
||||
|
||||
local dlg = dialog_create("settings_advanced",
|
||||
create_settings_formspec,
|
||||
handle_settings_buttons,
|
||||
|
@ -76,42 +76,7 @@ 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\"]+) <https://")
|
||||
language_names[language] = name or language
|
||||
f:close()
|
||||
else
|
||||
table.remove(languages, i)
|
||||
end
|
||||
end
|
||||
|
||||
languages[#languages + 1] = "en"
|
||||
language_names.en = "English"
|
||||
|
||||
-- Sort the languages list based on their human readable name
|
||||
table.sort(languages, function(a, b)
|
||||
return language_names[a] < language_names[b]
|
||||
end)
|
||||
|
||||
local language_name_list = {}
|
||||
for i, language in ipairs(languages) do
|
||||
language_name_list[i] = core.formspec_escape(language_names[language])
|
||||
end
|
||||
local language_dropdown = table.concat(language_name_list, ",")
|
||||
|
||||
local lang_idx = table.indexof(languages, fgettext("LANG_CODE"))
|
||||
if lang_idx < 0 then
|
||||
lang_idx = table.indexof(languages, "en")
|
||||
end
|
||||
local languages, language_dropdown, lang_idx = get_language_list()
|
||||
|
||||
local function formspec(tabview, name, tabdata)
|
||||
local fps = tonumber(core.settings:get("fps_max"))
|
||||
|
@ -228,6 +228,7 @@ void init_gettext(const char *path, const std::string &configured_language,
|
||||
char lang[3] = {0};
|
||||
strncpy(lang, locale[0].language, 2);
|
||||
setenv("LANG", lang, 1);
|
||||
setenv("LANGUAGE", lang, 1);
|
||||
}
|
||||
|
||||
SDL_free(locale);
|
||||
@ -237,6 +238,7 @@ void init_gettext(const char *path, const std::string &configured_language,
|
||||
NSString *syslang = [[NSLocale preferredLanguages] firstObject];
|
||||
[syslang getBytes:lang maxLength:2 usedLength:nil encoding:NSASCIIStringEncoding options:0 range:NSMakeRange(0, 2) remainingRange:nil];
|
||||
setenv("LANG", lang, 1);
|
||||
setenv("LANGUAGE", lang, 1);
|
||||
#endif
|
||||
setlocale(LC_ALL, "");
|
||||
}
|
||||
|
@ -555,6 +555,13 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_locale_path(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_locale).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_cache_path(lua_State *L)
|
||||
{
|
||||
@ -1000,6 +1007,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
|
||||
API_FCT(get_serverlistpath);
|
||||
API_FCT(get_texturepath);
|
||||
API_FCT(get_texturepath_share);
|
||||
API_FCT(get_locale_path);
|
||||
API_FCT(get_cache_path);
|
||||
API_FCT(get_temp_path);
|
||||
API_FCT(create_dir);
|
||||
|
@ -122,6 +122,8 @@ private:
|
||||
|
||||
static int l_get_texturepath_share(lua_State *L);
|
||||
|
||||
static int l_get_locale_path(lua_State *L);
|
||||
|
||||
static int l_get_cache_path(lua_State *L);
|
||||
|
||||
static int l_get_temp_path(lua_State *L);
|
||||
|
Loading…
x
Reference in New Issue
Block a user