Module:Country links: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 21: Line 21:


local language_code = p.page_language( frame )
local language_code = p.page_language( frame )
local wikitext = ''
local t = {}
local t = {}
for key, value in ipairs(frame.args) do
for key, value in ipairs(frame.args) do
Line 32: Line 33:
local url = p.get_donate_url( language_code, value[1] )
local url = p.get_donate_url( language_code, value[1] )
local link_text = value[2]
local link_text = value[2]
mw.log('* [' .. url .. ' ' .. link_text .. ']')
wikitext = wikitext .. '* [' .. url .. ' ' .. link_text .. ']'
end
end


return
return wikitext
end
end



Revision as of 13:36, 29 April 2023

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

local p = {}
local country_names_data = mw.loadJsonData( 'Module:Country links/data.json' )

function p.get_country_name( language_code, country_code )
	local name
	if country_names_data[language_code] == nil then language_code = 'en' end
    local name = country_names_data[language_code][country_code]
    return name
end

function p.get_donate_url( language_code, country_code )

    local url = 'https://donate.wikimedia.org/wiki/Special:FundraiserLandingPage?utm_medium=Waystogive&utm_source=Waystogive&utm_campaign=C11_Waystogive'
    url = url .. '&uselang=' .. language_code
    url = url .. '&country=' .. country_code
    return url

end

function p.make_donate_links( frame )

	local language_code = p.page_language( frame )
	local wikitext = ''
    local t = {}
    for key, value in ipairs(frame.args) do
        table.insert(t, {value, p.get_country_name(language_code, value)})
    end

    -- sort by country name
    table.sort(t, function (k1, k2) return k1[2] < k2[2] end)

    for key, value in ipairs(t) do
        local url = p.get_donate_url( language_code, value[1] )
        local link_text = value[2]
        wikitext = wikitext .. '* [' .. url .. ' ' .. link_text .. ']'
    end

    return wikitext
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