FANDOM


---------------------------------------------------------------------
-- This module is used to provide translation information to
-- corresponding templates.
---------------------------------------------------------------------
 
-- Import translation table(s)
local dictionaries = {
    "Module:Sandbox/CXuesong/Translations/Dictionary",
}
 
-- Known languages (except for English)
local languages = {
    cz = "Czech",
    de = "German",
    es = "Spanish",
    fi = "Finnish",
    fr = "French",
    gr = "Greek",
    hu = "Hungarian",
    hr = "Croatian",
    it = "Italian",
    ja = "Japanese",
    ko = "Korean",
    lt = "Lithuanian",
    nl = "Dutch",
    no = "Norwegian",
    pl = "Polish",
    pt = "Portuguese (Brazilian)",
    ro = "Romanian",
    ru = "Russian",
    si = "Slovenian",
    sk = "Slovakian",
    tr = "Turkish",
    vi = "Vietnamese",
    zh_cn = "Chinese Simplified",
    zh_tw = "Chinese Traditional",
}
 
local ENGLISH_TAG = "en"
local ENGLISH = "English"
 
--------------------------------------
 
local builder = nil
 
function CollectTranslations()
    if builder ~= nil then return end
    --builder = require("TranslationBuilder")
    builder = require("Module:Sandbox/CXuesong/Translations/TranslationBuilder")
    for i, k in ipairs(dictionaries) do
        local d = require(k)
        d(builder)
    end
end
 
function orderedKeys(dict)
    local keys = { }
    local result = { }
    for k, v in pairs(dict) do
        table.insert(keys, k)
    end
    table.sort(keys)
    for i, k in ipairs(keys) do
        table.insert(result, k)
    end
    return result
end
 
function NZ(value, valueifNil)
    if value == nil then return valueifNil end
    return value
end 
 
function TableIsEmpty(table)
    for k, v in pairs(table) do
        return false
    end
    return true
end
 
-- http://lua-users.org/wiki/StringTrim
function trim12(s)
 local from = s:match"^%s*()"
 return from > #s and "" or s:match(".*%S", from)
end
 
-- The module
local p = { }
 
function p.getLanguageName(frame)
    return languages(frame.args[1])
end
 
-- Returns the translation of specific keyword (English)
-- with the provided language tag,
-- or nil, if no such translation is found.
function p.translate(frame)
    local term = frame.args[1]
    local language = frame.args[2]
    -- Bypass English.
    if language == ENGLISH_TAG then return key end
    local t = builder.getTranslation(key)
    if t == nil then return nil end
    return t[language]
end
 
-- Generates a full glossary 
function p.glossaryTable(frame)
    local showEmpty = frame.args["showEmpty"] ~= nil and frame.args["showEmpty"] ~= ""
    local colhdr = mw.html.create("table")
    :attr("class", "wikitable table-is-wide")
    -- Fix Wikia long table margin problem
    :css("float", "left")
    :css("margin-right", "-1px")
    local body = mw.html.create("div")
    :css("overflow-x", "auto")
    :css("white-space", "nowrap")
    :tag("table")
    :attr("class", "wikitable")
    :css("margin", "0")
    :tag("tr")
    colhdr:tag("tr"):tag("th"):wikitext(' ')
    -- First Row: language names
    local langs = orderedKeys(languages)
    body:tag("th"):wikitext(ENGLISH)
    for i, langTag in ipairs(langs) do
        body:tag("th"):wikitext(languages[langTag])
    end
    body = body:done();
    -- Other Rows
    for i, key in ipairs(builder.orderedTerms()) do
        local trans = builder.getTranslation(key)
        if showEmpty or not TableIsEmpty(trans) then
            colhdr:tag("tr"):tag("th"):wikitext(key)
            body = body:tag("tr")
            -- Keep another English column in the table body,
            -- for sake of mobile view users.
            body:tag("td"):wikitext(key)
            for i, langTag in ipairs(langs) do
                t = trans[langTag]
                if t == nil then t = "-" end
                body:tag("td"):wikitext(t)
            end
            body = body:done()
        end
    end
    body = body:allDone()
    local div = mw.html.create("div")
    :node(colhdr)
    :node(body)
    return tostring(div)
end
 
-- Generates a table of translations of one or more English terms.
-- Returns a single row (English) if no such term exists in the dictionary.
function p.translationTable(frame)
    local terms = {}
    local trans = {}
    local langs = {}    -- hashset
    for i, v in ipairs(frame.args) do
        --if v == nil then v = "" else v = trim12(v) end
        if v ~= "" then     -- Allows downstream templates to pass empty parameters.
            table.insert(terms, v)
            trans[v] = builder.getTranslation(v)
            if trans[v] ~= nil then
                for k, t in pairs(trans[v]) do
                    langs[k] = "Y"  -- just put an arbitary value here
                end
            end
        end
    end
    if TableIsEmpty(terms) then
        -- display a user-defined message if no terms specified
        return frame.args.fallback
    end
 
    langs = orderedKeys(langs)
 
    local body = mw.html.create("table")
                :attr("class", "wikitable")
    local tr = body:tag("tr")
    tr:tag("th"):wikitext(ENGLISH)
    for i, term in ipairs(terms) do
        tr:tag("th"):wikitext(term)
    end
    for i, key in pairs(langs) do
        local tr = body:tag("tr")
        tr:tag("th"):wikitext(NZ(languages[key], key))
        for j, term in ipairs(terms) do
            local termTrans = trans[term]
            if termTrans == nil then
                --termTrans = "N/A"   -- emits a different mark if the entire term doesn't exist
                termTrans = "-"
            else
                termTrans = termTrans[key]
            end
            tr:tag("td"):wikitext(NZ(termTrans, "-"))
        end
    end
    return tostring(body)
end
 
-- Gets a list of warning messages from TranslationBuilder
function p.warnings(frame)
    local w = builder.getWarnings()
    if TableIsEmpty(w) then return frame.args[1] end
    local m = ""
    for i, v in ipairs(w) do
        m = m..v.."\n"
    end
    return m
end
 
CollectTranslations()
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.