Module:languages-oow: Difference between revisions
Jump to navigation
Jump to search
TheNightAvl (talk | contribs) No edit summary |
TheNightAvl (talk | contribs) No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
function format_gender(gender) | function format_gender(gender) | ||
local temp = gender | local temp = gender | ||
for code in string.gmatch(gender, " | for code in string.gmatch(gender, "[a-z]+") do | ||
gender_obj = assert(available_genders[code], "Invalid gender code detected") | gender_obj = assert(available_genders[code], "Invalid gender code detected") | ||
temp = string.gsub(temp, code, '<abbr title="' .. gender_obj.hint .. '">' .. gender_obj.label .. "</abbr>") | temp = string.gsub(temp, code, '<abbr title="' .. gender_obj.hint .. '">' .. gender_obj.label .. "</abbr>") | ||
end | end | ||
return '<span class="gender">' .. temp .. "</span>" | return '<span class="gender">' .. temp .. "</span>" | ||
end | |||
function export.is_valid_code(code) | |||
return data[code] ~= nil | |||
end | end | ||
Line 20: | Line 24: | ||
local language = data[code] | local language = data[code] | ||
return language.flag or ("[" .. string.upper(language.code) .. "]") | return language.flag or ("[" .. string.upper(language.code) .. "]") | ||
end | |||
function export.get_name(code) | |||
return data[code].name | |||
end | end | ||
Line 26: | Line 34: | ||
end | end | ||
function export.get_linked_term(code, term, gender) | function export.get_linked_term(code, term, alt, gender) | ||
local out = "[[wikt:" .. term .. "#" .. data[code].name .. "|" .. term .. "]]" | local out = "[[wikt:" .. term .. "#" .. data[code].name .. "|" .. (alt or term) .. "]]" | ||
if gender then out = out .. " " .. format_gender(gender) end | if gender then out = out .. " " .. format_gender(gender) end | ||
return out | return out | ||
Line 43: | Line 51: | ||
while args[i] or args[i+1] do | while args[i] or args[i+1] do | ||
if args[i] == "" or args[i] == nil then | if args[i] == "" or args[i] == nil then | ||
out = out .. "; " | if data[args[i+1]] then | ||
out = out .. " — " | |||
else | |||
out = out .. "; " | |||
end | |||
elseif data[args[i]] and (args[i-1] == "" or args[i-1] == nil) and args[i+1] then | |||
if not args["nocat"] then out = out .. "[[Category:Terms with " .. language.name .. " translations]] " end | |||
language = data[args[i]] | |||
out = out .. export.get_flag(language.code) .. " " | |||
else | else | ||
term, term_args = m_inline.parse(args[i]) | term, term_args = m_inline.parse(args[i]) | ||
if | if mw.ustring.sub(out, -1) ~= " " then out = out .. ", " end | ||
out = out .. export.get_linked_term(language.code, term, term_args.g) | out = out .. export.get_linked_term(language.code, term, term_args.alt, term_args.g) | ||
end | end | ||
Latest revision as of 23:13, 25 December 2024
Underlies {{def+}}
.
local export = {}
local getArgs = require('Module:Arguments').getArgs
local m_inline = require("Module:inline")
local data = mw.loadData("Module:languages-oow/data")
local available_genders = mw.loadData("Module:gender and number/data")
function format_gender(gender)
local temp = gender
for code in string.gmatch(gender, "[a-z]+") do
gender_obj = assert(available_genders[code], "Invalid gender code detected")
temp = string.gsub(temp, code, '<abbr title="' .. gender_obj.hint .. '">' .. gender_obj.label .. "</abbr>")
end
return '<span class="gender">' .. temp .. "</span>"
end
function export.is_valid_code(code)
return data[code] ~= nil
end
function export.get_flag(code)
local language = data[code]
return language.flag or ("[" .. string.upper(language.code) .. "]")
end
function export.get_name(code)
return data[code].name
end
function export.get_linked_name(code)
return "[[w:" .. data[code].wiki .. "|" .. data[code].name .. "]]"
end
function export.get_linked_term(code, term, alt, gender)
local out = "[[wikt:" .. term .. "#" .. data[code].name .. "|" .. (alt or term) .. "]]"
if gender then out = out .. " " .. format_gender(gender) end
return out
end
function export.definition(frame)
local args = getArgs(frame)
if args[2] == nil then error("At least one term is required") end
local language = assert(data[args[1]], "First argument needs to be a recognised language code")
local out = export.get_flag(language.code) .. " "
local i = 2
while args[i] or args[i+1] do
if args[i] == "" or args[i] == nil then
if data[args[i+1]] then
out = out .. " — "
else
out = out .. "; "
end
elseif data[args[i]] and (args[i-1] == "" or args[i-1] == nil) and args[i+1] then
if not args["nocat"] then out = out .. "[[Category:Terms with " .. language.name .. " translations]] " end
language = data[args[i]]
out = out .. export.get_flag(language.code) .. " "
else
term, term_args = m_inline.parse(args[i])
if mw.ustring.sub(out, -1) ~= " " then out = out .. ", " end
out = out .. export.get_linked_term(language.code, term, term_args.alt, term_args.g)
end
i = i + 1
end
if not args["nocat"] then out = out .. "[[Category:Terms with " .. language.name .. " translations]]" end
return out
end
return export
--[[
Debug console test string:
=p.definition(mw.getCurrentFrame():newChild{title="whatever",args={"de", "Zustand", "Verfassung"}})
]]--