پرش به محتوا

ماژول:Wikidata2/monolingualtext

ویکی‌پدیا، آزادِ دانشنومه، جه

توضیحات این پودمان می‌تواند در ماژول:Wikidata2/monolingualtext/توضیحات قرار گیرد.

local p = {}
local lang_module = require("Module:Lang")

local config = {
    i18n = {
        no = "نا",
        local_lang_code = "mzn",
        local_lang_name = "مازِرونی",
    },
    unsupported_langs = { "mis", "mul", "zxx" }
}

local function is_valid(x)
    if x and x ~= nil and x ~= "" and x ~= config.i18n.no then return x end
    return nil
end

local function full_template(lang_code, text)
    local lang_name = lang_module._name_from_tag({ [1] = lang_code })
    if not lang_name or lang_name == lang_code then
        lang_name = lang_code
    end

    local article_name = lang_name .. "زوون "
    local display_text = lang_name .. "جه"
    local link = "[[" .. article_name .. "|" .. display_text .. "]]"

    local formatted_text = lang_module._lang({
        code = lang_code,
        text = text
    })

    return "(" .. link .. ": " .. formatted_text .. ")"
end

local function short_template(lang_code, text)
    return lang_module._lang({
        code = lang_code,
        text = text
    })
end

local function resolve_template_type(lang_code, text, options, is_same_lang)
    local text_format = is_valid(options.textformat) or is_valid(options.formatting)

    if (lang_code == options.langpref and text_format == "text") or is_same_lang then
        return text
    elseif is_valid(options.showlang) then
        return full_template(lang_code, text)
    end
    return short_template(lang_code, text)
end

function p._main(datavalue, datatype, options)
    local lang_code, text = datavalue.value.language, datavalue.value.text
    
    local lang_name = lang_module._name_from_tag({ [1] = lang_code })

    if table.concat(config.unsupported_langs, " "):find(lang_code) then
        return text
    end

    if is_valid(options.nolang) == lang_code then
        return ""
    end

    local is_same_lang = lang_name == config.i18n.local_lang_name or lang_code == config.i18n.local_lang_code

    if is_valid(options.langpref) then
        local result = ""
        local pref = options.langpref
        if pref == "justlang" then
            result = lang_name
        elseif pref == "langcode" then
            result = lang_code
        elseif lang_code == pref then
            result = resolve_template_type(lang_code, text, options, is_same_lang)
        end
        return result
    end

    return resolve_template_type(lang_code, text, options, is_same_lang)
end

function p.main(frame)
    local datavalue = {
        value = {
            language = frame.args.language,
            text = frame.args.text
        }
    }
    return p._main(datavalue, frame.args.datatype, frame.args)
end

return p