Difference between revisions of "Module:Languages"

From Minetest Wiki
Jump to navigation Jump to search
m
m (Undo revision 15911 by MrIbby (talk))
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local TableBuilder = require("Module:TableBuilder")
+
local HtmlBuilder = require("Module:HtmlBuilder")
local getLanguageCode = require("Module:Languages").getLanguageCode
 
local animateImages = require("Module:Animated")._images
 
 
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc
 
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc
local dataPrefix = "Module:Infobox/"
+
local headers = mw.loadData('Module:Languages/headers')
  
-- Get data (IDs and names) for infobox row headers.
+
-- Get the language code of a page.
 
--
 
--
-- @param type Infobox type (e.g. block)
+
-- @param titleObj Title object of the page, defaults to current page
-- @param langCode Language code, defaults to current language
+
function p.getLanguageCode(titleObj)
local function getRowHeadersData(type, langCode, frame)
+
     titleObj = titleObj or mw.title.getCurrentTitle() -- Set default if necessary
    langCode = langCode or getLanguageCode()
+
 
     frame = frame or mw.getCurrentFrame()
+
     local title = titleObj.text -- e.g. Dirt/fr or Mods/Quartz
     local title = dataPrefix .. langCode
+
    local langCode = titleObj.subpageText -- e.g. fr or Quartz
     if frame:preprocess("{{#ifexist:" .. title .. "|true}}") ~= "" then
+
 
        local data = mw.loadData(title)
+
     -- titleObj.subpageText returns titleObj.text if not a subpage.
         return data and data[type] or nil
+
    -- If titleObj.subpageText is a subpage, check if it is a language code.
 +
    if langCode == title or not headers[langCode] then
 +
         return "en" -- Default to English
 
     end
 
     end
     return nil
+
 
 +
     return langCode
 
end
 
end
  
-- Create an infobox.
+
-- Create the linked text for a translation subpage if it exists.
 
--
 
--
-- @param args Infobox arguments
+
-- @param frame Current frame, used for preprocessing
-- @param type Infobox type (e.g. block)
+
-- @param title Page title
local function infobox(args, type)
+
-- @param langCode Language code
     local rowHeadersData = getRowHeadersData(type) or getRowHeadersData(type, "en")
+
local function createLanguageText(frame, title, langCode)
     local name = args.name
+
     title = title .. "/" .. langCode
    local width = args.width or "200px"
+
 
    local description = args.description
+
     -- Get language name in native language (a.k.a. autonym)
     local image = args.image or "Blank.png"
+
     local parsedLang = mw.language.fetchLanguageName(langCode)
     local imagesize = args.imagesize
+
 
     local imageText
+
     -- Use <bdi> to support both LTR languages and RTL languages
    if string.match(image, ",") then
+
     local bdi = HtmlBuilder.create("bdi")
         imageText = animateImages({image, imagesize})
+
         :attr("lang", langCode)
    else
+
         :wikitext("[[:", title, "|", parsedLang, "]]")
         if imagesize then
+
 
            imageText = "[[File:" .. image .. "|" .. imagesize .. "]]"
+
    -- Use preprocessing with #ifexist to check if page exists
        else
+
    return frame:preprocess("{{#ifexist:" .. title .. "|&nbsp;• " .. tostring(bdi) .. "}}")
            imageText = "[[File:" .. image .. "]]"
+
end
        end
 
    end
 
  
     local infobox = TableBuilder.create()
+
function p._languages(frame, args)
 +
     local currentTitleObj = mw.title.getCurrentTitle()
 +
    local currentLangCode = p.getLanguageCode(currentTitleObj)
 +
    local currentHeader = headers[currentLangCode]
  
     infobox
+
     local title = args[1] -- Use input page name if given
        :addClass("wikitable")
+
    if not title then -- Otherwise, use current page
        :css("float", "right")
+
         local namespace = currentTitleObj.nsText
        :css("text-align", "left")
+
         local basename = currentTitleObj.baseText
         :css("margin", "0 0 0.5em 1em")
+
         title = namespace .. ":" .. basename -- e.g. Template:Block Data
         :css("padding", "5px")
 
         :css("font-size", "90%")
 
        :css("position", "relative")
 
        :css("clear", "right")
 
        :css("overflow", "auto")
 
        :css("z-index", "1")
 
        :attr("width", width)
 
        :addRow()
 
            :addHeader()
 
                :attr("colspan", 2)
 
                :css("font-size", "110%")
 
                :css("text-align", "center")
 
                :wikitext(name)
 
                :done()
 
            :done()
 
        :addRow()
 
            :addHeader()
 
                :attr("colspan", 2)
 
                :tag("div")
 
                    :addClass("center")
 
                    :wikitext(imageText)
 
                    :done()
 
                :done()
 
            :done()
 
        :addRow()
 
            :addHeader()
 
                :attr("colspan", 2)
 
                :attr("align", "center")
 
                :wikitext(description)
 
  
    for _, rowHeaderData in ipairs(rowHeadersData) do
+
        -- Check if current page is English subpage (e.g. Mods/Quartz)
         local rowData = args[rowHeaderData.id]
+
         if currentLangCode == "en" then
        if rowData and rowData ~= "" then
+
            local subname = currentTitleObj.subpageText
            infobox
+
            if subname ~= basename then
                 :addRow()
+
                 title = title .. "/" .. subname
                    :addData()
+
            end
                        :wikitext("'''" .. rowHeaderData.name .. "'''")
 
                        :done()
 
                    :addData()
 
                        :wikitext(rowData)
 
 
         end
 
         end
 
     end
 
     end
  
     return infobox
+
     local languagesText = "[[:" .. title .. "|English]]" -- Start with English first
end
+
 
 +
    -- Bold English text if current language is English
 +
    if currentLangCode == "en" then
 +
        languagesText = "'''" .. languagesText .. "'''"
 +
    end
  
function p._block(args)
+
     local translated = false -- Flag for checking if any translation exists
     args.imagesize = args.imagesize or "150px"
 
    return tostring(infobox(args, "block"))
 
end
 
  
function p._item(args)
+
    -- Create an alphabetically sorted table of language codes
     args.imagesize = args.imagesize or "160px"
+
     local langCodes = {}
     return tostring(infobox(args, "item"))
+
     for langCode in pairs(headers) do
end
+
        table.insert(langCodes, langCode)
 +
    end
 +
    table.sort(langCodes)
  
function p._foodItem(args)
+
    -- Iterate through language codes in order
    args.imagesize = args.imagesize or "160px"
+
    for _, langCode in ipairs(langCodes) do
    return tostring(infobox(args, "foodItem"))
+
        if langCode ~= "en" then -- Skip English because already added
end
+
            local languageText = createLanguageText(frame, title, langCode)
 +
            if languageText ~= "" then -- Check if subpage exists
 +
                -- Bold language text if language is current language
 +
                if langCode == currentLangCode then
 +
                    languageText = "'''" .. languageText .. "'''"
 +
                end
 +
                languagesText = languagesText .. languageText
 +
                translated = true
 +
            end
 +
        end
 +
    end
  
function p._mob(args)
+
    local nmbox = frame:expandTemplate{
    args.imagesize = args.imagesize or "150px"
+
        title = "nmbox",
    return tostring(infobox(args, "mob"))
+
        args = {
end
+
            image = "[[File:Geographylogo.png|25px|Languages|link=]]",
 +
            header = currentHeader,
 +
            text = languagesText
 +
        }
 +
    }
  
function p._game(args)
+
     if not args.nocat then
     args.imagesize = args.imagesize or "150px"
+
        local categories = "[[Category:Languages pages]]"
    return tostring(infobox(args, "game"))
+
        if not translated then
end
+
            categories = categories .. "[[Category:Languages pages without translations]]"
 +
        end
 +
        return nmbox .. categories
 +
    end
  
function p._mod(args)
+
     return nmbox
    args.imagesize = args.imagesize or "150px"
 
     return tostring(infobox(args, "mod"))
 
 
end
 
end
  
p.block = makeInvokeFunc(p._block, {inherited = true})
+
p.languages = makeInvokeFunc(p._languages, {passFrameParam = true, inherited = true})
p.item = makeInvokeFunc(p._item, {inherited = true})
 
p.foodItem = makeInvokeFunc(p._foodItem, {inherited = true})
 
p.mob = makeInvokeFunc(p._mob, {inherited = true})
 
p.game = makeInvokeFunc(p._game, {inherited = true})
 
p.mod = makeInvokeFunc(p._mod, {inherited = true})
 
  
 
return p
 
return p

Revision as of 08:04, 26 May 2017

local p = {}

local HtmlBuilder = require("Module:HtmlBuilder") local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc local headers = mw.loadData('Module:Languages/headers')

-- Get the language code of a page. -- -- @param titleObj Title object of the page, defaults to current page function p.getLanguageCode(titleObj)

   titleObj = titleObj or mw.title.getCurrentTitle() -- Set default if necessary
   local title = titleObj.text -- e.g. Dirt/fr or Mods/Quartz
   local langCode = titleObj.subpageText -- e.g. fr or Quartz
   -- titleObj.subpageText returns titleObj.text if not a subpage.
   -- If titleObj.subpageText is a subpage, check if it is a language code.
   if langCode == title or not headers[langCode] then
       return "en" -- Default to English
   end
   return langCode

end

-- Create the linked text for a translation subpage if it exists. -- -- @param frame Current frame, used for preprocessing -- @param title Page title -- @param langCode Language code local function createLanguageText(frame, title, langCode)

   title = title .. "/" .. langCode
   -- Get language name in native language (a.k.a. autonym)
   local parsedLang = mw.language.fetchLanguageName(langCode)
   -- Use  to support both LTR languages and RTL languages
   local bdi = HtmlBuilder.create("bdi")
       :attr("lang", langCode)
       :wikitext("", parsedLang, "")
   -- Use preprocessing with #ifexist to check if page exists
   return frame:preprocess("")

end

function p._languages(frame, args)

   local currentTitleObj = mw.title.getCurrentTitle()
   local currentLangCode = p.getLanguageCode(currentTitleObj)
   local currentHeader = headers[currentLangCode]
   local title = args[1] -- Use input page name if given
   if not title then -- Otherwise, use current page
       local namespace = currentTitleObj.nsText
       local basename = currentTitleObj.baseText
       title = namespace .. ":" .. basename -- e.g. Template:Block Data
       -- Check if current page is English subpage (e.g. Mods/Quartz)
       if currentLangCode == "en" then
           local subname = currentTitleObj.subpageText
           if subname ~= basename then
               title = title .. "/" .. subname
           end
       end
   end
   local languagesText = "English" -- Start with English first
   -- Bold English text if current language is English
   if currentLangCode == "en" then
       languagesText = "" .. languagesText .. ""
   end
   local translated = false -- Flag for checking if any translation exists
   -- Create an alphabetically sorted table of language codes
   local langCodes = {}
   for langCode in pairs(headers) do
       table.insert(langCodes, langCode)
   end
   table.sort(langCodes)
   -- Iterate through language codes in order
   for _, langCode in ipairs(langCodes) do
       if langCode ~= "en" then -- Skip English because already added
           local languageText = createLanguageText(frame, title, langCode)
           if languageText ~= "" then -- Check if subpage exists
               -- Bold language text if language is current language
               if langCode == currentLangCode then
                   languageText = "" .. languageText .. ""
               end
               languagesText = languagesText .. languageText
               translated = true
           end
       end
   end
   local nmbox = frame:expandTemplate{
       title = "nmbox",
       args = {
           image = "Languages",
           header = currentHeader,
           text = languagesText
       }
   }
   if not args.nocat then
       local categories = ""
       if not translated then
           categories = categories .. ""
       end
       return nmbox .. categories
   end
   return nmbox

end

p.languages = makeInvokeFunc(p._languages, {passFrameParam = true, inherited = true})

return p