Module:ItemData/nav: Difference between revisionsGive feedback
Gammaton32 (talk | contribs) 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, | local function write_wrapped_item_list(slot, min_souls, max_souls, template, sep) | ||
if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then | |||
return 'slot must be Weapon, Armor (Vitality), or Tech (Spirit)' | |||
end | |||
local min_souls = tonumber(min_souls) | |||
local max_souls = tonumber(max_souls) | |||
if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end | |||
-- Retrieve all items that fit the bounds | |||
local items = {} | |||
for item_key, item_data in pairs(items_data) do | |||
-- future proofing; Disabled will be renamed to IsDisabled soon | |||
local this_cost = tonumber(item_data["Cost"]) | |||
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 slot == this_slot and this_cost >= min_souls and this_cost < max_souls then | |||
local item_name_english = lang_module.get_string(item_key, "en") -- Get the English name | |||
local lang_code = get_language_from_subpage() -- Get the language code from the subpage | |||
-- Construct the template based on the type | |||
local item_template | |||
if template == "ItemIcon" then | |||
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 .. "}}" | |||
elseif template == "ItemBox" then | |||
if lang_code == "en" then | |||
-- For English pages, do not include item_loc or link | |||
item_template = "{{ItemBox|item_name=" .. item_name_english .. "}}" | |||
else | |||
-- 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 | |||
local link = item_name_english .. "/" .. lang_code -- Add subpage for non-English languages | |||
item_template = "{{ItemBox|item_name=" .. item_name_english .. "|item_loc=" .. item_name_local .. "|link=" .. link .. "}}" | |||
end | |||
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) | ||
-- 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 sep = ' • ' | |||
local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemIcon", sep) | |||
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) | ||
-- 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 sep = ' ' | |||
local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemBox", sep) | |||
return process_debug_mode(item_list, debug_mode) | |||
end | end | ||