Module:ClassNav: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 2: Line 2:


-- Fonction utilitaire pour récupérer une traduction
-- Fonction utilitaire pour récupérer une traduction
local function getTrans(target_id, lang, type, field)
local function getTrans(target_id, lang, type_force, field)
     field = field or "name"
     field = field or "name"
     if not target_id or target_id == "" then return nil end
     if not target_id or target_id == "" then return nil end
   
    -- Si tu forces un type, on l'utilise, sinon on cherche par défaut dans 'wiki'
    local type_to_use = type_force or "wiki"
   
     local query = mw.ext.cargo.query("Translation", field, {
     local query = mw.ext.cargo.query("Translation", field, {
         where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, type),
         where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, type_to_use),
         limit = 1
         limit = 1
     })
     })
   
    -- Fallback sur l'anglais si rien n'est trouvé
     if not query or #query == 0 then
     if not query or #query == 0 then
         query = mw.ext.cargo.query("Translation", "name", {
         query = mw.ext.cargo.query("Translation", "name", {
             where = string.format("target_id = '%s' AND language = 'en' AND type = '%s'", target_id, type),
             where = string.format("target_id = '%s' AND language = 'en' AND type = '%s'", target_id, type_to_use),
             limit = 1
             limit = 1
         })
         })
     end
     end
   
     return (query and #query > 0) and query[1][field] or nil
     return (query and #query > 0) and query[1][field] or nil
end
end
Line 22: Line 29:
     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, "wiki") 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, "wiki") or label_id
         table.insert(html, "! style='width:8em;'| " .. label)
          
          
        -- Requête corrigée avec 'class_type'
         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
             local name = getTrans(c.id, lang, "hero_class") or c.id
             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]]',  
             table.insert(html, string.format('| style="display:inline-block; border:0px;"| [[File:%s.png|30px|link=%s/%s]] [[%s/%s|%s]]',  
                 c.id, c.id, lang, c.id, lang, name))
                 file_name, name_en, lang, name_en, lang, name_translated))
         end
         end
     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, "|-")
     table.insert(html, '| colspan=10 style="text-align: center;"| [[Heroes/' .. lang .. '|' .. (getTrans("all_heroes", lang, "ui") or "Tous les Héros") .. ']]')
     table.insert(html, '| colspan=10 style="text-align: center;"| [[Heroes/' .. lang .. '|' .. (getTrans("wiki_all_heroes", lang, "wiki") or "All Heroes") .. ']]')
      
      
     table.insert(html, "|}")
     table.insert(html, "|}")
   
     return table.concat(html, "\n")
     return table.concat(html, "\n")
end
end


return p
return p

Latest revision as of 17:39, 31 May 2026

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

local p = {}

-- Fonction utilitaire pour récupérer une traduction
local function getTrans(target_id, lang, type_force, field)
    field = field or "name"
    if not target_id or target_id == "" then return nil end
    
    -- Si tu forces un type, on l'utilise, sinon on cherche par défaut dans 'wiki'
    local type_to_use = type_force or "wiki"
    
    local query = mw.ext.cargo.query("Translation", field, {
        where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, type_to_use),
        limit = 1
    })
    
    -- Fallback sur l'anglais si rien n'est trouvé
    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_to_use),
            limit = 1
        })
    end
    
    return (query and #query > 0) and query[1][field] or nil
end

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, "wiki") 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, "wiki") 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("wiki_all_heroes", lang, "wiki") or "All Heroes") .. ']]')
    
    table.insert(html, "|}")
    
    return table.concat(html, "\n")
end

return p