Modul:Sprache

Aus Zeldapendium
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Sprache/Doku erstellt werden

local p = {}
local h = {}

function h.trimToEmpty(s)
    if not s then
        return ""
    else
        return s:gsub("^%s*(.-)%s*$", "%1")
    end
end

h.falsyStrings = {
    [""] = true,
    ["0"] = true,
    ["false"] = true,
    ["falsch"] = true,
    ["f"] = true,
    ["off"] = true,
    ["aus"] = true,
    ["no"] = true,
    ["nein"] = true,
    ["n"] = true
}

function h.strToBoolean(s, default)
    s = h.trimToEmpty(s)
    if default == nil then
        default = false
    end
    if s == "" then
        return default
    else
        return not h.falsyStrings[s:lower()]
    end
end

local nonLatin = {
    ["ja"] = true,
    ["zh"] = true,
    ["ko"] = true,
    ["el"] = true,
    ["ru"] = true,
    ["ar"] = true,
    ["th"] = true,
    ["hi"] = true
}

local function toHtml(content, lang, italics)
    out = mw.html.create("span")
    out:attr("lang", lang)
    local langBase = lang:gsub("^([a-zA-Z]+).*", "%1"):lower()
    if italics and (not nonLatin[langBase] or lang:lower():match("-latn")) then
        out:attr("style", "font-style: italic")
    end
    out:wikitext(content)
    return tostring(out)
end

-- args[1] ISO language code
-- args[2] Transcription
-- args["kursiv"] Print latin characters in italics (default true)
function p.transcription(args)
    local args = args
    if args == mw.getCurrentFrame() then
        args = args:getParent().args
    end
    
    local lang = h.trimToEmpty(args[1])
    assert(lang:len() >= 2, "Sprachcode ungültig")
    local langBase = lang:gsub("^([a-zA-Z]+).*", "%1")
    
    local transcription = h.trimToEmpty(args[2])
    assert(transcription ~= "", "Transkription fehlt")
    
    return toHtml(transcription, langBase .. "-Latn", h.strToBoolean(args["kursiv"], true))
end

-- args[1] ISO language code
-- args[2] Text in original language
-- args[3] Transcription of args[2] in latin characters
-- args["de"] German translation
-- args["kursiv"] Print transcription in italics (default true)
-- args["reihenfolge"] Output order (o = original, u = transcription, d = German translation) (default uod)
function p.format(args)
    local args = args
    if args == mw.getCurrentFrame() then
        args = args:getParent().args
    end

    local lang = h.trimToEmpty(args[1])
    assert(lang:len() >= 2, "Sprachcode ungültig")
    
    local parts = {}
    
    -- format original text
    
    local text = h.trimToEmpty(args[2])
    assert(text ~= "", "Text fehlt")
    parts["o"] = toHtml(text, lang, h.strToBoolean(args["kursiv"], true))
    
    -- format transcription
    
    local transcription = h.trimToEmpty(args[3])
    if transcription ~= "" then
        parts["u"] = p.transcription({[1] = lang, [2] = transcription, ["kursiv"] = args["kursiv"]})
    end
    
    -- format translation
    
    local translationDe = h.trimToEmpty(args["de"])
    if translationDe ~= "" then
        parts["d"] = '„' .. translationDe .. '“'
    end
    
    -- order parts
    
    local order = h.trimToEmpty(args["reihenfolge"]):lower() .. "uod"
    local ordered = {}
    local items = 0
    
    for i = 1, order:len() do
        local part = order:sub(i, i)
        if parts[part] then
            ordered[items] = parts[part]
            parts[part] = nil
            items = items + 1
        end
    end
    
    -- concatenate parts
    
    local out = ordered[0]
    
    if items > 1 then
        out = out .. " ("
        for i = 1, items - 1 do
            out = out .. ordered[i]
            if i < items - 1 then
                out = out .. ", "
            end
        end
        out = out .. ")"
    end

    return out
end

return p