Jump to content

Module:UnitPlural

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by RexxS (talk | contribs) at 19:29, 10 October 2018 (tostring). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Module to create plurals for units (initially)
-- Might be split into code and data

--[[
Plurals by language
--]]
local plural = {
	-- English
	en = {
		["inch"] = "inches",
		["foot"] = "feet",
		["square foot"] = "square feet",
		["cubic foot"] = "cubic feet",
		["pound-force"] = "pounds-force",
		["kilogram-force"] = "kilograms-force",
		["horsepower"] = "horsepower",
		["gauss"] = "gauss",
		["solar mass"] = "solar masses",
		["hertz"] = "hertz",
		["degree Fahrenheit"] = "degrees Fahrenheit",
		["degree Celsius"] = "degrees Celsius",
		["standard gravity"] = "standard gravities",
	},
}

--[[
findLang takes a "langcode" parameter if if supplied and valid.
Otherwise it tries to create it from the user's set language ({{int:lang}})
Failing that, it uses the wiki's content language.
It returns a language object.
--]]
local function findLang(langcode)
	local langobj
	langcode = mw.text.trim(langcode or "")
	if mw.language.isKnownLanguageTag(langcode) then
		langobj = mw.language.new( langcode )
	else
		langcode = mw.getCurrentFrame():preprocess( '{{int:lang}}' )
		if mw.language.isKnownLanguageTag(langcode) then
			langobj = mw.language.new( langcode )
		else
			langobj = mw.language.getContentLanguage()
		end
	end
	return langobj
end

--[[
pl takes a unit name and an optional language code
It returns the plural of that unit in the given language, if it can.
--]]
local function pl(unit, langcode)
	langcode = findLang(langcode).code
	unit = tostring(unit) or ""
	if plural.langcode then
		return "Debug: pass"
	else
		return "Debug: fail"
	end
end

local p = {}

--[[
p.pl takes a unit name and an optional language code from the frame or its parent.
It returns the plural of that unit in the given language, if it can.
--]]
function p.pl(frame)
	if frame.args.unit then
		args = frame.args
	else
		args = frame:getParent().args
	end
	if not args.unit then return "Debug: Nothing supplied" end

	return pl(args.unit, args.lang)
end

return p