Jump to content

Module:ForeignLanguageTextTable: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 2: Line 2:


function p.generateTable(frame)
function p.generateTable(frame)
-- Get parameters from the template call
-- Input variables from the template
local args = frame:getParent().args
local lang1 = frame.args["lang1"]
local lang2 = frame.args["lang2"]

-- Handle args explicitly to ensure parameters are properly retrieved
local lang1 = args['lang1']
local texts = {}
local lang2 = args['lang2']
local translations = {}

-- Collect up to 10 text and translation pairs
-- Base of the table
for i = 1, 10 do
local tableStart = '{| class="wikitable"\n|+\n'
local text = frame.args["text" .. i]
local tableHeader = '!{{ISO 639 name|' .. lang1 .. '}}\n!{{ISO 639 name|' .. lang2 .. '}}\n|-\n'
local tableBody = ''
local trans = frame.args["trans" .. i]

-- Function to add a row for text and translation
if text and trans then
table.insert(texts, text)
local function addRow(text, translation)
table.insert(translations, trans)
if text and translation and text ~= '' and translation ~= '' then
tableBody = tableBody .. '|{{Langx|' .. lang1 .. '|' .. text .. '|label=none}}\n'
tableBody = tableBody .. '|{{Langx|' .. lang2 .. '|' .. translation .. '|label=none|italic=' .. (args.useitalic or 'no') .. '}}\n|-\n'
end
end
end
end

-- Add the first two rows (mandatory ones)
-- Start building the table
local tableStr = '{| class="wikitable"\n'
addRow(args['text1'], args['translation1'])
tableStr = tableStr .. "! " .. lang1 .. " !! " .. lang2 .. "\n"
addRow(args['text2'], args['translation2'])

-- Loop through the texts and translations and add rows to the table
-- Check optional text/translation fields dynamically (text3, text4, etc.)
for i = 1, #texts do
for i = 3, 10 do -- Adjust the loop limit based on how many pairs you want to support
local text = args['text' .. i]
tableStr = tableStr .. "|-\n"
tableStr = tableStr .. "| " .. texts[i] .. " || " .. translations[i] .. "\n"
local translation = args['translation' .. i]
addRow(text, translation)
end
end

-- Combine the table parts
-- Close the table
local tableEnd = '|}\n'
tableStr = tableStr .. "|}"
return tableStart .. tableHeader .. tableBody .. tableEnd
return tableStr
end
end



Revision as of 18:09, 17 October 2024

local p = {}

function p.generateTable(frame)
    -- Input variables from the template
    local lang1 = frame.args["lang1"]
    local lang2 = frame.args["lang2"]
    
    local texts = {}
    local translations = {}
    
    -- Collect up to 10 text and translation pairs
    for i = 1, 10 do
        local text = frame.args["text" .. i]
        local trans = frame.args["trans" .. i]
        
        if text and trans then
            table.insert(texts, text)
            table.insert(translations, trans)
        end
    end
    
    -- Start building the table
    local tableStr = '{| class="wikitable"\n'
    tableStr = tableStr .. "! " .. lang1 .. " !! " .. lang2 .. "\n"
    
    -- Loop through the texts and translations and add rows to the table
    for i = 1, #texts do
        tableStr = tableStr .. "|-\n"
        tableStr = tableStr .. "| " .. texts[i] .. " || " .. translations[i] .. "\n"
    end
    
    -- Close the table
    tableStr = tableStr .. "|}"
    
    return tableStr
end

return p