search items / groups in translated strings

master
Alexander Weber 2020-12-01 09:14:44 +01:00
parent 8b807e4cf8
commit f790cc8bb3
4 changed files with 30 additions and 9 deletions

View File

@ -7,6 +7,13 @@ local inventory_form = smartfs.create("smart_inventory:main", function(state)
-- enhanced object to the main inventory functions
state.param.invobj = maininv.get(state.location.player)
-- Set language code
local player_info = minetest.get_player_information(state.location.player)
if player_info and player_info.lang_code ~= "" then
state.lang_code = player_info.lang_code
end
-- tabbed view controller
local tab_controller = {
_tabs = {},

View File

@ -685,7 +685,7 @@ local function crafting_callback(state)
return
end
local filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list_all, search_string)
local filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list_all, search_string, state.location.rootState.lang_code)
filtered_list = ui_tools.filter_by_revealed(filtered_list, player)
state.param.crafting_grouped_items = ui_tools.get_list_grouped(filtered_list)
update_group_selection(state, true)

View File

@ -158,9 +158,10 @@ local function creative_callback(state)
searchfield:setCloseOnEnter(false)
searchfield:onKeyEnter(function(self, state, player)
local search_string = self:getText()
local filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list, search_string)
local lang_code = state.location.rootState.lang_code
local filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list, search_string, lang_code)
state.param.creative_grouped_items = ui_tools.get_list_grouped(filtered_list)
filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list_shape, search_string)
filtered_list = ui_tools.filter_by_searchstring(ui_tools.root_list_shape, search_string, lang_code)
state.param.creative_grouped_shape_items = filtered_list
update_group_selection(state, 0)
end)

View File

@ -104,19 +104,32 @@ end
-----------------------------------------------------
-- Filter a list by search string
-----------------------------------------------------
function ui_tools.filter_by_searchstring(list, search_string)
function ui_tools.filter_by_searchstring(list, search_string, lang_code)
local filtered_list = {}
search_string = search_string:lower()
for _, entry in ipairs(list) do
local def = minetest.registered_items[entry.item]
if string.find(def.description:lower(), search_string) or
string.find(def.name:lower(), search_string) then
local description = def.description
if lang_code then
description = minetest.get_translated_string(lang_code, description)
end
if string.find(description:lower(), search_string) or
string.find(def.name:lower(), search_string) then
table.insert(filtered_list, entry)
else
for _, cgroup in pairs(entry.citem.cgroups) do
if cgroup.keyword and string.find(cgroup.keyword:lower():gsub("_", ":"), search_string:gsub("_", ":")) then
table.insert(filtered_list, entry)
break
if cgroup.keyword then
if string.find(cgroup.keyword:lower():gsub("_", ":"), search_string:gsub("_", ":"))then
table.insert(filtered_list, entry)
break
end
end
if cgroup.group_desc then
local group_desc =txt[cgroup.group_desc] or cgroup.group_desc
if string.find(group_desc:lower(), search_string)then
table.insert(filtered_list, entry)
break
end
end
end
end