Difference between revisions of "Module:UnitPlural"

From blackwiki
Jump to navigation Jump to search
blackwiki>RexxS
(debug)
blackwiki>RexxS
(local u1,2)
Line 63: Line 63:
 
else
 
else
 
local per = plural[langcode].per
 
local per = plural[langcode].per
u1, u2 = unit:match("(.+) " .. per .. " (.+)")
+
local u1, u2 = unit:match("(.+) " .. per .. " (.+)")
 
if u1 then
 
if u1 then
 
-- recurse to give plural of bit before " per "
 
-- recurse to give plural of bit before " per "
-- ret = pl(u1) .. " per " .. u2
+
ret = pl(u1) .. " per " .. u2
ret = "Debug:" .. " 2=" .. (u2 or "zilch") .. " 1=" .. pl(u1, langcode) .. " per=" .. per
+
-- ret = "Debug:" .. " 2=" .. (u2 or "zilch") .. " 1=" .. pl(u1, langcode) .. " per=" .. per
 
else
 
else
 
-- standard plural
 
-- standard plural

Revision as of 20:14, 10 October 2018

Function main

The main function takes a number and unit name (|quantity=) and an optional language code (|lang=) from the frame.

It returns the quantity with proper plural units in the given language, if it can.

It will find use when the quantity is returned from Wikidata, so that the numerical value is not yet known.

Examples

  • {{#invoke:UnitPlural |main |quantity=1 week}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3 week}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3.50 week}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=1 foot}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3 foot}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=1 mile per hour}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3 mile per hour}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=1 standard gravity}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3 standard gravity}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=1 foot}} → Debug: Nothing supplied
  • {{#invoke:UnitPlural |main |quantity=3 solar mass}} → Debug: Nothing supplied

Function plural

Function plural is equivalent to function main(frame), but takes parameters for use in other modules. The langcode is optional and is "en" by default on Lua error in Module:WikidataIB at line 2966: attempt to index field 'wikibase' (a nil value)..

  • quant = plural(quant, langcode)

Function pl

Function pl returns the plural of the unit name, and takes parameters for use in other modules. The langcode is optional and is "en" by default on Lua error in Module:WikidataIB at line 2966: attempt to index field 'wikibase' (a nil value)..

  • unitnameplural = pl(unitname, langcode)



-- 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

--[[
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 ""
	local ret = ""
	if plural[langcode] then
		if plural[langcode][unit] then
			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
				-- ret = "Debug:" .. " 2=" .. (u2 or "zilch") .. " 1=" .. pl(u1, langcode) .. " per=" .. per
			else
				-- standard plural
				ret = unit .. plural[langcode][1]
			end
		end
	else
		ret = "Debug: unknown language " .. unit
	end
	return ret
end

local p = {}

--[[
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 not args.unit then return "Debug: Nothing supplied" end

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

return p