Jump to content

Module:terjime

ئورنى Wiktionary

Documentation for this module may be created at Module:terjime/doc

b = require('Module:bases')
lang = require('Module:langues')

local p = {}

local cats = {}
-- Activer pour voir les catégories générées
-- Seulement pour déboguer avec l'aperçu : désactiver en sauvegardant
local isdebug = false

function _categorie_section(nom_cat, clef, ecrit)
    if isdebug then
    	if clef then
        	return b.fait_categorie(nom_cat, nil, true) .. '(' .. clef .. ')'
        else
        	return b.fait_categorie(nom_cat, nil, true)
        end
    else
        return b.fait_categorie_contenu(nom_cat, clef, ecrit)
    end
end

function _fait_span_trad(texte, code)
    if (texte ~= nil and code ~= nil) then
        return '<span class="terjime-' .. code .. '">' .. texte .. '</span>'
    else
        return ''
    end
end

-- Fonction pour écrire l'équivalent du modèle T
function p.T(frame)
    -- Récupération des variables nécessaires à la création du titre
    local args = frame:getParent().args
    args = b.trim_parametres(args)
    local code  = args[1]
    local trier = false
    if (args[2] == 'trier') then trier = true end
    local article = mw.title.getCurrentTitle()
    
    -- Pas de code de langue, indispensable
    if code == nil or code == '' then
    	table.insert(cats, _categorie_section('Wiktionary:تەرجىمە «ت» نىڭ تىل كودى يوق'))
        return '[[WT:تىللار تىزىملىگى|تىلى ئېنىقسىز]]' .. table.concat(cats)
    end
    
    -- A-t-on une langue correspondant au code ?
    local langue = lang.get_nom(code)
    local textet = {}
    
    -- Oui : écrit le texte
    if (langue ~= nil and langue ~= '') then
        table.insert(textet, b.ucfirst(langue))
        
        -- Catégories
        local newcattext = trier and 'چە تەرجىمە تەرتىۋى' or 'چە تەرجىمە'
        table.insert(cats, _categorie_section(langue .. newcattext ))
    
    -- Langue sans code! Catégorisée pour la retrouver
    else
        table.insert(textet, '<i><font color="red">' .. code .. '</font></i>[[WT:تىللار تىزىملىگى|*]]')
        
        table.insert(cats, _categorie_section('Wiktionary:تەرجىمە «ت» نىڭ تىل كودى تېخى ئېنىقلانمىغان', code))
    end
    
    texte = _fait_span_trad(table.concat(textet), code)
    
    return texte .. table.concat(cats)
end

---------------------------------------------------------------------------------------------------------
-- Fonctions pour créer les éléments de trad_defaut
function _fait_lien_local(code_langue, mot, dif)
    local texte_lien
    if dif ~= nil and dif ~= '' then
        texte_lien = dif
    else
        texte_lien = mot
    end
    
    local texte_final =  '[[' .. mot .. '#' .. code_langue .. '|' .. '<span lang="' .. code_langue .. '">' .. texte_lien .. '</span>' .. ']]'
    return texte_final
    -- Manque dif, et un <span lang="">, à faire par une fonction de base
end

function _fait_asterisque(code_langue, mot, statut)
    -- Dessine l'astérisque selon le statut de l'article dans le Wiktionnaire correspondant
    local asterisque = ''
    
    -- Destination du lien
    -- Corriger le lien pour les wikis spéciaux
    local code_langue_wikt =  lang.get_lien_Wikimedia(code_langue) or code_langue
    local destination = ':' .. code_langue_wikt .. ':' .. mot
    
    -- Pas de statut, ou statut du Wiki inconnu ? Ne rien afficher
    if (statut == '' or statut == nil) then
        asterisque = ''
        destination = nil
        
    -- Pas de Wiki ?
    elseif (statut == 'nowikt') then
        asterisque = '<span class="terjime-nowikt">&rlm;(*)</span>'
        destination = 'Wiktionary:بۇ تىلدىكى ۋىكىلۇغەت مەۋجۇت ئەمەس'
    elseif (statut == 'existe') then
        asterisque = '<span class="terjime-existe">&rlm;(' .. code_langue_wikt .. ')</span>'
    elseif (statut == 'inconnu') then
        asterisque = '<span class="terjime-inconnu">&rlm;(' ..code_langue_wikt .. ')</span>'
    elseif (statut == 'absent') then
        asterisque = '<span class="terjime-absent">&rlm;(' .. code_langue_wikt .. ')</span>'
    end
    
    local texte_final = destination and '<span class="terjime-exposant">[[' .. destination .. '|' ..  asterisque .. ']]</span>' or ''
    return texte_final
end

function _fait_ecrit_traditionnel(code_langue, tradi)
    if (tradi == nil or tradi == '') then return nil end
    
    local destination = tradi .. '#' .. code_langue
    local affichage = tradi
    -- Manque un <span lang="">, à faire par une fonction de base
    local texte_final = '[[' .. destination .. '|' .. affichage .. ']]'
    return texte_final
end

function _fait_transcrit(code_langue, transc, code_lang_R)
    if (transc == nil or transc == '' or code_langue == nil) then return nil end
    
    local lang = code_lang_R or code_langue .. '-Latn'
    local texte_final = '<span lang="' .. lang .. '">' .. transc .. '</span>'
    return texte_final
end

-- Genre : seulement les genres définis (sinon on invoque n'importe quel modèle, voire un modèle inexistant)
function _fait_genre(genre)
	if genre == nil then return nil end
	
	-- Liste des genres autorisés
	local listeg = {}
	listeg['m'] = 'ئەرچە'
	listeg['f'] = 'ئايالچە'
	listeg['n'] = 'ئارا'
	listeg['c'] = 'ئورتاق'
	listeg['s'] = 'بىرلىك'
	listeg['p'] = 'كۆپلۈك'
	listeg['mf'] = 'ئەر ئايالچىسى ئوخشاش'
	listeg['ma'] = 'masculin animé'
	listeg['mi'] = 'masculin inanimé'
	listeg['fa'] = 'féminin animé'
	listeg['fi'] = 'féminin inanimé'
	
	if listeg[genre] then
		return "''" .. listeg[genre] .. "''"
	else
		table.insert(cats, _categorie_section("Wiktionary:تەرجىمىنىڭ خاراكتېرى مەۋجۇت ئەمەس", genre))
		return ''
	end
end

-- Fonction pour écrire l'équivalent de trad/défaut
function p.trad_defaut(frame)
    -- Récupération des variables
    local args = frame.args
    local argsnum = b.trim_parametres(args) -- trim les paramètres numériques
    
    local code_langue = argsnum[1]                  -- Code langue
    local code_lang_s = args['lang-s']              -- Code langue plus précis (rare) PAS UTILISÉ ?
    local code_lang_R = args['lang-R'] or args['lang-tr']    -- Code langue du terme traditionnel (chinois et coréen)
    
    local mot = argsnum[2]                          -- Le terme traduit
    local dif = args['dif']                         -- Affichage alternatif du terme traduit (rare)
    local tradi = args['tradi']                     -- Terme traditionnel (chinois et coréen)
    local transc = args['R'] or args['tr']          -- Transcription du terme traduit
    
    local statut = args['statut']                   -- Remplace "color", voir les statuts possibles dans _fait_asterisque
    local nocat = (args['nocat'] and true or false) -- Pas catégorisé (vraiment utile ?)
    local genre = argsnum[3]
    local erreur = argsnum[4]

    -- Vérification des paramètres
    if code_langue == nil or code_langue == '' then
        table.insert(cats, _categorie_section("Wiktionary:تەرجىمنىڭ تىل كودى ئېنىقسىز")) --Traductions sans code langue précisé
        return '[[WT:تىللار تىزىملىگى|تىلى ئېنىقسىز]] (پارامېتىر 1)'
    elseif lang.get_nom(code_langue) == nil then
        table.insert(cats, _categorie_section('Wiktionary:تەرجىمە «تەرجىمە» نىڭ تىل كودى ئېنىقسىز', code)) --Traduction trad avec code langue non défini
    end
    
    -- Mot par défaut = titre de l'article
    if mot == nil or mot =='' then
        local article = mw.title.getCurrentTitle()
        table.insert(cats, _categorie_section("Wiktionary:تەرجىمىسى ئېنىقسىز")) --Traductions sans traduction précisée
        mot = article.text
    end
    
    -- Préparation du texte à afficher
    local lien_local = _fait_lien_local(code_langue, mot, dif)
    local asterisque = _fait_asterisque(code_langue, mot, statut)
    local ecrit_traditionnel = tradi and _fait_ecrit_traditionnel(code_langue, tradi) or nil
    local transcrit = transc and _fait_transcrit(code_langue, transc, code_lang_R) or nil
    local texte_genre = _fait_genre(genre)
    
    -- Création du texte à afficher
    local textet_final = {lien_local}
    if (asterisque) then table.insert(textet_final, ' ' .. asterisque) end
    if (ecrit_traditionnel) then table.insert(textet_final, ' (' .. ecrit_traditionnel .. ')') end
    if (transcrit) then table.insert(textet_final, ' (' .. transcrit .. ')') end
    if (texte_genre) then table.insert(textet_final, ' ' .. texte_genre) end
    if erreur ~= nil then table.insert(cats, _categorie_section("Wiktionary:تەرجىمە پارامېتىرلىكى بەك كۆپ", erreur)) end
    return table.concat(textet_final)
end

-- Interface à utiliser dans les modèles trad, trad+, trad-, trad--
function p.trad(frame)
    -- Récupère les arguments du modèle
    local par_model = frame:getParent()
    
    -- Ajoute le statut
    par_model.args['statut'] = frame.args['statut']
    return p.trad_defaut(par_model) .. table.concat(cats)
end

return p