Module:Msg

Revision as of 15:14, 31 August 2025 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Module:Msg

This Lua module provides a page-language-aware message lookup system for PokeHero wiki content.

Usage

Use the module in wikitext with:

{{#invoke:Msg|msg|<category>|<last word>|lang=<language>}}
  • category – the category of messages: "types", "moves", "items", etc.
  • last word – the last part of the key. The module constructs the full key internally as:
 pokehero-<category>-<last word>
  • lang – optional. Overrides the page language (defaults to en).

Example

{{#invoke:Msg|msg|type|energy}}

This will display the translation for the page language. English is used as fallback if the page language is unavailable.

Fallback behavior

  • If the category table does not exist → returns <full key> (missing category table: <category>).
  • If the key does not exist in the table → returns <full key> (missing key in category <category>).
  • If the translation for the page language does not exist → falls back to English; if English is missing, returns <full key> (missing translations in category <category>).

Extending

  1. Add new categories as separate submodules under Module:Msg/ (plural names: types, moves, items).
  2. Add keys following the format pokehero-<category>-<last word> in the respective submodule.
  3. The main module automatically loads only the needed table for efficiency.

local p = {}

-- Helper to get page language
local function getPageLang()
    return mw.getCurrentFrame():preprocess("{{PAGELANGUAGE}}")
end

function p.msg(frame)
    local category = frame.args[1]
    local lastWord = frame.args[2]
    if not category or not lastWord then
        return "(missing category or key)"
    end

    local key = "pokehero-" .. category .. "-" .. lastWord
    local lang = frame.args.lang or getPageLang()
    
    local catTable
    local success, result = pcall(function()
        return mw.loadData("Module:Msg/" .. category)
    end)
    
    if success and result then
        catTable = result
    else
        -- Fallback: category table does not exist
        return key .. " (missing category table: " .. category .. ")"
    end

    if catTable[key] then
        if catTable[key][lang] then
            return catTable[key][lang]
        elseif catTable[key]["en"] then
            return catTable[key]["en"] -- fallback to English
        else
            return key .. " (missing translations in category " .. category .. ")"
        end
    else
        -- Key does not exist
        return key .. " (missing key in category " .. category .. ")"
    end
end

return p