Module:User:DTLHS/translations

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of DTLHS, for his own experimentation. Items in this module may be added and removed at DTLHS's discretion; do not rely on this module's stability.


local m_links = require("Module:links")
local languages = mw.loadData("Module:languages")

local export = {}

function build_translation(t_params, frame, langcode, langinfo)
    -- modified from Module:translations
    
    local interwiki = t_params["iw"] or ""
    if interwiki == "" or interwiki == "-" then
        interwiki = "tneg"
    elseif interwiki == "+" then
        interwiki = "tpos"
    end
    
    NAMESPACE = mw.title.getCurrentTitle().nsText
    local ret = ""
    local categories = {}
 
    local sc = t_params["sc"] or ""; if sc == "" then sc = langinfo.scripts[1] end
    
    local lang = langcode
    local term = t_params["t"] or (NAMESPACE == "Template" and "term") or error("Term has not been specified.")
    local alt = t_params["alt"]; if alt == "" then alt = nil end
 
    local tr = t_params["tr"]; if tr == "" then tr = nil end
    local qualifier = t_params["q"]; if qualifier == "" then qualifier = nil end
    if qualifier ~= nil then
        qualifier = frame:expandTemplate{ title = "qualifier", args = mw.text.split(qualifier, ",") }
    end
    
    -- Interwiki link
    local interwiki_langs = {
        ["nan"] = "zh-min-nan",
        ["yue"] = "zh-yue",
        ["cmn"] = "zh",
        ["nb"] = "no",
        ["rup"] = "roa-rup",
        ["kmr"] = "ku",
        ["nds-de"] = "nds",
        ["nds-nl"] = "nds",
        ["pdt"] = "nds" }
 
    -- Don't show the link if the language is not recognised by MediaWiki
    -- or if the term contains links (for SOP translations)
    if term:find("[[", nil, true) or not mw.language.isKnownLanguageTag(interwiki_langs[lang] or lang) then
        interwiki = false
    end
 
    -- If no transliteration was given, try auto-transliteration if available.
    -- This information should really be in Module:languages, but for now it works.
    local has_auto_translit = {
        ["ab"] = true,
        ["ady"] = true,
        ["axm"] = true,
        ["ba"] = true,
        ["bg"] = true,
        ["be"] = true,
        ["cv"] = true,
        ["hy"] = true,
        ["el"] = true,
        ["ka"] = true,
        ["kk"] = true,
        ["ky"] = true,
        ["mk"] = true,
        ["mn"] = true,
        ["os"] = true,
        ["ru"] = true,
        ["si"] = true,
        ["tg"] = true,
        ["tt"] = true,
        ["ug"] = true,
        ["uk"] = true,
        ["xcl"] = true }
    local needs_translit = {
        ["alr"] = true, 
        ["ab"] = true,
        ["abq"] = true,
        ["ady"] = true,
        ["agh"] = true,
        ["akv"] = true,
        ["am"] = true,
        ["ani"] = true, 
        ["aqc"] = true, 
        ["ar"] = true,
        ["as"] = true,
        ["av"] = true, 
        ["ba"] = true,
        ["bbl"] = true, 
        ["bdk"] = true, 
        ["be"] = true,
        ["bg"] = true,
        ["bn"] = true,
        ["bo"] = true,
        ["bph"] = true, 
        ["bxr"] = true, 
        ["ce"] = true, 
        ["cji"] = true, 
        ["ckt"] = true, 
        ["cv"] = true, 
        ["dar"] = true, 
        ["dlg"] = true, 
        ["dng"] = true, 
        ["dv"] = true,
        ["el"] = true,
        ["enf"] = true, 
        ["ess"] = true, 
        ["eve"] = true, 
        ["evn"] = true, 
        ["fa"] = true,
        ["gdo"] = true, 
        ["got"] = true,
        ["gu"] = true,
        ["he"] = true,
        ["hi"] = true,
        ["hy"] = true,
        ["inh"] = true, 
        ["itl"] = true, 
        ["ja"] = true,
        ["ka"] = true,
        ["kap"] = true, 
        ["kbd"] = true, 
        ["kca"] = true, 
        ["kjh"] = true, 
        ["kk"] = true,
        ["km"] = true,
        ["kn"] = true,
        ["ko"] = true,
        ["krc"] = true, 
        ["kv"] = true, 
        ["kva"] = true, 
        ["ky"] = true,
        ["lez"] = true, 
        ["lo"] = true,
        ["mdf"] = true, 
        ["mk"] = true,
        ["ml"] = true,
        ["mn"] = true,
        ["mr"] = true,
        ["my"] = true,
        ["myv"] = true, 
        ["ne"] = true,
        ["or"] = true,
        ["os"] = true,
        ["ps"] = true,
        ["ru"] = true,
        ["rue"] = true,
        ["sah"] = true, 
        ["si"] = true,
        ["ta"] = true,
        ["te"] = true,
        ["tg"] = true,
        ["th"] = true,
        ["ti"] = true,
        ["tt"] = true,
        ["tt"] = true,
        ["tyv"] = true, 
        ["udm"] = true, 
        ["ug"] = true,
        ["uk"] = true,
        ["ur"] = true,
        ["xal"] = true, 
        ["yi"] = true,
        ["yrk"] = true}
 
    if not tr and has_auto_translit[lang] then
        local translit_module = require("Module:" .. lang .. "-translit")
        tr = translit_module.tr(alt or term)
    end
 
    -- Gender and number
    -- Iterate over all numbered parameters starting from 3, until one is empty.
    local genders = {}
    local g = t_params["g"] or ""
    
    if g ~= "" then
        if mw.ustring.find(g, "?", nil, true) then
            table.insert(categories, langinfo.names[1] .. " terms with incomplete gender")
        end
        table.insert(genders, g)
    end
    
    -- Show the term
    ret = ret .. frame:expandTemplate{ title = sc, args = { m_links.language_link(term, alt, lang), lang = lang } }
 
    -- Show the interwiki link
    if interwiki then
        ret = ret .. "<span class=\"" .. interwiki .. "\">&nbsp;[[:" .. (interwiki_langs[lang] or lang) .. ":" .. term .. "|(" .. (interwiki_langs[lang] or lang) .. ")]]</span>"
    end
 
    -- Show transliteration
    if tr then
        ret = ret .. " (" .. tr .. ")"
    elseif needs_translit[lang] then
        table.insert(categories, langinfo.names[1] .. " translations lacking transliteration")
    end
 
    -- Show the genders
    if genders then
        local gen = require("Module:gender and number")
        ret = ret .. " " .. gen.format_list(genders)
    end
    
    -- Add qualifier
    if qualifier then
        ret = ret .. " " .. qualifier .. ""
    end
    
    -- Add the categories
    for key, cat in ipairs(categories) do
        ret = ret .. "[[Category:" .. cat .. "]]"
    end
 
    return ret
end

function export.show(frame)
    local args = frame:getParent().args
    
    local t_args = {}
    local langinfo_t = {}
    local langname_t = {}
    
    for i, v in pairs(args) do
        if type(i) ~= "number" then
            s = mw.text.split(i, '_')
            
            langcode = s[1]
            if langinfo_t[langcode] == nil then
                langinfo = languages[langcode] or error("The language code \"" .. lang .. "\" is not valid.")
                langinfo_t[langcode] = langinfo
                
            end
            langname = langinfo_t[langcode]["names"][1]
            langname_t[langname] = langcode
            
            t = s[2]
            n = s[3]
            
            if t_args[langname] == nil then
                t_args[langname] = {}
            end
            
            if t_args[langname][n] == nil then
                t_args[langname][n] = {}
            end

            t_args[langname][n][t] = v
            
        end
    end

    local result_t = {}
    
    -- nest t based on sc parameter
    local script_nesting = {
        ["sh"] = {["Cyrl"] = "Cyrillic", ["Latn"] = "Roman", ["Arab"] = "Arebica"},
        ["ku"] = {["Latn"] = "Kurmanji", ["ku-Arab"] = "Sorani"},
        ["arc"] = {["Hebr"] = "Hebrew", ["Syrc"] = "Syriac"},
        ["ks"] = {["ks-Arab"] = "Arabic", ["Deca"] = "Devanagari"},
        ["cu"] = {["Cyrl"] = "Cyrillic", ["Glag"] = "Glagolitic"},
        ["ro"] = {["Latn"] = "Latin", ["Cyrl"] = "Cyrillic"}
    }
    
    -- nest languages that have a distinct code
    local language_nesting = {
        ["grc"] = {"el"},
        ["gmy"] = {"el"},
        ["arq"] = {"ar", ["alt"] = "Algerian"},
        ["xaa"] = {"ar", ["alt"] = "Andalusian"},
        ["abv"] = {"ar", ["alt"] = "Bahrani"},
        ["shu"] = {"ar", ["alt"] = "Chadian"},
        ["arz"] = {"ar", ["alt"] = "Egyptian"},
        ["afb"] = {"ar", ["alt"] = "Gulf"},
        ["mey"] = {"ar", ["alt"] = "Hassānīya"},
        ["acm"] = {"ar", ["alt"] = "Iraqi"},
        ["apc"] = {"ar", ["alt"] = "Lebanese"},
        ["ayl"] = {"ar", ["alt"] = "Libyan"},
        ["ary"] = {"ar", ["alt"] = "Moroccan"},
        ["apd"] = {"ar", ["alt"] = "Sudanese"},
        ["aln"] = {"sq", ["alt"] = "Gheg"},
        ["bxr"] = {"bua", ["alt"] = "Buryar"},
        ["frm"] = {"fr"},
        ["fro"] = {"fr"},
        ["ksh"] = {"de", ["alt"] = "Kölsch"},
        ["gsw"] = {"de", ["alt"] = "Swiss German"},
        ["sgs"] = {"lt"},
        ["nds-de"] = {"nds"},
        ["nds-nl"] = {"nds"},
        ["cmg"] = {"mn"},
        ["nb"] = {"no"},
        ["nn"] = {"no"},
        ["sro"] = {"sc"},
        ["osp"] = {"es"}
    }
    
    -- nest under a macro language that has no code of its own
    local macro_nesting = {
        ["yue"] = "Chinese",
        ["dng"] = "Chinese",
        ["gan"] = "Chinese",
        ["hak"] = "Chinese",
        ["cmn"] = "Chinese",
        ["nan"] = "Chinese",
        ["cdo"] = "Chinese",
        ["wuu"] = "Chinese",
        ["umu"] = "Lenape",
        ["unm"] = "Lenape",
        ["dsb"] = "Sorbian",
        ["hsb"] = "Sorbian"
    }
    
    -- nest based on dialect parameter (where dialect has no distinct code)
    local dialect_nesting = {
        ["sq"] = {"Tosk"},
        ["cop"] = {"Bohairic", "Sahidic"},
        ["lt"] = {"Aukštaitian"},
        ["ms"] = {"Rumi", "Jawa"},
        ["sc"] = {"Nugorese"},
        ["tly"] = {"Asalemi"}
    }
    
    for langname, translation_data in pairs(t_args) do
        
        local langcode = langname_t[langname]
        
        if language_nesting[langcode] == nil then
            if result_t[langname] == nil then
                result_t[langname] = {[1] = "* " .. langname .. ":"}
            end
        end
        
        if script_nesting[langcode] == nil and language_nesting[langcode] == nil then
            for translation_number, t_params in pairs(translation_data) do
                result_t[langname][1] = result_t[langname][1] .. " " .. build_translation(t_params, frame, langcode, langinfo_t[langcode]) .. ","
            end
            result_t[langname][1] = string.sub(result_t[langname][1], 0, #result_t[langname][1] - 1)
        end
        if script_nesting[langcode] ~= nil then
            for scriptcode, scriptname in pairs(script_nesting[langcode]) do
                result_t[langname][scriptcode] = "*: " .. scriptname .. ":"
                for translation_number, t_params in pairs(translation_data) do
                    t_sc = t_params["sc"] or "Latn"
                    if t_sc == scriptcode then
                        result_t[langname][scriptcode] = result_t[langname][scriptcode] .. " " .. build_translation(t_params, frame, langcode, langinfo_t[langcode]) .. ","
                    end
                end
                result_t[langname][scriptcode] = string.sub(result_t[langname][scriptcode], 0, #result_t[langname][scriptcode] - 1)
            end
        end
        if language_nesting[langcode] ~= nil then
            parent_langcode = language_nesting[langcode][1]
            parent_langname = languages[parent_langcode]["names"][1]
            if result_t[parent_langname] == nil then
                result_t[parent_langname] = {[1] = "* " .. parent_langname .. ":"}
            end
            if language_nesting[langcode]["alt"] ~= nil then
                langname = language_nesting[langcode]["alt"]
            end
            result_t[parent_langname][2] = "*: " .. langname .. ":"
            for translation_number, t_params in pairs(translation_data) do
                result_t[parent_langname][2] = result_t[parent_langname][2] .. " " .. build_translation(t_params, frame, langcode, langinfo_t[langcode]) .. ","
            end
            result_t[parent_langname][2] = string.sub(result_t[parent_langname][2], 0, #result_t[parent_langname][2] - 1)
        end
    end
    
    result_t_keys = {}
    for i in pairs(result_t) do table.insert(result_t_keys, i) end
    
    table.sort(result_t_keys)
    
    local result = ""
    for i1, v1 in pairs(result_t_keys) do
        for i2, v2 in pairs(result_t[v1]) do
            result = result .. v2 .. "\n"
        end
    end
    
    return result
end

return export