Jump to content

Module:UnitPlural: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m StraussInTheHouse moved page Module:RexxS/Plural to Module:Sandbox/RexxS/Plural without leaving a redirect: Per consensus at 875507098.
export function for use in other modules
Line 1: Line 1:
--[[
-- Module to create plurals for units (initially)
Module to create plurals for units (initially)
-- Might be split into code and data
Might be split into code and data
--]]


--[[
--[[
Line 49: Line 51:
return langobj
return langobj
end
end

local p = {}


--[[
--[[
pl takes a unit name and an optional language code
p.pl takes a unit name and an optional language code
It returns the plural of that unit in the given language, if it can.
It returns the plural of that unit in the given language, if it can.
it is exported for use in other modules.
--]]
--]]
local function pl(unit, langcode)
function p.pl(unit, langcode)
langcode = findLang(langcode).code
langcode = findLang(langcode).code
unit = tostring(unit) or ""
unit = tostring(unit) or ""
Line 79: Line 84:
return ret
return ret
end
end

local p = {}


--[[
--[[
Line 95: Line 98:
if not args.unit then return "" end
if not args.unit then return "" end


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



Revision as of 13:49, 14 October 2019

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

--[[
Plurals by language
--]]
local plural = {
	-- English
	en = {
		-- standard suffix, and "per":
		"s",
		["per"] = "per",
		-- irregular plurals:
		["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

local p = {}

--[[
p.pl takes a unit name and an optional language code
It returns the plural of that unit in the given language, if it can.
it is exported for use in other modules.
--]]
function p.pl(unit, langcode)
	langcode = findLang(langcode).code
	unit = tostring(unit) or ""
	local ret = ""
	if plural[langcode] then
		if plural[langcode][unit] then
			-- irregular plural from lookup
			ret = plural[langcode][unit]
		else
			local per = plural[langcode].per
			local u1, u2 = unit:match("(.+) " .. per .. " (.+)")
			if u1 then
				-- recurse to give plural of bit before " per "
				ret = pl(u1) .. " per " .. u2
			else
				-- standard plural
				ret = unit .. plural[langcode][1]
			end
		end
	else
		-- unknown language, so return unchanged
		ret = unit
	end
	return ret
end

--[[
p.main 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.main(frame)
	if frame.args.unit then
		args = frame.args
	else
		args = frame:getParent().args
	end
	-- if nothing supplied, return nothing (or an error message if debugging)
	if not args.unit then return "" end

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

return p