Module:translations: difference between revisions

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
m
m
Line 209: Line 209:
 
 
 
-- Show the genders
 
-- Show the genders
if genders ~= {} then
+
if #genders > 0 then
 
local gen = require("Module:gender and number")
 
local gen = require("Module:gender and number")
 
ret = ret .. " " .. gen.format_list(genders)
 
ret = ret .. " " .. gen.format_list(genders)

Revision as of 12:45, 12 August 2013

This module is used for the translation templates {{t}} and {{t+}}. It contains a single function which is invoked by these templates. See their documentation for more information.
local m_languages = mw.loadData("Module:languages")
local m_scriptutils = require("Module:script utilities")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
 
local export = {}
 
-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
    local args = frame:getParent().args
    local interwiki = frame.args["interwiki"]
 
    NAMESPACE = mw.title.getCurrentTitle().nsText
    local categories = {}
 
    local lang = args[1] or (NAMESPACE == "Template" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
    local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    local sc = args["sc"] or ""; if sc == "" then sc = langinfo.scripts[1] end
 
    local term = args[2] or (NAMESPACE == "Template" and "term") or error("Term has not been specified. Please pass parameter 2 to the template.")
    local alt = args["alt"]; if alt == "" then alt = nil end
 
    local tr = args["tr"]; if tr == "" then tr = nil 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 interwiki 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
 
    -- Try to detect the script if it was not provided
    if not sc then
        -- Does this language have more than one script?
        -- If not, we can bypass the detection for a speed bonus.
        if langinfo.scripts[2] then
            sc = m_utilities.detect_script(alt or term, lang) or langinfo.scripts[1]
        else
            sc = langinfo.scripts[1]
        end
    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(m_links.remove_links(alt or term))
    end
 
    -- Gender and number
    -- Iterate over all numbered parameters starting from 3, until one is empty.
    local genders = {}
    local i = 3
    local g = args[i] or ""
 
    while g ~= "" do
        if mw.ustring.find(g, "?", nil, true) then
            table.insert(categories, langinfo.names[1] .. " terms with incomplete gender")
        end
 
        table.insert(genders, g)
        i = i + 1
        g = args[i] or ""
    end
 
    -- Show the term
    local ret = m_scriptutils.tag_text(m_links.language_link(term, alt, lang), lang, sc)
 
    -- Show the interwiki link
    if interwiki then
        ret = ret .. "<span class=\"" .. interwiki .. "\">&nbsp;[[:"
        .. (interwiki_langs[lang] or lang) .. ":"
        .. m_links.prepare_title(term, lang)
        .. "|(" .. (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 > 0 then
        local gen = require("Module:gender and number")
        ret = ret .. "&nbsp;" .. gen.format_list(genders)
    end
 
    -- Add the categories
    for key, cat in ipairs(categories) do
        ret = ret .. "[[Category:" .. cat .. "]]"
    end
 
    return ret
end
 
return export