Jump to content

Module:Zh

From Simple English Wikipedia, the free encyclopedia
Revision as of 16:39, 25 April 2014 by JohnBlackburne (talk | changes) (fix error)

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

local p = {}
 
local t1st = {
	["228 Incident"] = true,
	["Bao'an County"] = true,
	["Chinese calendar"] = true,
	["Lippo Centre, Hong Kong"] = true,
	["Republic of China"] = true,
	["Republic of China at the 1924 Summer Olympics"] = true,
	["Taiwan"] = true,
	["Taiwan (island)"] = true,
	["Taiwan Province"] = true,
	["Wei Boyang"] = true,
}
 
local labels = {
	["c"] = "Chinese",
	["s"] = "simplified Chinese",
	["t"] = "traditional Chinese",
	["h"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Cantonese Yale",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Zhuyin Fuhao",
	["l"] = "literally",
}

local wlinks = {
	["c"] = "Chinese language",
	["s"] = "simplified Chinese characters",
	["t"] = "traditional Chinese characters",
	["h"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Yale romanization of Cantonese",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Bopomofo",
}

local ISOlang = {
	["c"] = "zh",
	["t"] = "zh-Hant",
	["s"] = "zh-Hans",
}

 
function p.Zh(frame)
	local pframe = frame:getParent()
	local args = pframe.args
	return p._Zh(args)
end
	
function p._Zh(args)
	local no = args["links"] == "no" -- whether to add links
 
	local t1 -- whether traditional Chinese characters go first
	if (args["first"] == nil) then
		local title = mw.title.getCurrentTitle()
		t1 = t1st[title.text] == true
	else
		t1 = args["first"] == "t"
	end
	-- based on setting/preference specify order
	local orderlist
	if (t1) then
		orderlist = {"c", "t", "s", "h", "tp", "w", "j", "cy", "poj", "zhu", "l"}
	else
		orderlist = {"c", "s", "t", "h", "tp", "w", "j", "cy", "poj", "zhu", "l"}
	end
	
	-- rewrite rules. Rules to change parameters based on others
	-- hp an alias for p ([hanyu] pinyin)
	if (args["hp"]) then
		args["p"] = args["hp"]
	end
	-- Treat Simplified + Traditional as Chinese if they're the same
	if (args["s"] == args["t"]) then
		args["c"] = args["s"]		
		args["s"] = nil
		args["t"] = nil
	end

	local body = "" -- the output string

	-- go through all possible fields in loop, adding them to the output
	for i, name in ipairs(orderlist) do
		if (args[name]) then
			local val = args[name]
			if (val ~= "") then
			
				if (ISOlang[name]) then
					val = p.lang(ISOlang[name], val)
				end
			
				if (no) or name == "l" then
					body = body .. labels[name] .. ": " .. val .. "; "
				else
					body = body .. "[[" .. wlinks[name] .. "|" .. labels[name] .. "]]: " .. val .. "; "
				end
			end
		end
	end
	
	if (body > "") then -- check for empty string
		return string.sub(body, 1, -3) -- chop off final semicolon and space
	else --no named parameters; see if there's an unnamed first parameter
		c = args[1]
		if (c) then
			if (c > "") then -- if there is treat it as Chinese
				c = p.lang("zh", c)
				if (no) then -- handle "links=no"
					return "Chinese: " .. c
				else 
					return "[[Chinese language|Chinese]]: " .. c
				end
		end
	end
		return body
	end
end
 
function p.lang(language, contents)
	local displayName = mw.language.fetchLanguageName( language, "en" )
	if (displayName ~= "Chinese") then
		local langobj = mw.getLanguage("en")
		displayName = langobj:lcfirst(displayName)
	end
	local params = {lang=language}
	params["xml:lang"] = language
	local cats = "[[Category:Articles containing " .. displayName .. "-language text]]"
	return mw.text.tag({name="span",attr=params, content=contents}) .. cats
end
 
 
return p