Module:Sandbox/CXuesong/Translations

local translations = require("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"

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

-- 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 return translations[key][language] end

-- Generates a full glossary function p.glossaryTable local colhdr = mw.html.create("table") :attr("class", "wikitable") :css("float", "left") :css("margin", "0") :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(ENGLISH) local langs = orderedKeys(languages) for i, langTag in ipairs(langs) do       body:tag("th"):wikitext(languages[langTag]) end body = body:done; local transKeys = orderedKeys(translations) for i, key in ipairs(transKeys) do       colhdr:tag("tr"):tag("th"):wikitext(key) body = body:tag("tr") local trans = translations[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 body = body:allDone local div = mw.html.create("div") :node(colhdr) :node(body) return tostring(div) end

-- Generates a table of translations of a specific English term. -- Returns nil if no such term exists in the dictionary. function p.translationTable(frame) local term = frame.args[1] local trans = translations[term] if trans == nil then return nil end local body = mw.html.create("table") :attr("class", "wikitable") local transKeys = orderedKeys(trans) body:tag("tr") :tag("th") :wikitext(ENGLISH) :done :tag("th") :wikitext(term) :done for i, key in ipairs(transKeys) do       body:tag("tr") :tag("th") :wikitext(NZ(languages[key], key)) :done :tag("td") :wikitext(NZ(trans[key], "-")) :done end return tostring(body) end

return p