Difference between revisions of "Module:Languages"

From Minetest Wiki
Jump to navigation Jump to search
m (Fixed for subpages linking to other pages)
m (Added documentation and added getLanguageCode)
Line 10: Line 10:
 
end
 
end
  
local function getLangText(frame, title, lang)
+
-- 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 lang Language
 +
local function createLanguageText(frame, title, lang)
 
     title = title .. "/" .. lang
 
     title = title .. "/" .. lang
 +
 +
    -- Get language name in native language (a.k.a. autonym)
 
     local parsedLang = mw.language.fetchLanguageName(lang)
 
     local parsedLang = mw.language.fetchLanguageName(lang)
 +
 +
    -- Use <bdi> to support both LTR languages and RTL languages
 
     local bdi = HtmlBuilder.create("bdi")
 
     local bdi = HtmlBuilder.create("bdi")
 
         :attr("lang", lang)
 
         :attr("lang", lang)
 
         :wikitext("[[:", title, "|", parsedLang, "]]")
 
         :wikitext("[[:", title, "|", parsedLang, "]]")
 +
 +
    -- Use preprocessing with #ifexist to check if page exists
 
     return frame:preprocess("{{#ifexist:" .. title .. "|&nbsp;• " .. tostring(bdi) .. "}}")
 
     return frame:preprocess("{{#ifexist:" .. title .. "|&nbsp;• " .. tostring(bdi) .. "}}")
 
end
 
end
  
 
function p._languages(frame, args)
 
function p._languages(frame, args)
     local inputTitle = args[1]
+
     local currentTitleObj = mw.title.getCurrentTitle()
    local titleObj = mw.title.getCurrentTitle()
+
     local currentLangCode = p.getLanguageCode(currentTitleObj)
     local fullTitle = titleObj.text
+
     local currentHeader = headers[currentLangCode]
     local title = inputTitle or titleObj.nsText .. ":" .. titleObj.baseText
+
 
    local titleLang = titleObj.subpageText
+
    local title = args[1] -- Use input page name if given
    if titleLang == fullTitle then
+
    if not title then -- Otherwise, use current page
         titleLang = "en"
+
        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
 
     end
    local header = headers[titleLang]
+
 
    if not header then
+
     local languagesText = "[[:" .. title .. "|English]]" -- Start with English first
        title = inputTitle or fullTitle
+
     local translated = false -- Flag for checking if any translation exists
        titleLang = "en"
 
        header = headers[titleLang]
 
    end
 
     local text = "[[:" .. title .. "|English]]"
 
     local translated = false
 
 
     for lang, _ in pairs(headers) do
 
     for lang, _ in pairs(headers) do
         if lang ~= "en" then
+
         if lang ~= "en" then -- Skip English because already added
             local langText = getLangText(frame, title, lang)
+
             local languageText = createLanguageText(frame, title, lang)
             if langText ~= "" then
+
             if languageText ~= "" then -- Check if subpage exists
                 text = text ..langText
+
                 languagesText = languagesText .. languageText
 
                 translated = true
 
                 translated = true
 
             end
 
             end
Line 49: Line 83:
 
         args = {
 
         args = {
 
             image = "[[File:Geographylogo.png|25px|Languages|link=]]",
 
             image = "[[File:Geographylogo.png|25px|Languages|link=]]",
             header = header,
+
             header = currentHeader,
             text = text
+
             text = languagesText
 
         }
 
         }
 
     }
 
     }

Revision as of 08:09, 13 April 2017

local p = {}

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

function p.languages(frame)

   local args = getArgs(frame, {inherited = true})
   return p._languages(frame, args)

end

-- 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 lang Language local function createLanguageText(frame, title, lang)

   title = title .. "/" .. lang
   -- Get language name in native language (a.k.a. autonym)
   local parsedLang = mw.language.fetchLanguageName(lang)
   -- Use  to support both LTR languages and RTL languages
   local bdi = HtmlBuilder.create("bdi")
       :attr("lang", lang)
       :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
   local translated = false -- Flag for checking if any translation exists
   for lang, _ in pairs(headers) do
       if lang ~= "en" then -- Skip English because already added
           local languageText = createLanguageText(frame, title, lang)
           if languageText ~= "" then -- Check if subpage exists
               languagesText = languagesText .. languageText
               translated = true
           end
       end
   end
   local nmbox = frame:expandTemplate{
       title = "nmbox",
       args = {
           image = "Languages",
           header = currentHeader,
           text = languagesText
       }
   }
   local categories = ""
   if not translated then
       categories = categories .. ""
   end
   return nmbox .. categories

end

return p