Ce module a pour but de fournir des fonctions pour écrire les modèles {{T}}
, {{trad}}
, {{trad+}}
et {{trad-}}
.
#invoke
et renvoie le nom de la langue correspondant au code langue fourni si elle existe, plus des catégories (traductions en cette langue, code langue manquant ou traduction à trier avec un second paramètre 2="trier").La documentation de ce module est générée par le modèle {{Documentation module}}.
Elle est incluse depuis la page Module:traduction/Documentation. Veuillez placer les catégories sur cette page-là.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local m_bases = require("Module:bases")
local m_params = require("Module:paramètres")
local m_langs = require("Module:langues")
local p = {}
local cats = {}
-- List of languages for which the "’" must be kept
local keepApos = { fr = true, de = true, }
-- Shows categories when true.
-- For debugging purposes, deactivate *before* saving.
local DEBUG = false
--- Returns the category with the given name.
--- @param categoryName string The category’s name.
--- @param sortingKey string The sorting key.
--- @param asLink boolean Whether the category should be a link or not.
--- @return string The category.
local function getCategory(categoryName, sortingKey, asLink)
if DEBUG then
if sortingKey then
return m_bases.fait_categorie(categoryName, nil, true) .. "(" .. sortingKey .. ")"
else
return m_bases.fait_categorie(categoryName, nil, true)
end
else
return m_bases.fait_categorie_contenu(categoryName, sortingKey, asLink)
end
end
--- Formats a language name.
--- @param langName string The language’s name.
--- @param langCode string The language’s code.
--- @return string The formated language name.
local function formatLanguageName(langName, langCode)
if langName and langCode then
return mw.ustring.format('<span class="trad-%s">%s</span>', langCode, langName)
else
return ""
end
end
--- Function for the template {{T}}.
--- frame.args (string): Language code.
--- frame.args (string): If equal to "trier", tells that this translation has to be sorted by users.
--- @return string Template’s code.
function p.templateT(frame)
local args = m_params.process(frame:getParent().args, {
= {},
= {},
})
local langCode = args
local sort = args == "trier"
-- Language code is mandatory.
if not langCode then
return "]"
.. getCategory("Wiktionnaire:Traductions T sans langue précisée")
end
local langName = m_langs.get_nom(langCode)
local formatedText
local category
if langName then
formatedText = m_bases.ucfirst(langName)
local categoryNamePrefix = sort and "Wiktionnaire:Traductions à trier en " or "Traductions en "
category = getCategory(categoryNamePrefix .. langName)
-- Undefined language code, categorize for easy checking.
else
formatedText = mw.ustring.format('<i><span style="color:red">%s</span></i>]', langCode)
category = getCategory("Wiktionnaire:Traductions T avec code langue non défini", langCode)
end
return formatLanguageName(formatedText, langCode) .. category
end
--- Generates the link pointing to another Wiktionary.
--- @param langCode string Word’s language code.
--- @param word string The word to link to.
--- @param status string The status (either "existe", "inconnu", "absent", "" or nil).
--- @return string The outgoing link.
local function generateOutgoingLink(langCode, word, status)
local displayedText = ""
-- Link destination
local wikiLangCode = m_langs.get_lien_Wikimedia(langCode) or langCode
if not keepApos then
word = mw.ustring.gsub(word, "", "'") -- apostrophes dactylographique et modificative
end
local destination = mw.ustring.format(":%s:%s", wikiLangCode, word)
if not m_langs.has_wiktionary(langCode) and langCode ~= 'conv' then
displayedText = '<span class="trad-nowikt">(*)</span>'
destination = "Wiktionnaire:Pas de wiktionnaire dans cette langue"
elseif status == "existe" then
displayedText = mw.ustring.format('<span class="trad-existe">(%s)</span>', wikiLangCode)
elseif status == "inconnu" then
displayedText = mw.ustring.format('<span class="trad-inconnu">(%s)</span>', wikiLangCode)
elseif status == "absent" then
displayedText = mw.ustring.format('<span class="trad-absent">(%s)</span>', wikiLangCode)
else
displayedText = ""
destination = nil
end
return destination
and mw.ustring.format('<span class="trad-exposant">]</span>', destination, displayedText)
or ""
end
--- Formats the given text as traditional script (mainly for Chinese).
--- @param langCode string The language code.
--- @param word string The word to format.
--- @return string The formated word.
local function formatTraditionalScript(langCode, word)
if not word then
return nil
end
-- Traditional Chinese is better displayed by browsers when using the code zh-Hant.
if langCode == "zh" then
langCode = "zh-Hant"
elseif langCode == "ko" then
langCode = "ko-Hani"
end
-- Mark the text as “traditional” for gadgets.
local shownText = mw.ustring.format('<span class="ecrit_tradi">%s</span>', word)
return m_bases.lien_modele(word, langCode, nil, shownText, true)
end
--- Formats a transcription.
--- @param langCode string Transcription’s language code.
--- @param transcription string Text to format.
--- @param traditionalLangCode string Optional language code for traditional scripts.
--- @return string The formated transcription.
local function formatTranscription(langCode, transcription, traditionalLangCode)
if not transcription or not langCode then
return nil
end
local lang = traditionalLangCode or langCode .. "-Latn"
return m_bases.balise_langue(transcription, lang)
end
--- Returns the full gender label for the given code.
--- @param gender string Gender code.
--- @return string The full gender label.
local function formatGender(gender)
if not gender then
return nil
end
-- List of authorized genders and associated numbers
local gendersList = {
= "masculin",
= "féminin",
= "neutre",
= "commun",
= "singulier",
= "pluriel",
= "duel",
= "animé",
= "inanimé",
= "masculin et féminin identiques",
= "masculin pluriel",
= "féminin pluriel",
= "masculin et féminin pluriel",
= "neutre pluriel",
= "masculin animé",
= "masculin inanimé",
= "féminin animé",
= "féminin inanimé",
= "neutre animé",
= "neutre inanimé",
}
if gendersList then
return mw.ustring.format("''%s''", gendersList)
else
table.insert(cats, getCategory("Wiktionnaire:Traductions avec genre inexistant", gender))
return ""
end
end
--- Generates the code for the templates {{trad}}, {{trad+}}, {{trad-}} and {{trad--}}.
--- @param status string Status of the interwiki link.
--- @param langCode string Translation’s language code.
--- @param word string The translation.
--- @param gender string Word’s gender.
--- @param alternativeText string Alternative text to display instead of the word.
--- @param transcription string Word’s transcription.
--- @param traditionalLangCode string Language code for traditional script if any (for Chinese and Korean).
--- @param traditionalTerm string Word for traditional script (for Chinese and Korean).
--- @return string Template’s code.
function p._templateTrad(status, langCode, word, gender, alternativeText, transcription, traditionalLangCode, traditionalTerm)
if not langCode then
table.insert(cats, getCategory("Wiktionnaire:Traductions sans langue précisée"))
return '<span style="color:red;">] (paramètre 1)</span>'
elseif m_langs.get_nom(langCode) == nil then
table.insert(cats, getCategory("Wiktionnaire:Traductions trad avec code langue non défini"))
end
local localLink = ""
local superscriptText = ""
if not word then
table.insert(cats, getCategory("Wiktionnaire:Traductions sans traduction précisée"))
localLink = '<span style="color:red">pas de traduction précisée (paramètre 2)</span>'
else
localLink = m_bases.lien_modele(word, langCode, nil, alternativeText, true)
superscriptText = generateOutgoingLink(langCode, word, status)
end
local formatedTraditionalScript = traditionalTerm and formatTraditionalScript(langCode, traditionalTerm) or nil
local formatedTranscription = transcription and formatTranscription(langCode, transcription, traditionalLangCode) or nil
local formatedGender = formatGender(gender)
local finalText = localLink
if superscriptText then
finalText = finalText .. " " .. superscriptText
end
if formatedTraditionalScript then
finalText = finalText .. " (" .. formatedTraditionalScript .. ")"
end
if formatedTranscription then
finalText = finalText .. " " .. formatedTranscription
end
if formatedGender then
finalText = finalText .. " " .. formatedGender
end
return finalText
end
--- Generates the code for the templates {{trad}}, {{trad+}}, {{trad-}} and {{trad--}}.
--- frame.args (string): Status of the interwiki link.
--- parent frame.args (string): Translation’s language code.
--- parent frame.args (string): The translation.
--- parent frame.args (string): Word’s gender.
--- parent frame.args (string): Alternative text to display instead of the word.
--- parent frame.args/parent frame.args (string): Word’s transcription.
--- parent frame.args/parent frame.args (string): Language code for traditional script if any (for Chinese and Korean).
--- parent frame.args (string): Word for traditional script (for Chinese and Korean).
--- @return string Template’s code.
function p.templateTrad(frame)
local status = m_params.process(frame.args, {
= { enum = { "nowikt", "existe", "inconnu", "absent", nil } },
})
local args = m_params.process(frame:getParent().args, {
= {},
= {},
= {},
= {},
= { alias_of = "lang-tr" },
= {},
= { alias_of = "tr" },
= {},
= {},
})
local langCode = args
local traditionalLangCode = args
local word = args
local alternativeText = args
local traditionalTerm = args
local transcription = args
local gender = args
local wikicode = p._templateTrad(status, langCode, word, gender, alternativeText, transcription, traditionalLangCode, traditionalTerm)
return wikicode .. table.concat(cats)
end
return p