Module:Daterange

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

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

Code

-- Translates and format date range sand other non punctual dates
--inspired by [[Commons:Module:Other dates]]
local datemodule = require('Module:Date')
local fallback = require('Module:Fallback')
local p = {}
--[[ wikibase format
	precision = 
0: 1 Gigayear
1: 100 Megayears
2: 10 Megayears
3: Megayear
4: 100 Kiloyears
5: 10 Kiloyears
6: Kiloyear
7: 100 years
8: 10 years
9: years
10: months
11: days
12: hours
13: minutes
14: seconds	
]]-- 

local function totext(obj, lang, case)
	return datemodule.Wikibasedate(obj, lang, case)
end

local function from(startpoint, lang)
	local precision = tonumber(startpoint.precision)
	local value = fallback._langSwitch({
		ar = function() return 'من ' .. totext(startpoint, lang) end,
		bn = function() return totext(startpoint, lang) .. ' থেকে' end,
		cs = function() return 'od ' .. totext(startpoint, lang) end,
		de = function() return 'ab ' .. totext(startpoint, lang) end,
		el = function() return 'από ' .. totext(startpoint, lang) end,
		en = function() return 'from ' .. totext(startpoint, lang) end,
		et = function() return 'alates ' .. totext(startpoint, lang) end,
		fa = function() return 'از  ' .. totext(startpoint, lang) end,
		-- ? fi = // Template: {{#ifeq:{{IsNum|{{{2|}}} }}|1|vuodesta}} {{ISOdate|1={{{2|}}}|2=fi|form=ela}} alkaen
		fr = function()
			if precision > 10 then -- precision > month
				return "à partir du " .. totext(startpoint, lang)
			else 
				return "à partir de " .. totext(startpoint, lang)
			end
		end,
		gl = function() return 'desde o ' .. totext(startpoint, lang) end,
		he = function() return 'מ- ' .. totext(startpoint, lang) end,
		hr = function() return 'od ' .. totext(startpoint, lang) end,
		it = function() return 'dal ' .. totext(startpoint, lang) end,
		ja = function() return totext(startpoint, lang) .. 'から' end,
		mk = function() return 'од ' .. totext(startpoint, lang) end,
		ml = function() return totext(startpoint, lang) .. 'മുതൽ'	 end,
		nds = function() return 'vonaf ' .. totext(startpoint, lang) end,
		nl = function() return 'vanaf ' .. totext(startpoint, lang) end,
		no = function() return 'fra ' .. totext(startpoint, lang) end,
		pl = function() return 'od '  .. totext(startpoint, lang, 'gen') end,
		qu = function() return totext(startpoint, lang) .. " p'unchawmanta" end,
		ro = function() return 'din ' .. totext(startpoint, lang) end,
		ru = function() return 'С ' .. totext(startpoint, lang, 'gen') end,
		sl = function() return 'od ' .. totext(startpoint, lang, 'gen') end, 
		sv = function() return 'från ' .. totext(startpoint, lang) end,
		ur = function() return 'از  ' .. totext(startpoint, lang) end,
		vi = function() return 'từ ' .. totext(startpoint, lang) end,
		}, lang)
	return value()
end

local function fromuntil(startpoint, endpoint, lang)
	local precision = tonumber(startpoint.precision) -- may need 2 precisions for start and end
	local value = fallback._langSwitch({
		ar = function() return  ' من ' .. totext(startpoint, lang) .. ' إلى ' end,
		bn = function() return totext(startpoint, lang) .. ' থেকে '.. totext(endpoint, lang) .. ' পর্যন্ত'end,
		ca = function() return totext(startpoint, lang)  .. ' fins ' .. totext(endpoint, lang) end,
		cs = function() return totext(startpoint, lang) .. ' až '.. totext(endpoint, lang) end,
		da = function() return 'fra ' .. totext(startpoint, lang) .. ' til '.. totext(endpoint, lang) end,
		de = function() return totext(startpoint, lang) .. ' bis '.. totext(endpoint, lang) end,
		el = function() return 'από ' .. totext(startpoint, lang) .. ' bis '.. totext(endpoint, lang) end,
		en = function() return 'from ' .. totext(startpoint, lang) .. ' to '.. totext(endpoint, lang) end,
		es = function() return 'de ' .. totext(startpoint, lang) .. ' a '.. totext(endpoint, lang) end,
		et = function() return 'fra ' .. totext(startpoint, lang) .. ' kuni '.. totext(endpoint, lang) end,
		fa = function() return ' از ' .. totext(startpoint, lang) .. ' تا ' end,
		fi = function() return totext(startpoint, lang) .. '-'.. totext(endpoint, lang) end,
		fr = function() -- could actually be better should be "d'octobre à mars 2013' not "de octore 2013 à mars 2013" 
			if precision > 10 then -- precision > month
				return "du " .. totext(startpoint, lang) .. " au " ..  totext(endpoint, lang)
			else
				-- "DE septebmbre" / "D'octobre"
				return require('Module:Linguistic').of(totext(startpoint, 'fr'), 'fr') .. " à " .. totext(endpoint, lang)
			end
		end,
		gl = function() return 'de ' .. totext(startpoint, lang) .. ' a '.. totext(endpoint, lang) end,
		he = function() return 'מ- ' .. totext(startpoint, lang) .. ' עד '.. totext(endpoint, lang) end,
		hr = function() return 'od ' .. totext(startpoint, lang) .. ' do '.. totext(endpoint, lang) end,
		hu = function() return totext(startpoint, lang) .. ' - '.. totext(endpoint, lang) end,
		it = function() return 'dal ' .. totext(startpoint, lang) .. ' al '.. totext(endpoint, lang) end,
		ja = function() return totext(startpoint, lang) .. 'から'.. totext(endpoint, lang) .. 'まで'end,
		mk = function() return 'од ' .. totext(startpoint, lang) .. ' до '.. totext(endpoint, lang) end,
		nds = function() return totext(startpoint, lang) .. ' bet '.. totext(endpoint, lang) end,
		nl = function() return totext(startpoint, lang) .. ' tot '.. totext(endpoint, lang) end,
		no = function() return totext(startpoint, lang) .. ' til '.. totext(endpoint, lang) end,
		pl = function() return 'od ' .. totext(startpoint, lang, 'gen') .. ' do '.. totext(endpoint, lang) end,
		pt = function() return 'de ' .. totext(startpoint, lang) .. ' a '.. totext(endpoint, lang) end,
		qu = function() return totext(startpoint, lang) .. " p'unchawmanta ".. totext(endpoint, lang) .. " p'unchawkama" end,
		ro = function() return  'din '.. totext(startpoint, lang) .. ' până în '.. totext(endpoint, lang) end,
		ru = function() return 'c ' .. totext(startpoint, lang, 'gen') .. ' до '.. totext(endpoint, lang, 'gen') end,
		sl = function() return 'od ' .. totext(startpoint, lang, 'gen') .. ' do '.. totext(endpoint, lang, 'gen') end,
		sv = function() return 'från ' .. totext(startpoint, lang) .. ' till '.. totext(endpoint, lang) end,
		uk= function() return 'з ' .. totext(startpoint, lang, 'gen') .. ' до '.. totext(endpoint, lang, 'gen') end,
		ur = function() return ' از ' .. totext(startpoint, lang) .. ' تا ' end,
		vi = function() return 'từ ' .. totext(startpoint, lang) .. ' đến '.. totext(endpoint, lang) end,
		zh = function() return '从' .. totext(startpoint, lang) .. '到'.. totext(endpoint, lang) end,
		['zh-hant'] = function() return '從' .. totext(startpoint, lang) .. '到'.. totext(endpoint, lang) end,
		default = function() return totext(startpoint, lang) .. '-'.. totext(endpoint, lang) end,
		}, lang)
	return value()
end

local function upto(endpoint, lang)
	local precision = tonumber(endpoint.precision) -- may need 2 precisions for start and end
	local value = fallback._langSwitch({
		bn = function() return totext(endpoint, lang) .. ' পর্যন্ত' end,
		en = function() return 'until '.. totext(endpoint, lang) end,
		fr = function()
			if precision > 10 then -- precision > month
				return "jusqu’au " .. totext(endpoint, lang)
			else 
				return "jusqu’à " .. totext(endpoint, lang)
			end
		end
		}, lang)
	return value()
end

function p.wikibasedaterange(timedata, lang, case)
	local startpoint, endpoint, timepoint = timedata.startpoint, timedata.endpoint, timedata.timepoint
	if startpoint and endpoint then
		return fromuntil(startpoint, endpoint, lang)
	end
	if startpoint then
		return from(startpoint, lang)
	end
	if endpoint then
		return upto(endpoint, lang)
	end
	if timepoint then
		return datemodule.Wikibasedate(timepoint, lang)
	end
end

function p.compactdaterange(timedata, lang) -- "YXXX–YXXX" format
	local startera, endera, startyear, endyear
	local pattern1, pattern2 = "(%W)(%d+)%-(%d+)%-(%d+)", "(%d+)%-(%d+)%-(%d+)"
	if timedata.startpoint and timedata.startpoint.time then
		startera, startyear = timedata.startpoint.time:match(pattern1)
		startyear = startyear or timedata.startpoint.time:match(pattern2)
		startyear = startyear and tostring(tonumber(startyear), lang)
	end
	if timedata.endpoint and timedata.endpoint.time then 
		endera, endyear = timedata.endpoint.time:match(pattern1)
		endyear = endyear or timedata.endpoint.time:match(pattern2)
		endyear = endyear and tostring(tonumber(endyear), lang)
	end
	return (startyear or '?') .. '–' .. (endyear or '?')
end
return p