Module:Main: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
new tax message function
refactor to use a shared fallback_pages function
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


local p = {} -- p stands for package
local p = {} -- p stands for package

-- Given an array of titles as strings, check them in order and return the first page which exists
function p.fallback_pages( list )
for key, value in ipairs( list ) do
local p = mw.title.new( value )
if p.exists then
return p
end
end
end


function p.get_appeal( frame )
function p.get_appeal( frame )
Line 8: Line 18:
local language = frame.args.language
local language = frame.args.language
local country = frame.args.country
local country = frame.args.country

local p1 = mw.title.new( 'Template:Appeal/' .. appeal_name .. '/' .. language .. '/' .. country )
local page = p.fallback_pages({
'Template:Appeal/' .. appeal_name .. '/' .. language .. '/' .. country,
local p2 = mw.title.new( 'Template:Appeal/' .. appeal_name .. '/' .. language )
'Template:Appeal/' .. appeal_name .. '/' .. language,
local p3 = mw.title.new( 'Template:Appeal/' .. appeal_name .. '/en' )
'Template:Appeal/' .. appeal_name .. '/en',
local p4 = mw.title.new( 'Template:Appeal/default/' .. language )
'Template:Appeal/default/' .. language,
local p5 = mw.title.new( 'Template:Appeal/default/en' )
'Template:Appeal/default/en'
})

if p1.exists then
page = p1
elseif p2.exists then
page = p2
elseif p3.exists then
page = p3
elseif p4.exists then
page = p4
else
page = p5
end


local expanded = frame:expandTemplate{ title = page, args = frame.args }
local expanded = frame:expandTemplate{ title = page, args = frame.args }
Line 37: Line 37:
local language = frame.args.language
local language = frame.args.language
local country = frame.args.country
local country = frame.args.country

local p1 = mw.title.new( 'Template:Infobox/' .. infobox_name .. '/' .. language .. '/' .. country )
local page = p.fallback_pages({
'Template:Infobox/' .. infobox_name .. '/' .. language .. '/' .. country,
local p2 = mw.title.new( 'Template:Infobox/' .. infobox_name .. '/' .. language )
'Template:Infobox/' .. infobox_name .. '/' .. language,
local p3 = mw.title.new( 'Template:Infobox/' .. infobox_name .. '/en' )
'Template:Infobox/' .. infobox_name .. '/en',
local p4 = mw.title.new( 'Template:Infobox/Default/' .. language )
'Template:Infobox/Default/' .. language,
local p5 = mw.title.new( 'Template:Infobox/Default/en' )
'Template:Infobox/Default/en'
})

if p1.exists then
page = p1
elseif p2.exists then
page = p2
elseif p3.exists then
page = p3
elseif p4.exists then
page = p4
else
page = p5
end


local expanded = frame:expandTemplate{ title = page, args = frame.args }
local expanded = frame:expandTemplate{ title = page, args = frame.args }
Line 61: Line 51:
end
end


function p.get_tax_message( frame )
-- TODO: Refactor this and others, could just build an array of titles and then pass to
-- another function to do fallback
function p.get_tax_message_new( frame )


local tax_message = frame.args.tax_message
local tax_message = frame.args.tax_message
local language = frame.args.language
local language = frame.args.language
local country = frame.args.country
local country = frame.args.country
local page

local p1 = mw.title.new( 'Template:Tax/' .. tax_message .. '/' .. language ) -- custom message
local p2 = mw.title.new( 'Template:Tax/' .. country .. '/' .. language ) -- special countries e.g. FR/NL
local p3 = mw.title.new( 'Template:Tax/Default/' .. language )
local p4 = mw.title.new( 'Template:Tax/LinkOnly' )


if ( country == 'US' ) then
if ( country == 'US' ) then
page = p4
page = mw.title.new( 'Template:Tax/LinkOnly' )
elseif p1.exists then
page = p1
elseif p2.exists then
page = p2
elseif p3.exists then
page = p3
else
else
page = p4
page = p.fallback_pages({
'Template:Tax/' .. tax_message .. '/' .. language, -- custom message
'Template:Tax/' .. country .. '/' .. language, -- special countries e.g. FR/NL
'Template:Tax/Default/' .. language,
'Template:Tax/LinkOnly'
})
end
end


Line 89: Line 72:
return expanded
return expanded
end

function p.get_tax_message( frame )

local tax_message = frame.args.tax_message
local language = frame.args.language
local country = frame.args.country

if ( tax_message == '' and country == 'FR' ) then
tax_message = 'FR'
end
if ( tax_message == '' and country == 'ES' ) then
tax_message = 'ES'
end

local p1 = mw.title.new( 'Template:Tax/' .. tax_message .. '/' .. language )
local p2 = mw.title.new( 'Template:Tax/LinkOnly' )

if p1.exists then
page = p1
else
page = p2
end

local expanded = frame:expandTemplate{ title = page, args = frame.args }
return expanded

end
end



Latest revision as of 17:55, 1 October 2024

Documentation

get_appeal

Includes the best localised appeal subpage of Template:Appeal, based on appeal name, language, and country given. Also passes the parameters to the selected appeal template for their use (e.g. in localising amounts with {{AppealAmountSwitch}})

Parameters:

  • appeal - initial "Appeal-" (which was required for older links) is stripped off if present
  • language
  • country
  • Any other parameters to pass through to the appeal template (e.g. sitename)

Example:

{{#invoke:Main|get_appeal|appeal=Alan|country=CA|language=fr}}

Replaces {{2012FR/Switch/Appeal}}

get_infobox

Does basically the same thing for subpages of Template:Infobox

Replaces {{2012FR/Switch/Infobox}}

get_tax_message

Code should be fairly self explanatory. Allows overriding the default message for certain countries.

page_language

Return the language code of the current subpage. If it isn't a subpage, or isn't a valid language code, return 'en'.


-- Single utility module. Break this into separate modules if it gets too large.

local p = {} -- p stands for package

-- Given an array of titles as strings, check them in order and return the first page which exists
function p.fallback_pages( list )
	for key, value in ipairs( list ) do
		local p = mw.title.new( value )
		if p.exists then
			return p
		end
	end
end

function p.get_appeal( frame )

    local appeal_name = string.gsub( frame.args.appeal , 'Appeal%-', '' )
    local language = frame.args.language
    local country = frame.args.country
    
    local page = p.fallback_pages({
    	'Template:Appeal/' .. appeal_name .. '/' .. language .. '/' .. country,
		'Template:Appeal/' .. appeal_name .. '/' .. language,
		'Template:Appeal/' .. appeal_name .. '/en',
		'Template:Appeal/default/' .. language,
		'Template:Appeal/default/en'
    })

    local expanded = frame:expandTemplate{ title = page, args = frame.args }
    return expanded

end

function p.get_infobox( frame )

    local infobox_name = frame.args.infobox
    local language = frame.args.language
    local country = frame.args.country
    
    local page = p.fallback_pages({
    	'Template:Infobox/' .. infobox_name .. '/' .. language .. '/' .. country,
		'Template:Infobox/' .. infobox_name .. '/' .. language,
		'Template:Infobox/' .. infobox_name .. '/en',
		'Template:Infobox/Default/' .. language,
		'Template:Infobox/Default/en'
    })

    local expanded = frame:expandTemplate{ title = page, args = frame.args }
    return expanded

end

function p.get_tax_message( frame )

    local tax_message = frame.args.tax_message
    local language = frame.args.language
    local country = frame.args.country
    local page

    if ( country == 'US' ) then
        page = mw.title.new( 'Template:Tax/LinkOnly' )
    else
    	page = p.fallback_pages({
    		'Template:Tax/' .. tax_message .. '/' .. language, -- custom message
			'Template:Tax/' .. country .. '/' .. language, -- special countries e.g. FR/NL
			'Template:Tax/Default/' .. language,
			'Template:Tax/LinkOnly'
    	})
    end

    local expanded = frame:expandTemplate{ title = page, args = frame.args }
    return expanded
    
end

function p.page_language( frame )

    local full_title = mw.title.getCurrentTitle().prefixedText
    local t = mw.text.split( full_title, '/' )
    local lang = t[#t]

    if mw.language.isKnownLanguageTag( lang ) then
        return lang
    else
        return 'en'
    end

end

return p