Module:ItemData/nav: Difference between revisions

Undo revision 22015 by Gammaton32 (talk)
Tag: Undo
added translation subpage support
Line 4: Line 4:
local generic_module = require('Module:GenericData')
local generic_module = require('Module:GenericData')
local util_module = require('Module:Utilities')
local util_module = require('Module:Utilities')
local function get_language_from_subpage()
    -- Get the current page title
    local page_title = mw.title.getCurrentTitle().text
   
    -- Check if the page is a subpage (e.g., "Items/ru")
    local subpage_lang = page_title:match("/(%a%a)$") -- Match two-letter language code at the end of the subpage
   
    -- If a language code is found, return it; otherwise, default to English ("en")
    return subpage_lang or "en"
end


--With debug_mode on, it outputs unprocessed wikitext
--With debug_mode on, it outputs unprocessed wikitext
Line 21: Line 32:
-- get_item_nav_bulletpoints. 'sep' should not be combined with 'right_wrap',  
-- get_item_nav_bulletpoints. 'sep' should not be combined with 'right_wrap',  
-- as the trailing separator should also be removed from the string
-- as the trailing separator should also be removed from the string
local function write_wrapped_item_list(slot, min_souls, max_souls, left_wrap, right_wrap, sep)
local function write_wrapped_item_list(slot, min_souls, max_souls, template, sep)
if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
    if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
return 'slot must be Weapon, Armor (Vitality), or Tech (Spirit)'
        return 'slot must be Weapon, Armor (Vitality), or Tech (Spirit)'
end
    end
   
local min_souls = tonumber(min_souls)
    local min_souls = tonumber(min_souls)
local max_souls = tonumber(max_souls)
    local max_souls = tonumber(max_souls)
if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
    if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
   
--Retrieve all items that fit the bounds
    -- Retrieve all items that fit the bounds
local items = {}
    local items = {}
for item_key, item_data in pairs(items_data) do
    for item_key, item_data in pairs(items_data) do
--future proofing; Disabled will be renamed to IsDisabled soon
        -- future proofing; Disabled will be renamed to IsDisabled soon
local this_cost = tonumber(item_data["Cost"])
        local this_cost = tonumber(item_data["Cost"])
local this_slot = item_data["Slot"]
        local this_slot = item_data["Slot"]
if item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil and this_slot ~= nil then
        if item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil and this_slot ~= nil then
if slot == this_slot and this_cost>=min_souls and this_cost<max_souls then
            if slot == this_slot and this_cost >= min_souls and this_cost < max_souls then
table.insert(items, lang_module.get_string(item_key))
                local item_name_english = lang_module.get_string(item_key, "en") -- Get the English name
end
                local lang_code = get_language_from_subpage() -- Get the language code from the subpage
end
               
end
                -- Construct the template based on the type
                local item_template
--Order list alphabetically
                if template == "ItemIcon" then
table.sort(items) --O(nlogn)
                    local item_name_local = lang_module.get_string(item_key, lang_code) -- Get the localized name
                    item_template = "{{ItemIcon|" .. item_name_english .. "|lang=" .. lang_code .. "|" .. item_name_local .. "}}"
--Add each item to output
                elseif template == "ItemBox" then
-- Each item is surrounded by left and right wrap, and separated by sep
                    if lang_code == "en" then
local ret = ''
                        -- For English pages, do not include item_loc or link
for index, item_name in ipairs(items) do
                        item_template = "{{ItemBox|item_name=" .. item_name_english .. "}}"
ret = ret .. left_wrap .. item_name .. right_wrap .. sep
                    else
end
                        -- For non-English pages, include item_loc and link
                        local item_name_local = lang_module.get_string(item_key, lang_code) -- Get the localized name
--Remove the last separator thats trailing after the last item
                        local link = item_name_english .. "/" .. lang_code -- Add subpage for non-English languages
ret = string.sub(ret, 1, -(string.len(sep))-1)
                        item_template = "{{ItemBox|item_name=" .. item_name_english .. "|item_loc=" .. item_name_local .. "|link=" .. link .. "}}"
                    end
return ret
                else
                    return "Invalid template type"
                end
                table.insert(items, item_template)
            end
        end
    end
   
    -- Order list alphabetically
    table.sort(items) -- O(nlogn)
   
    -- Add each item to output
    -- Each item is already wrapped in the template, so no need for additional wrapping
    local ret = table.concat(items, sep)
   
    return ret
end
end


-- for [[Template:Item Navbox]]
-- for [[Template:Item Navbox]]
function p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode)
function p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode)
-- If called internally (direct Lua call), args will be passed directly.
    -- If called from wikitext, `slot` will be the `frame` object, and we get args from `frame.args`.
     -- Handle the case where it's called via #invoke (i.e., from wikitext)
     -- Handle the case where it's called via #invoke (i.e., from wikitext)
     if type(slot) == "table" and slot.args then
     if type(slot) == "table" and slot.args then
Line 73: Line 96:
     end
     end
local left_wrap = '{{ItemIcon|'
    local sep = ' &bull; '
local right_wrap = '}}'
local sep = ' &bull; '
local item_list = write_wrapped_item_list(slot, min_souls, max_souls, left_wrap, right_wrap, sep)
    local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemIcon", sep)
return process_debug_mode(item_list, debug_mode)
    return process_debug_mode(item_list, debug_mode)
end
end


-- for [[Template:Infobox ShopItems]]
-- for [[Template:Infobox ShopItems]]
function p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode)
function p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode)
-- If called internally (direct Lua call), args will be passed directly.
    -- If called from wikitext, `slot` will be the `frame` object, and we get args from `frame.args`.
     -- Handle the case where it's called via #invoke (i.e., from wikitext)
     -- Handle the case where it's called via #invoke (i.e., from wikitext)
     if type(slot) == "table" and slot.args then
     if type(slot) == "table" and slot.args then
Line 96: Line 114:
     end
     end
local left_wrap = '{{ItemBox|item_name='
    local sep = ' '
local right_wrap = '}}'
local sep = ' '
local item_list = write_wrapped_item_list(slot, min_souls, max_souls, left_wrap, right_wrap, sep)
    local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemBox", sep)
return process_debug_mode(item_list, debug_mode)
    return process_debug_mode(item_list, debug_mode)
end
end