Module:Country

From Saintapedia
Jump to navigation Jump to search

Country gets data about a country using its ISO code, decimal code, or name.

Syntax

Options

Name Description Example output
alpha-2 2-letter country code Lua error in package.lua at line 80: module 'Country/data' not found.
alpha-3 3-letter country code Lua error in package.lua at line 80: module 'Country/data' not found.
country-code Country code Lua error in package.lua at line 80: module 'Country/data' not found.
iso_3166-2 ISO code Lua error in package.lua at line 80: module 'Country/data' not found.
name Country name Lua error in package.lua at line 80: module 'Country/data' not found.
region Region name Lua error in package.lua at line 80: module 'Country/data' not found.
region-code Region code Lua error in package.lua at line 80: module 'Country/data' not found.
sub-region Subregion name Lua error in package.lua at line 80: module 'Country/data' not found.
sub-region-code Subregion code Lua error in package.lua at line 80: module 'Country/data' not found.

Example

Lua error in package.lua at line 80: module 'Dev:Arguments' not found. Lua error in package.lua at line 80: module 'Dev:Arguments' not found. Lua error in package.lua at line 80: module 'Dev:Arguments' not found.

See also


--[[<pre> https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes
Description : Gets data about a country, see format in Module:Country/data
Syntax : 
{{#invoke:country|main|country-isocode|[name":"New Zealand/alpha-2/alpha-3/country-code
/sub-region-code/region-code/iso_3166-2/region/sub-region}} 
{{#invoke:country|countrycode|china}} 
* country-isocode (e.g. fr =="France")

]]--

local p = {}
local countryData = mw.loadData("Country/data")
  
function p.main(frame)
	local args = frame.args

	if args then
		local countryCode = args[1] or args.code
		local info = args[2] or args.info

		if countryCode and info then
			return getCountryData(countryCode, info)
		end
	end
end

function p.countrycode(frame)
	local args = frame.args

	if args then
		local countryCode = args[1] or args.name

		if countryCode then
			return getCountryCode(countryCode, args.match)
		end
	end
end

function getCountryData(code, info)
	if info and code then
		-- if the code doesn't exist try looking for a country name
		if not countryData[code]  then
			code = getCountryCode(code)
		end
		return (countryData[code] or {})[info]
	end
end

function getCountryCode(name, bCloseMatch)
	name = name:lower()
	
	for sCode, sData in pairs(countryData) do
		if sData.name:lower() == name or bCloseMatch and sData.name:match(name) then
			return sCode:lower()
		end
	end
end

return p