Jump to content

Module:Crh: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m removed unused error code
added Arabic script formatting
Line 79: Line 79:
table.insert(parts, cyrillicText)
table.insert(parts, cyrillicText)
end
end
if arabicText then
local formattedArabic = "<span lang=\"crh-Arab\" dir=\"rtl\">" .. arabicText .. "</span>"


if arabicText and not labelOnly and link then
if arabicText and not labelOnly and link then
table.insert(parts, "[[Arabic script|Arabic]]: " .. arabicText)
table.insert(parts, "[[Arabic script|Arabic]]: " .. formattedArabic)
elseif arabicText and not labelOnly and not link then
elseif arabicText and not labelOnly and not link then
table.insert(parts, "Arabic: " .. arabicText)
table.insert(parts, "Arabic: " .. formattedArabic)
elseif arabicText and labelOnly then
elseif arabicText and labelOnly then
table.insert(parts, arabicText)
table.insert(parts, formattedArabic)
end
end
end


if literalTranslation and link then
if literalTranslation and link then

Revision as of 23:34, 31 May 2024

require('strict')

local p = {}

-- Errors
local err_category = {
	['crh'] = '[[Category:crh template errors]]'
}

local function errorMessage(error)
	return '<span class="error" style="font-size:100%">Error in crh template: {{' .. error .. '}}</span>'
	end

-- Helper function to parse boolean parameters
local function parseBoolean(value, default)
    if value == nil then
        return default
    elseif type(value) == "string" then
        value = value:lower()
        if value == "yes" or value == "y" or value == "true" or value == "1" or value == "on" then
            return true
        elseif value == "no" or value == "n" or value == "false" or value == "0" or value == "off" then
            return false
        end
    end
    return default
end

-- Helper function to get parameter value, considering aliases
local function getParamValue(params, paramName, aliases)
    local value = params[paramName]
    if value == nil and aliases then
        for _, alias in ipairs(aliases) do
            value = params[alias]
            if value then break end
        end
    end
    return value
end

-- Main function to render text
function p.renderText(frame)
    local args = require ('Module:Arguments').getArgs (frame);
    
    local latinText = args[1]
    local cyrillicText = args[2]
    local arabicText = args[3]
    local literalTranslation = getParamValue(args, "lit", {"literal", "literally"})
    local link = parseBoolean(args["link"], true)
    local lead = parseBoolean(args["lead"], true)
    local labelOnly = parseBoolean(args["label-only"], true)
    
    if not latinText or latinText == "" then
        return errorMessage("Latin text required")
    end

    local output = {}
    local parts = {}

    if lead and link then
        table.insert(output, "[[Crimean Tatar language|Crimean Tatar]]: ")
    elseif lead and not link then
		table.insert(output, "Crimean Tatar: ")
    end

    if lead and not labelOnly and link then
        table.insert(parts, "[[Latin alphabet|Latin]]: ''" .. latinText .. "''")
    elseif lead and not labelOnly and link then
        table.insert(parts, "Latin: ''" .. latinText .. "''")
    else
        table.insert(parts, "''" .. latinText .. "''")
    end

    if cyrillicText and not labelOnly and link then
        table.insert(parts, "[[Cyrillic script|Cyrillic]]: " .. cyrillicText)
    elseif cyrillicText and not labelOnly and not link then
        table.insert(parts, "Cyrillic: " .. cyrillicText)
    elseif cyrillicText and labelOnly then
        table.insert(parts, cyrillicText)
    end
    
    if arabicText then
    	local formattedArabic = "<span lang=\"crh-Arab\" dir=\"rtl\">" .. arabicText .. "</span>"

	    if arabicText and not labelOnly and link then
	        table.insert(parts, "[[Arabic script|Arabic]]: " .. formattedArabic)
	    elseif arabicText and not labelOnly and not link then
	        table.insert(parts, "Arabic: " .. formattedArabic)
	    elseif arabicText and labelOnly then
	        table.insert(parts, formattedArabic)
	    end
	end

    if literalTranslation and link then
        table.insert(parts, "[[Literal translation|lit.]] '" .. literalTranslation .. "'")
    elseif literalTranslation and not link then
        table.insert(parts, "<abbr title=\"literal translation\">lit.</abbr> '" .. literalTranslation .. "'")
    end

    table.insert(output, table.concat(parts, ", "))

    return table.concat(output)
end

return p