Bienvenue sur le wiki de Nuit debout, nous sommes le 3002 mars.
Modification de Module:Langue
Attention : Vous n’êtes pas connecté. Votre adresse IP sera visible de tout le monde si vous faites des modifications. Si vous vous connectez ou créez un compte, vos modifications seront attribuées à votre nom d’utilisateur, entre autres avantages.
Cette modification va être défaite. Veuillez vérifier les modifications ci-dessous, puis publier si c’est bien ce que vous voulez faire.
Version actuelle | Votre texte | ||
Ligne 1 : | Ligne 1 : | ||
local Langue = { } | local Langue = { } | ||
− | -- Chargement de la base de | + | -- Chargement du de la base de donnée des langues avec gestion d'erreur. |
local dataLangue | local dataLangue | ||
− | + | local success, resultat = pcall (mw.loadData, 'Module:Langue/Data' ) | |
− | + | ||
− | local success, resultat = pcall (mw.loadData, | + | |
− | + | ||
if success then | if success then | ||
− | + | dataLangue = resultat | |
else | else | ||
− | + | -- Base de donnée à minima en cas de bug sur le Module:Langue/Data | |
− | + | dataLangue = { en = { code = 'en', nom = anglais }, | |
− | + | de = { code = 'de', nom = 'allemand' }, | |
− | + | it = { code = 'it', nom = 'italien' }, | |
− | + | es = { code = 'es', nom = 'espagnol' }, } | |
− | + | dataLangue.anglais = dataLangue.en | |
− | + | dataLangue.allemand = dataLangue.de | |
− | + | dataLangue.italien = dataLangue.it | |
− | + | dataLangue.espagnol = dataLangue.es | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
end | end | ||
− | -- premierParametre renvoie le premier paramètre de Frame, que celui-ci ait été | + | -- premierParametre renvoie le premier paramètre de Frame, que celui-ci ait été au module par invoke, directement au modèle, |
-- ou à l'intérieur d'un module sous forme de string dans un tableau ou directement de string. | -- ou à l'intérieur d'un module sous forme de string dans un tableau ou directement de string. | ||
− | -- Si aucun de ces | + | -- Si aucun de ces argument ne contient de chaine, la fonction renvoie nil. |
− | -- Si le deuxième paramètre est true, la chaine est renvoyée trimée et en | + | -- Si le deuxième paramètre est true, la chaine est renvoyée trimée et en minuscule. |
local function premierParametre( frame, lowerCase ) | local function premierParametre( frame, lowerCase ) | ||
− | + | local arg | |
− | + | if type( frame ) == 'table' then | |
− | + | arg = ( frame.getParent and ( frame.args[1] or frame:getParent().args[1] ) ) or frame[1] | |
− | + | elseif type( frame ) == 'string' then | |
− | + | arg = frame | |
− | + | end | |
− | + | if type( arg ) ~= 'string' then | |
− | + | arg = nil | |
− | + | end | |
− | + | if arg and lowerCase then | |
− | + | arg = mw.ustring.lower( mw.text.trim ( arg ) ) | |
− | + | end | |
− | + | return arg | |
end | end | ||
− | -- | + | -- Voir Modèle:Direction langue |
− | -- | + | -- Paramètre : |
− | function Langue. | + | -- 1 : nom de langue ou code IETF. |
− | + | function Langue.directionLangue( frame ) | |
− | + | local arg = premierParametre( frame, true ) | |
− | + | if type( arg ) ~= 'string' or arg == '' then | |
− | + | return 'ltr' | |
− | + | end | |
− | + | local tabLangue = dataLangue[ arg ] | |
− | + | if tabLangue and tabLangue.rtl then | |
− | + | return 'rtl' | |
− | + | elseif tabLangue == nil then | |
− | + | -- si la langue n'es pas directement dans la base, on essai avec les subtag | |
− | + | local codeMain, codeSub = mw.ustring.match( arg, '^(%a-)%-(.+)$' ) | |
+ | if codeSub then | ||
+ | if string.len(codeSub) == 4 and dataLangue[ 'rtl script' ] then | ||
+ | -- codeSub est un code de script, on compare au script rtl | ||
+ | codeSub = string.upper( string.sub( codeSub, 1, 1 ) ) .. string.sub( codeSub, 2 ) | ||
+ | if dataLangue[ 'rtl script' ][ codeSub ] then | ||
+ | return 'rtl' | ||
+ | end | ||
+ | else | ||
+ | -- sinon on utilise le code principal et on teste s'il est présent dans la base. | ||
+ | tabLangue = dataLangue[ codeMain ] | ||
+ | if tabLangue and tabLangue.rtl then | ||
+ | return 'rtl' | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | return 'ltr' | ||
end | end | ||
-- Voir Modèle:Code langue | -- Voir Modèle:Code langue | ||
− | -- Paramètre : | + | -- Paramètre : |
-- 1 : nom de langue. | -- 1 : nom de langue. | ||
function Langue.codeLangue( frame ) | function Langue.codeLangue( frame ) | ||
− | + | local arg = premierParametre( frame, true ) | |
− | + | local tabLangue = dataLangue[ arg ] | |
− | + | return (tabLangue and tabLangue.code) or arg or '' | |
end | end | ||
-- Voir Modèle:Code langue 2 | -- Voir Modèle:Code langue 2 | ||
− | -- Paramètre : | + | -- Paramètre : |
-- 1 : nom de langue. | -- 1 : nom de langue. | ||
function Langue.codeLangue2( frame ) | function Langue.codeLangue2( frame ) | ||
− | + | local arg = premierParametre( frame, true ) | |
− | + | local tabLangue = dataLangue[ arg ] | |
− | + | return (tabLangue and tabLangue.code) or '' | |
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
-- Voir Modèle:Nom langue | -- Voir Modèle:Nom langue | ||
− | -- Paramètre : | + | -- Paramètre : |
-- 1 : code IETF de langue. | -- 1 : code IETF de langue. | ||
function Langue.nomLangue( frame ) | function Langue.nomLangue( frame ) | ||
− | + | local arg = premierParametre( frame, true ) | |
− | + | if type( arg ) ~= 'string' or arg == '' then | |
− | + | return '<span style="color:red">langue non précisée</span>' | |
− | + | end | |
− | + | local tabLangue = dataLangue[ arg ] | |
− | + | if tabLangue == nil then | |
− | + | tabLangue = dataLangue[ mw.ustring.match( arg, '^(%a-)%-' ) ] | |
− | + | end | |
− | + | if tabLangue then | |
− | + | local nom | |
− | + | if type( tabLangue.page ) == 'string' then | |
− | + | nom = tabLangue.page .. '|' .. tabLangue.nom | |
− | + | else | |
− | + | nom = tabLangue.nom | |
− | + | end | |
− | + | return '[[' .. nom .. ']]' | |
− | + | else | |
− | + | return '<span style="color:red;">langue non reconnue : ' .. arg .. ' </span>' | |
− | + | end | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
end | end | ||
− | + | -- Voir Modèle:Lang | |
− | -- Voir Modèle: | + | |
-- Paramètres : | -- Paramètres : | ||
− | -- 1 : code IETF de langue | + | -- 1 : code IETF de langue, |
− | -- texte ou 2 : texte dans cette langue | + | -- texte ou 2 : texte dans cette langue, |
− | -- trans : | + | -- trans : translitération du texte, |
-- dir : direction de la langue (obsolète : peut être en paramètre 1, avec code en 2 et texte en 3). | -- dir : direction de la langue (obsolète : peut être en paramètre 1, avec code en 2 et texte en 3). | ||
− | function Langue. | + | function Langue.lang( frame ) |
− | + | local args = ( frame.getParent and frame:getParent().args ) or frame or { } -- préparation pour appel par modèle ou direct. | |
− | + | local code = mw.ustring.lower( mw.text.trim( args[1] or '' ) ) | |
− | + | local texte = args.texte or '' | |
− | + | if texte == '' then | |
− | + | texte = args[2] or '' | |
− | + | end | |
− | + | local dir = args.dir | |
− | + | local trans = args.trans | |
− | + | -- Décalage des paramètres si code contient la direction du texte (obsolete mais toujours possible). | |
− | + | if code == 'ltr' or code == 'rtl' then | |
− | + | dir = code | |
− | + | code = mw.ustring.lower( mw.text.trim( args[2] ) ) | |
− | + | texte = args[3] or '' | |
− | + | end | |
− | + | ||
− | + | -- Si la langue est reconnue, la valeur de la table est prise en compte (permet de corrigé les noms de langue en toute lettre). | |
− | + | local tabLangue = dataLangue[ code ] | |
− | + | if tabLangue and tabLangue.code then | |
− | + | code = tabLangue.code | |
− | + | else | |
− | + | -- Si elle n'est pas reconnue, on essaye uniquement le code principal. | |
− | + | local codeMain, codeSub = mw.ustring.match( code, '^(%a-)%-(.+)$' ) | |
− | + | tabLangue = dataLangue[ codeMain ] | |
− | + | if tabLangue and tabLangue.code then | |
− | + | -- Si le subcode est un style d'écriture (len = 4) on applique sa direction | |
− | + | if string.len(codeSub) == 4 and dataLangue[ 'rtl script' ] then | |
− | + | codeSub = string.upper( string.sub( codeSub, 1, 1 ) ) .. string.sub( codeSub, 2 ) | |
− | + | tabLangue = { code = tabLangue.code, | |
− | + | rtl = dataLangue[ 'rtl script' ][ codeSub ], | |
− | + | invalide = tabLangue.invalide | |
− | + | } | |
− | + | end | |
− | + | code = tabLangue.code .. '-' .. codeSub | |
− | + | end | |
− | + | end | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | -- Prépatation du rendu de direction du texte. | |
− | + | dir = mw.ustring.lower(dir or '') | |
− | + | if dir == 'ltr' or dir == 'rtl' then | |
− | + | dir = ' dir=' .. dir | |
− | + | else | |
− | + | dir = (tabLangue and tabLangue.rtl and ' dir=rtl') or '' | |
− | + | end | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | -- Transliteration. | |
− | + | if trans and trans ~= '' then | |
− | + | trans = ' (<span class="lang-' .. code .. ' transcription" lang="' .. code .. '-Latn"' | |
− | + | .. ' dir="ltr">' .. trans .. '</span>)' | |
− | + | else | |
− | + | trans = '' | |
− | + | end | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | -- Compilation du texte à retourner. | |
− | + | local wikiText = '' | |
− | + | if code == '' then | |
− | + | wikiText = texte | |
− | + | else | |
− | + | wikiText = '<span class="lang-' .. code .. '" lang="' .. code .. '"' | |
− | + | .. dir .. '>' .. texte .. '</span>' .. trans | |
− | + | end | |
− | + | ||
− | + | ||
− | + | ||
− | + | -- Ajout de la catégorie Page avec code de langue invalide si le code langue non reconnu ou invalide. | |
+ | if success and ( type( tabLangue ) ~= 'table' or tabLangue.invalide ) then | ||
+ | local namespaceCategorisation = { [0] = true, [4] = true, [10] = true, [14] = true, [100] = true } | ||
+ | if namespaceCategorisation[ mw.title.getCurrentTitle().namespace ] then | ||
+ | wikiText = wikiText .. '[[Catégorie:Page avec code de langue invalide|' .. code .. ']]' | ||
+ | end | ||
+ | end | ||
+ | |||
+ | return wikiText | ||
end | end | ||
− | -- | + | -- Voir Modèle:Langue |
− | + | -- Paramètres : | |
− | + | -- 1 : nom de langue ou code IETF, | |
+ | -- texte ou 2 : texte dans cette langue, | ||
+ | -- trans : translitération du texte, | ||
+ | -- dir : direction de la langue. | ||
+ | function Langue.langue( frame ) | ||
+ | return Langue.lang( frame ) | ||
+ | end | ||
-- Voir Modèle:Indication de langue | -- Voir Modèle:Indication de langue | ||
-- Paramètres : | -- Paramètres : | ||
− | -- 1 : nom de langue | + | -- 1 : nom de langue, |
− | -- 2 : code IETF | + | -- 2 : code IETF, |
− | -- texte : texte dans cette langue | + | -- texte : texte dans cette langue, |
-- dir : direction de la langue. | -- dir : direction de la langue. | ||
function Langue.indicationDeLangue( frame ) | function Langue.indicationDeLangue( frame ) | ||
− | + | local args = ( frame.getParent and frame:getParent().args ) or frame or { } | |
− | + | local nomLangue = args[1] or '' | |
− | + | local code = args.langue or mw.text.trim( args[2] or '' ) | |
− | + | local texte = args.texte | |
− | + | local dir = args.dir | |
− | + | local wikiText = '' | |
− | + | -- Cas ou le premier et/ou le deuxième paramètre est vide. | |
− | + | if code .. nomLangue == '' then | |
− | + | return texte | |
− | + | elseif nomLangue == '' then | |
− | + | nomLangue = dataLangue[ mw.ustring.lower( code ) ] | |
− | + | nomLangue = (nomLangue and nomLangue.nom or '???') | |
− | + | elseif code == '' then | |
− | + | code = dataLangue[ nomLangue ] | |
− | + | code = ( code and code.code or '' ) | |
− | + | if code == '' then | |
− | + | return texte | |
− | + | end | |
− | + | end | |
− | + | -- Gestion du texte. | |
− | + | if texte and texte ~= '' then | |
− | + | texte = ' ' .. Langue.lang{ code, dir = dir, texte = texte } | |
− | + | else | |
− | + | texte = '' | |
− | + | end | |
− | + | -- Compilation de l'indicateur de langue et du texte. | |
− | + | wikiText = '<span class="indicateur-langue">(<abbr class="abbr" title="Langue : ' | |
− | + | .. nomLangue .. '">' | |
− | + | .. code .. '</abbr>)</span>' | |
− | + | .. texte | |
− | + | return wikiText | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
end | end | ||
− | -- Voir Modèle: | + | -- Voir Modèle:Multiling |
− | -- Paramètres : codes IETF ou noms de langue, en nombre indéfini (string ou nil uniquement) | + | -- Paramètres : codes IETF ou noms de langue, en nombre indéfini. (string ou nil uniquement) |
function Langue.indicationMultilingue( frame ) | function Langue.indicationMultilingue( frame ) | ||
− | + | local args = (frame.getParent and frame:getParent().args) or frame | |
− | + | local listeNom = { } | |
− | + | local listeCode = { } | |
− | + | local tabLangue | |
− | + | -- Valeur par défaut du premier paramètre = 'mul'. | |
− | + | code = mw.ustring.lower( mw.text.trim( args[1] or '' ) ) | |
− | + | if code == '' then | |
− | + | code = 'mul' | |
− | + | end | |
− | + | -- Ajout des noms et codes de langue de chaque paramètre dans listeNom et ListeCode. | |
− | + | local i = 1 | |
− | + | repeat | |
− | + | tabLangue = dataLangue[ code ] | |
− | + | if type( tabLangue ) == 'table' then | |
− | + | table.insert( listeNom, tabLangue.nom ) | |
− | + | table.insert( listeCode, tabLangue.code ) | |
− | + | else | |
− | + | table.insert( listeNom, '???' ) | |
− | + | local erreur = ' <span style="color:red; font-size:120%;">langue non reconnue : ' | |
− | + | .. code .. ' </span>' | |
− | + | table.insert( listeCode, erreur ) | |
− | + | end | |
− | + | i = i+1 | |
− | + | code = mw.ustring.lower( mw.text.trim( args[i] or '' ) ) | |
− | + | until code == '' | |
− | + | -- Préparation et renvoie du texte. | |
− | + | local n = #listeCode | |
− | + | local pluriel = '' | |
− | + | if n == 0 then | |
− | + | return '' | |
− | + | elseif n > 1 then | |
− | + | pluriel = 's' | |
− | + | end | |
− | + | local wikiText = '<span class="indicateur-langue">(<abbr class="abbr" title="Langue' .. pluriel .. ' : ' | |
− | + | .. mw.text.listToText( listeNom ) .. '">' | |
− | + | .. table.concat( listeCode, '+' ) .. '</abbr>)</span>' | |
− | + | return wikiText | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
end | end | ||
− | |||
-- Voir Modèle:Langue avec nom | -- Voir Modèle:Langue avec nom | ||
-- Paramètres : | -- Paramètres : | ||
− | -- 1 : code IETF de langue | + | -- 1 : code IETF de langue, |
− | -- texte ou 2 : texte dans cette langue | + | -- texte ou 2 : texte dans cette langue, |
− | -- trans : | + | -- trans : translitération du texte, |
-- dir : direction de la langue. | -- dir : direction de la langue. | ||
function Langue.langueAvecNom( frame ) | function Langue.langueAvecNom( frame ) | ||
− | + | local args = ( frame.getParent and frame:getParent().args ) or frame or { } | |
− | + | local code = mw.ustring.lower( mw.text.trim( args [1] or '') ) | |
− | + | local texte = args.texte or args[2] or '' | |
− | + | local trans = args.trans | |
− | + | local dir = args.dir | |
− | + | local wikiText = '' | |
− | + | -- Détection de la direction du texte. | |
− | + | if code == 'ltr' or code == 'rtl' then | |
− | + | dir = code | |
− | + | code = mw.ustring.lower( mw.text.trim( args[2] ) ) | |
− | + | texte = args[3] or '' | |
− | + | end | |
− | + | -- Définition du nom de la langue en français. | |
− | + | local nom = Langue.nomLangue{ code } | |
− | + | if texte ~= '' then | |
− | + | texte = ' ' .. Langue.lang{ code, dir = dir, texte = texte, trans = trans } | |
− | + | end | |
− | + | wikiText = nom .. ' :' .. texte | |
− | + | return wikiText | |
end | end | ||
− | + | -- erreurModuleData affiche d'un message d'erreur si le Module:Langue/Data n'a pas été chargé correctement, | |
− | + | -- pour la page de discussion de la base de donnée et ceux qui veulent surveiller cette page. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | -- erreurModuleData affiche un message d'erreur si le Module:Langue/Data n'a pas été chargé correctement, | + | |
− | -- pour la page de discussion de la base de | + | |
function Langue.erreurModuleData() | function Langue.erreurModuleData() | ||
− | + | if success == false then | |
− | + | local message = [[<strong class="error">Le chargement du module Langue/Data génère une erreur : </strong><br />%s<br /> | |
− | <span class="error">Cette erreur doit être corrigée au plus vite car des milliers de | + | <span class="error">Cette erreur doit être corrigée au plus vite car des milliers de page ne s'affichent pas correctement</span> |
]] | ]] | ||
− | + | return string.format( message, resultat ) | |
− | + | end | |
end | end | ||
− | -- tableauLangues génère un tableau triable de la liste des langues disponible dans Module:langue/Data. | + | local dataLangue = mw.loadData( 'Module:langue/Data' ) |
+ | |||
+ | --tableauLangues génère un tableau triable de la liste des langues disponible dans Module:langue/Data. | ||
function Langue.tableauLangues() | function Langue.tableauLangues() | ||
− | + | if type( dataLangue ) ~= 'table' then | |
− | + | return | |
− | + | end | |
− | + | local tableau = { } | |
− | + | local entete = [[{| class="wikitable alternance sortable" | |
|- | |- | ||
!scope=col|Alias | !scope=col|Alias | ||
− | !scope=col| | + | !scope=col|code IETF |
!scope=col|Nom principal | !scope=col|Nom principal | ||
− | !scope=col|Page | + | !scope=col|Page |
!scope=col|RTL | !scope=col|RTL | ||
− | !scope=col| | + | !scope=col|invalide]] |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | local lignetab, lignesrt | |
− | + | for i, v in pairs( dataLangue ) do | |
− | + | if v.code then | |
− | + | lignetab = { | |
− | + | i, | |
− | + | v.code, | |
− | + | v.nom, | |
− | + | v.page or '', | |
− | + | v.rtl and '[[Image:Yes check.svg|15px|oui|lien=]]' or '', | |
− | + | v.invalide and '[[Image:Yes check.svg|15px|oui|lien=]]' or '', | |
− | + | } | |
− | + | lignesrt = table.concat( lignetab, '\n|' ) | |
− | + | table.insert( tableau, lignesrt ) | |
− | + | end | |
− | + | end | |
− | + | table.sort( tableau ) | |
− | + | table.insert( tableau, 1, entete ) | |
+ | table.insert( tableau, '}' ) | ||
+ | |||
+ | return table.concat( tableau, '\n|-\n|' ) | ||
end | end | ||
return Langue | return Langue |