Mô đun:Country alias

Trang khóa di chuyển
Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[xem] [sửa] [lịch sử] [làm mới]

Mô đun này thực hiện {{country alias}}. Kiểm tra tài liệu hướng dẫn sử dụng

Sử dụng[sửa mã nguồn]

This module is used by {{country alias}} to return either the country name or the country's flag, based on a combination of the three-letter country code (required), the year (optional), and the competition being played (optional).

{{#gọi:Country alias|main|alias=|flag=|games=|year=}}

-- This module returns the country name or the flag name for a country,
-- based on the three-letter IOC/CGA/FINA alias.

--[[
The following country code is used for multiple countries:
    ANG (workaround: added ANG_CGF for use with Commonwealth Games)

The following names have different names/flags based on sport/year
    Anh Quốc (và N.I.)               GBR, GBR_WCA (latter added to add text in parens)
    Hồng Kông                        HKG, HKG_CGF (latter added to keep colonial flag)
    Vận động viên Olympic cá nhân    IOA, IOA_2000 (IOA changed to Independent Olympic Athletes in 2012)
    Vận động viên Nga				 OAR (2018), ROC_2020 (2020 Summer, 2022 Winter)
    SWZ                              Swaziland became Eswatini after the 2018 Commonwealth Games
    MKD								 Macedonia became North Macedonia in 2019
    ART								 No "Athlete" before Refugee Team @ 2017 AIMAG

The following countries have multiple aliases due to CGF/IOC/FINA/IAAF/etc differences, or deprecated uses
    Anguilla                         AIA, ANG_CGF
    Antigua và Barbuda               ANT, ATG
    Bahrain                          BHN, BHR, BRN
    Curaçao                          CUR, CUW
    Đông Timor						 TLS, TMP
    Quần đảo Faroe                   FAR, FRO
    Guernsey                         GGY, GUE
    Iran                             IRI, IRN
    Ireland                          IRE, IRL - IRE is *only* for CGF apps
    Jersey                           JER, JEY
    Liban                            LBN, LIB
    Montserrat                       MNT, MSR
    Nicaragua                        NCA, NIC
    Đảo Norfolk                      NFI, NFK
    Oman                             OMA, OMN
    Đội tuyển Olympic người tị nạn   ROA, ROT, EOR
    România                          ROM, ROU
    Saint Helena                     SHE, SHN
    Saint Vincent và Grenadines      SVG, VIN
    Sarawak                          SAR, SWK
    Singapore                        SGP, SIN
    Nam Phi                          RSA, SAF
    Tonga                            TGA, TON
    Trinidad và Tobago               TRI, TTO
    Quần đảo Turks và Caicos         TCA, TCI, TKS

Oddity that needs to be revisited
    Polynésie thuộc Pháp             PYF, TAH - TAH has been converted to Tahiti per SILENCE
]]

local function stripToNil(text)
	-- If text is a string, return its trimmed content, or nil if empty.
	-- Otherwise return text (which may, for example, be nil).
	if type(text) == 'string' then
		text = text:match('(%S.-)%s*$')
	end
	return text
end

local function yes(parameter)
	-- Return true if parameter should be interpreted as "yes".
	return ({ y = true, yes = true, on = true, [true] = true })[parameter]
end

local function getAlias(args)
	-- Return alias parameter, possibly modified for exceptional cases.
	local alias = stripToNil(args.alias)
	local games = stripToNil(args.games)
	local year = tonumber(args.year)
	local fullName = stripToNil(args.fullName)
	if fullName then
		year = tonumber(fullName:match('%d+$'))  -- ignore args.year
	end
	if alias == 'ANG' then
		if games == 'Đại hội Thể thao Khối Thịnh vượng chung' then
			alias = 'ANG_CGF'
		end
	elseif alias == 'ART' then
		if games == 'Đại hội Thể thao Trong nhà và Võ thuật châu Á' then
			alias = 'ART_AIMAG'
		end
	elseif alias == 'BAH' then
		if games == 'Olympic Toán học Quốc tế' then
			alias = 'BHR'
		end
	elseif alias == 'GBR' then
		if games == 'Giải vô địch điền kinh thế giới' or games == 'Giải vô địch điền kinh thế giới' or games == 'Giải vô địch điền kinh châu Âu' then
			alias = 'GBR_WCA'
		elseif games == 'Giải vô địch châu Âu' then
			if year == 2018 then
				alias = 'GBR_WCA'
			end
		end
	elseif alias == 'HKG' then
		if games == 'Đại hội Thể thao Khối Thịnh vượng chung' then
			alias = 'HKG_CGF'
		end
	elseif alias == 'KHM' then
		if games == 'Olympic Toán học Quốc tế' then
			alias = 'CAM'
		end
	elseif alias == 'IOA' then
		if year == 2000 then
			alias = 'IOA_2000'
		end
	elseif alias == 'MAL' or alias == 'MAS' then
		if year and year < 1963 then
			alias = 'MAL'
		else
			alias = 'MAS'
		end
	elseif alias == 'SWZ' then
		if fullName then
			if year and year >= 2018 and fullName ~= 'Đại hội Thể thao Khối Thịnh vượng chung 2018' then
				alias = 'SWZ_YO2018'
			end
		elseif year and year >= 2018 and games ~= 'Đại hội Thể thao Khối Thịnh vượng chung' then
			alias = 'SWZ_YO2018'
		else
			alias = 'SWZ_YO2018'
		end
	elseif alias == 'MKD' then
		if year and year < 2019 then
			alias = 'MKD'
		else
			alias = 'MKD_2019'
		end
	elseif alias == 'VNM' then
		if year and year <= 1954 then
			alias = 'VIE'
	else
			local pageName = mw.title.getCurrentTitle().text
			if pageName == 'Việt Nam tại Olympic Toán học Quốc tế' or 'Việt Nam tại Olympic Vật lý Quốc tế' or 'Việt Nam tại Olympic Hóa học Quốc tế' or 'Việt Nam tại Olympic Tin học Quốc tế' or 'Việt Nam tại Olympic Sinh học Quốc tế' then
				alias = 'VIE'
			end
		end
	elseif alias == 'RUS' then
		if year and year==2020 then
				alias = 'ROC_2020'
			elseif games=='Thế vận hội Người khuyết tật Mùa hè' then
				alias = 'RPC'
			elseif year and year==2022 then
				alias = 'ROC_2020'
		end
	elseif alias == 'ROC' then
		if year and year==1960 then
			alias = 'ROC_Formosa'
		elseif year and ( year==1964 or year==1968 ) then
			alias = 'ROC_Taiwan'
		elseif year and year==2020 then
			alias = 'ROC_2020'
		elseif year and year==2022 then
			alias = 'ROC_2020'
		elseif year and year==2020 and games=='Thế vận hội Người khuyết tật Mùa hè' then
			alias = 'RPC'
		else
			local pageName = mw.title.getCurrentTitle().text
			if pageName == 'Ủy ban Olympic Nga tại Thế vận hội' then
				alias = 'ROC_2020'
			end
		end
	elseif alias == 'YUG' or alias == 'SCG' then
		if year and year > 1992 and year < 2004 and games ~= 'Thế vận hội Người khuyết tật Mùa hè' then
			alias = 'FR_YUG'
		end
	end
	return alias
end


local function getFlag(args, country)
	-- Return name of flag selected from country data (nil if none defined).
	local year = tonumber(args.year)
	local games = stripToNil(args.games)
	if games then
		local gdata = country[games]
		if gdata then
			if type(gdata) == 'string' then
				return gdata
			end
			if gdata[year] then
				return gdata[year]
			end
		end
	end
	for _, item in ipairs(country) do
		if type(item) == 'string' then
			return item
		end
		if year and year <= item[1] then
			return item[2]
		end
	end
end

local data = mw.loadData('Mô đun:Country alias/data')
local function countryAlias(args)
	local alias = getAlias(args)
	local country = data.countries[alias] or data.countries[data.countryAliases[alias]]
	local function quit(message)
		return args.error or error(message)
	end
	if not country then
		return quit('Biến thể quốc gia không hợp lệ: ' .. tostring(alias))
	end
	if yes(args.flag) then
		return getFlag(args, country) or quit('Không có lá cờ nào được xác định cho ' .. alias)
	else
		return country.name or quit('Không có tên được xác định cho ' .. alias)
	end
end

local function flagIOC(frame)
	-- Implement {{flagIOC}} which previously called this module three times.
	-- Returns <flag> <country link> <athletes>, with the third value optional
	local args = frame:getParent().args
	local code = stripToNil(args[1]) or error('flagIOC tham số 1 phải là một mã quốc gia')
	local games = stripToNil(args[2])
	local athletes = stripToNil(args[3])
	games = games and ('Thế vận hội ' .. games) or 'Thế vận hội'
	local parms = {
		alias = code,
		fullName = games,
		year = games:match('%d+$'),
		games = games:gsub('%d+$ ?', ''),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	local dispName = fullName
	if fullName == 'Trung Phi' then
		fullName = 'Cộng hòa Trung Phi'
	end
	if fullName == 'Nam Phi' then
		fullName = 'Cộng hòa Nam Phi'
	end
	if fullName == 'CHDCND Triều Tiên' then
		fullName = 'Cộng hòa Dân chủ Nhân dân Triều Tiên'
	end
	if fullName == 'UAE' then
		fullName = 'Các Tiểu vương quốc Ả Rập Thống nhất'
	end
	if fullName == 'Micronesia' then
		fullName = 'Liên bang Micronesia'
	end
	if fullName == 'New Caledonia' then
		fullName = 'Nouvelle-Calédonie'
	end
	if fullName == 'CIS' then
		fullName = 'Cộng đồng các Quốc gia Độc lập'
	end
	return (('[[Tập tin:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} tại {games}|{dispName}]]{athletes}')
		:gsub('{(%w+)}', {
			athletes = athletes and
				('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
				'',
			flag = countryAlias(parms),
			games = games,
			name = fullName,
			dispName = dispName,
		}))
end

local function flagXYZ(frame)
	-- Implement {{flagIOC2}} and its variants which previously called this module three times.
	-- Returns one of four possible outputs:
	--	from flagIOC2:			<flag> <country link> <athletes>, with the third value optional
	--	from flagIOC2team:		<flag> <country link> <country alias>
	--	from flagIOC2athlete:	<flag> <athlete(s)> <country alias/link>
	--	from flagIOC2medalist:	<athlete(s)><br><flag> <country link>
	local args = frame:getParent().args
	local dispType = stripToNil(frame.args['type'])
	local code=''
	local games=''
	local athletes=''
	if dispType == 'name' or dispType == 'team' then
		code = stripToNil(args[1]) or error('Tham số 1 phải là một mã quốc gia')
		games = stripToNil(args[2]) or error('Tham số 2 phải là một tên thi đấu')
		athletes = stripToNil(args[3])
	elseif dispType == 'athlete' or dispType == 'medalist' then
		athletes = stripToNil(args[1]) or error('Tham số 1 phải là (các) tên của (các) vận động viên')
		code = stripToNil(args[2]) or error('Tham số 2 phải là một mã quốc gia')
		games = stripToNil(args[3]) or error('Tham số 3 phải là một tên thi đấu')
	end
	local dispName = stripToNil(args.name)
	
	local parms = {
		alias = code,
		fullName = games,
		year = games:match('%d+$'),
		games = games:gsub('%d+$ ?', ''),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	local dispName = stripToNil(args.name)
	if fullName == 'Trung Phi' then
		fullName = 'Cộng hòa Trung Phi'
		dispName = 'Trung Phi'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'Nam Phi' then
		fullName = 'Cộng hòa Nam Phi'
		dispName = 'Nam Phi'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'CHDCND Triều Tiên' then
		fullName = 'Cộng hòa Dân chủ Nhân dân Triều Tiên'
		dispName = 'CHDCND Triều Tiên'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'UAE' then
		fullName = 'Các Tiểu vương quốc Ả Rập Thống nhất'
		dispName = 'UAE'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'Micronesia' then
		fullName = 'Liên bang Micronesia'
		dispName = 'Micronesia'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'New Caledonia' then
		fullName = 'Nouvelle-Calédonie'
		dispName = 'New Caledonia'
		if dispType == 'team' then dispType = 'name' end
	end
	if fullName == 'CIS' then
		fullName = 'Cộng đồng các Quốc gia Độc lập'
		dispName = 'CIS'
		if dispType == 'team' then dispType = 'name' end
	end
	
	if dispType == 'name' then
		return (('[[Tập tin:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} tại {games}|{dispName}]]{athletes}')
			:gsub('{(%w+)}', {
				athletes = athletes and
					('&nbsp;<span style="font-size:90%;">(' .. athletes .. ')</span>') or
					'',
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'team' then
		return (('[[Tập tin:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} tại {games}|{dispName}]]{alias}')
			:gsub('{(%w+)}', {
				alias = ('&nbsp;<span style="font-size:90%;">(' .. code .. ')</span>'),
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'athlete' then
		return (('[[Tập tin:{flag}|22x20px|border|alt=|link=]]&nbsp;{athletes}&nbsp;<span style="font-size:90%;">([[{name} tại {games}|{dispName}]])</span>')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = code,
			}))
	elseif dispType == 'medalist' then
		return (('{athletes}<br>[[Tập tin:{flag}|22x20px|border|alt=|link=]]&nbsp;[[{name} tại {games}|{dispName}]]')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	end
end
local function main(frame)
	return countryAlias(frame.args)
end

return {
	flagIOC = flagIOC,
	flagXYZ = flagXYZ,
	main = main,
}