Module:ClassNav: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
-- Fonction utilitaire pour récupérer une traduction
local function getTrans(target_id, lang, type, field)
    field = field or "name"
    if not target_id or target_id == "" then return nil end
    local query = mw.ext.cargo.query("Translation", field, {
        where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, type),
        limit = 1
    })
    if not query or #query == 0 then
        query = mw.ext.cargo.query("Translation", "name", {
            where = string.format("target_id = '%s' AND language = 'en' AND type = '%s'", target_id, type),
            limit = 1
        })
    end
    return (query and #query > 0) and query[1][field] or nil
end
function p.render(frame)
function p.render(frame)
     local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
     local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
     local html = {'{| class="wikitable" style="width:100%; text-align:left;"'}
     local html = {'{| class="wikitable" style="width:100%; text-align:left;"'}
      
      
     -- Titre
     -- 1. Titre "Classes" (utilisation de wiki_hero_class ou wiki_classes)
     table.insert(html, '! colspan=10 style="text-align:center; letter-spacing:.3px;"| ' .. (getTrans("wiki_classes", lang, "ui") or "Classes"))
    -- J'utilise wiki_hero_class comme tu l'as indiqué
    local title = getTrans("wiki_hero_class", lang, "hero_infobox") or "Classes"
     table.insert(html, '! colspan=10 style="text-align:center; letter-spacing:.3px;"| ' .. title)
      
      
-- Fonction pour créer les lignes
    -- Fonction interne modifiée pour utiliser les nouveaux IDs
     local function addRow(category_id, class_type)
     local function addRow(category_id, class_type, label_id)
         table.insert(html, "|-")
         table.insert(html, "|-")
         table.insert(html, "! style='width:8em;'| " .. (getTrans(category_id, lang, "ui") or category_id))
        -- 2. Label dynamique (Force ou Magie)
        local label = getTrans(label_id, lang, "common") or label_id
         table.insert(html, "! style='width:8em;'| " .. label)
          
          
        -- Requête pour lister les classes
         local classes = mw.ext.cargo.query("HeroClass", "id", {where = "class_type = '" .. class_type .. "'"})
         local classes = mw.ext.cargo.query("HeroClass", "id", {where = "class_type = '" .. class_type .. "'"})
         for _, c in ipairs(classes) do
         for _, c in ipairs(classes) do
            -- 1. Récupération du nom traduit pour le lien
             local name_translated = getTrans(c.id, lang, "hero_class") or c.id
             local name_translated = getTrans(c.id, lang, "hero_class") or c.id
           
            -- 2. Récupération du nom anglais pour le nom de fichier et le lien racine
            -- On utilise getTrans avec 'en' pour forcer la récupération de l'anglais
             local name_en = getTrans(c.id, 'en', 'hero_class') or c.id
             local name_en = getTrans(c.id, 'en', 'hero_class') or c.id
           
            -- Remplacement des espaces par des underscores pour le nom de fichier (si nécessaire)
             local file_name = name_en:gsub("%s+", "_")
             local file_name = name_en:gsub("%s+", "_")
              
              
            -- 3. Construction de l'élément (File:English_Name.png | [[English_Name/fr|Nom traduit]])
             table.insert(html, string.format('| style="display:inline-block; border:0px;"| [[File:%s.png|30px|link=%s/%s]] [[%s/%s|%s]]',  
             table.insert(html, string.format('| style="display:inline-block; border:0px;"| [[File:%s.png|30px|link=%s/%s]] [[%s/%s|%s]]',  
                 file_name, name_en, lang, name_en, lang, name_translated))
                 file_name, name_en, lang, name_en, lang, name_translated))
Line 49: Line 26:
     end
     end


     -- Appel avec les bonnes valeurs (might ou magic)
     -- 3. Appel avec les bons IDs de traduction
     addRow("might_heroes", "might")
     addRow("might_heroes", "might", "wiki_might")
     addRow("magic_heroes", "magic")
     addRow("magic_heroes", "magic", "wiki_magic")
      
      
     table.insert(html, "|-")
     table.insert(html, "|-")
Line 59: Line 36:
     return table.concat(html, "\n")
     return table.concat(html, "\n")
end
end
return p

Revision as of 09:44, 31 May 2026

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

function p.render(frame)
    local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
    local html = {'{| class="wikitable" style="width:100%; text-align:left;"'}
    
    -- 1. Titre "Classes" (utilisation de wiki_hero_class ou wiki_classes)
    -- J'utilise wiki_hero_class comme tu l'as indiqué
    local title = getTrans("wiki_hero_class", lang, "hero_infobox") or "Classes"
    table.insert(html, '! colspan=10 style="text-align:center; letter-spacing:.3px;"| ' .. title)
    
    -- Fonction interne modifiée pour utiliser les nouveaux IDs
    local function addRow(category_id, class_type, label_id)
        table.insert(html, "|-")
        -- 2. Label dynamique (Force ou Magie)
        local label = getTrans(label_id, lang, "common") or label_id
        table.insert(html, "! style='width:8em;'| " .. label)
        
        local classes = mw.ext.cargo.query("HeroClass", "id", {where = "class_type = '" .. class_type .. "'"})
        for _, c in ipairs(classes) do
            local name_translated = getTrans(c.id, lang, "hero_class") or c.id
            local name_en = getTrans(c.id, 'en', 'hero_class') or c.id
            local file_name = name_en:gsub("%s+", "_")
            
            table.insert(html, string.format('| style="display:inline-block; border:0px;"| [[File:%s.png|30px|link=%s/%s]] [[%s/%s|%s]]', 
                file_name, name_en, lang, name_en, lang, name_translated))
        end
    end

    -- 3. Appel avec les bons IDs de traduction
    addRow("might_heroes", "might", "wiki_might")
    addRow("magic_heroes", "magic", "wiki_magic")
    
    table.insert(html, "|-")
    table.insert(html, '| colspan=10 style="text-align: center;"| [[Heroes/' .. lang .. '|' .. (getTrans("all_heroes", lang, "ui") or "Tous les Héros") .. ']]')
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
end