Module:Country links: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
local country_names_data = mw.loadJsonData( 'Module:Country links/data.json' ) |
local country_names_data = mw.loadJsonData( 'Module:Country links/data.json' ) |
||
p.regions = { |
|||
{ |
|||
name = "Africa", |
|||
name_code = "002", |
|||
countries = { |
|||
"DZ", "AO", "BI", "TD", "EG", "GH", "KE", "LR", "MA", "NE", |
|||
"NG", "ZA", "UG" |
|||
} |
|||
}, |
|||
{ |
|||
name = "Asia", |
|||
name_code = "142", |
|||
countries = { |
|||
"AZ", "BH", "BD", "BT", "BN", "KH", "CN", "TL", "HK", "IN", |
|||
"IL", "JP", "JO", "KZ", "KW", "MO", "MY", "MV", "MN", "NP", |
|||
"OM", "PK", "PH", "QA", "SG", "LK", "TW", "TJ", "TH", "AE", |
|||
"VN" |
|||
} |
|||
}, |
|||
{ |
|||
name = "Europe", |
|||
name_code = "150", |
|||
countries = { |
|||
"AL", "AD", "AT", "BE", "BA", "BG", "HR", "CY", "CZ", "DK", |
|||
"EE", "FR", "GE", "DE", "GR", "HU", "IS", "IT", "LV", "LI", |
|||
"LT", "LU", "MT", "MC", "ME", "NL", "NO", "PL", "PT", "IE", |
|||
"RO", "RS", "SK", "SI", "ES", "SE", "CH", "UA", "GB", "VA" |
|||
} |
|||
}, |
|||
{ |
|||
name = "North America", |
|||
name_code = "003", |
|||
countries = { |
|||
"US", "AW", "BS", "BB", "BZ", "BM", "CA", "CR", "DM", "GD", |
|||
"GP", "HT", "HN", "JM", "MQ", "MX", "NI", "PA", "PR", "KN", |
|||
"LC", "PM", "VC", "TT", "TC" |
|||
} |
|||
}, |
|||
{ |
|||
name = "South America", |
|||
name_code = "005", |
|||
countries = { |
|||
"AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PY", |
|||
"PE", "SR", "UY", "VE" |
|||
} |
|||
}, |
|||
{ |
|||
name = "Oceania", |
|||
name_code = "009", |
|||
countries = { |
|||
"AS", "AU", "CK", "FJ", "PF", "GU", "KI", "MH", "FM", "NR", |
|||
"NZ", "PW" |
|||
} |
|||
} |
|||
} |
|||
function p.get_country_name( language_code, country_code ) |
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] |
local name = country_names_data[language_code][country_code] |
||
return name |
return name |
||
Line 10: | Line 65: | ||
function p.get_donate_url( language_code, country_code ) |
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' |
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 .. '&uselang=' .. language_code |
||
url = url .. '&country=' .. country_code |
url = url .. '&country=' .. country_code |
||
return url |
return url |
||
end |
end |
||
function p.make_donate_links( |
function p.make_donate_links( country_list ) |
||
local language_code = p.page_language( frame ) |
|||
local wikitext = '' |
|||
local t = {} |
local t = {} |
||
for key, value in ipairs( |
for key, value in ipairs(country_list) do |
||
table.insert(t, {value, p.get_country_name(language_code, value)}) |
table.insert(t, {value, p.get_country_name(language_code, value)}) |
||
end |
end |
||
Line 36: | Line 89: | ||
end |
end |
||
return wikitext |
|||
end |
|||
function p.main( frame ) |
|||
local language_code = p.page_language( frame ) |
|||
local wikitext = '' |
|||
for i,v in ipairs(p.regions) do |
|||
local region_name = p.get_country_name( language_code, v.name_code ) |
|||
wikitext = wikitext .. '\n== ' .. region_name .. ' ==\n' |
|||
wikitext = wikitext .. p.make_donate_links( v.countries ) |
|||
end |
|||
return wikitext |
return wikitext |
||
end |
end |
||
function p.page_language( frame ) |
function p.page_language( frame ) |
||
local full_title = mw.title.getCurrentTitle().prefixedText |
local full_title = mw.title.getCurrentTitle().prefixedText |
||
local t = mw.text.split( full_title, '/' ) |
local t = mw.text.split( full_title, '/' ) |
||
local lang = t[#t] |
local lang = t[#t] |
||
if mw.language.isKnownLanguageTag( lang ) then |
if mw.language.isKnownLanguageTag( lang ) then |
||
return lang |
return lang |
||
Line 50: | Line 112: | ||
return 'en' |
return 'en' |
||
end |
end |
||
end |
end |
||
Revision as of 11:35, 30 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' )
p.regions = {
{
name = "Africa",
name_code = "002",
countries = {
"DZ", "AO", "BI", "TD", "EG", "GH", "KE", "LR", "MA", "NE",
"NG", "ZA", "UG"
}
},
{
name = "Asia",
name_code = "142",
countries = {
"AZ", "BH", "BD", "BT", "BN", "KH", "CN", "TL", "HK", "IN",
"IL", "JP", "JO", "KZ", "KW", "MO", "MY", "MV", "MN", "NP",
"OM", "PK", "PH", "QA", "SG", "LK", "TW", "TJ", "TH", "AE",
"VN"
}
},
{
name = "Europe",
name_code = "150",
countries = {
"AL", "AD", "AT", "BE", "BA", "BG", "HR", "CY", "CZ", "DK",
"EE", "FR", "GE", "DE", "GR", "HU", "IS", "IT", "LV", "LI",
"LT", "LU", "MT", "MC", "ME", "NL", "NO", "PL", "PT", "IE",
"RO", "RS", "SK", "SI", "ES", "SE", "CH", "UA", "GB", "VA"
}
},
{
name = "North America",
name_code = "003",
countries = {
"US", "AW", "BS", "BB", "BZ", "BM", "CA", "CR", "DM", "GD",
"GP", "HT", "HN", "JM", "MQ", "MX", "NI", "PA", "PR", "KN",
"LC", "PM", "VC", "TT", "TC"
}
},
{
name = "South America",
name_code = "005",
countries = {
"AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PY",
"PE", "SR", "UY", "VE"
}
},
{
name = "Oceania",
name_code = "009",
countries = {
"AS", "AU", "CK", "FJ", "PF", "GU", "KI", "MH", "FM", "NR",
"NZ", "PW"
}
}
}
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( country_list )
local language_code = p.page_language( frame )
local wikitext = ''
local t = {}
for key, value in ipairs(country_list) 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 .. ']\n'
end
return wikitext
end
function p.main( frame )
local language_code = p.page_language( frame )
local wikitext = ''
for i,v in ipairs(p.regions) do
local region_name = p.get_country_name( language_code, v.name_code )
wikitext = wikitext .. '\n== ' .. region_name .. ' ==\n'
wikitext = wikitext .. p.make_donate_links( v.countries )
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