ናብ ትሕዝቶ ኪድ

ሞድዩል:Convert

ካብ ዊኪፐድያ፣ ናጻ ኢንሳይክሎፐድያ

Documentation for this module may be created at ሞድዩል:Convert/doc

local p  = {}
local ti = mw.language.new('ti')

local tableElement = require('Module:Tables').element
local toLink       = require('Module:Wikidata/Formats').toLink
local round        = require('Module:Math')._round

-- Constants
local units = {
	['Q794261'] = {
		['label']    = 'ኩቢክ ሜተር ኣብ ካልኢት',
		['abbreviation'] = 'ኩቢክ ሜተር ኣብ ካልኢት',
		['unit change'] = {
		},
		['unit SI'] = true,
	},	
	['Q25343'] = {
		['label']    = 'ትርብዒት ሜተር',
		['abbreviation'] = 'ትርብዒት ሜተር',
		['unit change'] = {
			{'Q712226', 1000000, '>'}, -- ትርብዒት ኪ.ሜ.
			{'Q35852', 10000, '>'} -- ሄክታር
		},
		['unit SI'] = true,
	},	
	['Q712226'] = {
		['label']    = 'ትርብዒት ኪሎሜተር',
		['abbreviation'] = 'ትርብዒት ኪ.ሜ.',
		['conversionSI'] = { 1000000, 'Q25343'}, -- 1 ትርብዒት ኪ.ሜ. = 1000 000 ትርብዒት ሜተር
		['unit SI'] = true,
	},
	['Q35852']  = {
		['label']    = 'ሄክታር',
		['abbreviation'] = 'ሄክታር',
		['conversionSI'] = {10000, 'Q25343'}, -- 1 ሄክታር = 10.000 ትርብዒት ሜተር
		['unit SI'] = true,
	},
	['Q81292'] = {
		['label'] = 'ኤክር',
		['link']    = 'ኤክር',
		['plural']    = 'ኤክራት',
		['conversionSI'] = { 4046.8564224, 'Q25343'} -- 1 ኤክር = 4046,8564224 ትርብዒት ሜተር
	},
	['Q232291'] = {
		['label']    = 'ትርብዒት ማይል',
		['abbreviation'] = 'ትርብዒት ማይል',		
		['conversionSI'] = {2589988.110336, 'Q25343'} -- 1 ትርብዒት ማይል = 2 589 988,110336 ትርብዒት ሜተር
	},
	['Q216795'] = {
		['label']    = 'ዱናም',
		['conversionSI'] = {1000, 'Q25343'}
	},
	['Q23931109'] = {
		['label']    = 'ሜትሪክ ዱናም',
		['conversionSI'] = {1000, 'Q25343'}
	},
    -- Length units
	['Q11573'] = {
		['label']    = 'ሜተር',
		['abbreviation'] = 'ሜተር',
		['unit change'] = {
			{'Q828224', 1000, '>'}, -- ኪሎሜተር
		},
		['unit SI'] = true,
	},
	['Q1811'] = {
		['label']    = 'ስነ ኮኾባዊ ኣሃዱ',
		['abbreviation'] = 'ስነ ኮኾባዊ ኣሃዱ',
		['conversionSI'] = { 149597870691, 'Q11573'}, -- 1 ስነ ኮኾባዊ ኣሃዱ = 149 597 870 691 ሜተር
		['unit SI'] = true,
	},
	['Q12129'] = {
		['label']    = 'ፓርሰክ',
		['abbreviation'] = 'ፓርሰክ',
		['conversionSI'] = {30856775814913672.7891393795779647161073192116040917980140401922770232921869992968698321353388065559933270120238005882778325, 'Q11573'},
		['unit SI'] = true,
	},
	['Q531'] = {
		['label']    = 'ዓመት ብርሃን',
		--['abbreviation'] = 'mi',
		['plural']    = 'ዓመታት ብርሃን',		
		['conversionSI']  = {9460730472580800 , 'Q11573'}, -- 1 ዓመት ብርሃን = 9460730472580800  ሜተር
		['unit SI'] = true,
	},

	['Q93318'] = {
		['label']    = 'ማይል ባሕሪ',
		--['abbreviation'] = 'mi',
		['conversionSI']  = {1852 , 'Q11573'}, -- 1 ማይል ባሕሪ = 1852  ሜተር
		--['unit SI'] = true,
	},

	['Q253276'] = {
		['label']    = 'ማይል',
		['abbreviation'] = 'ማይል',
		['conversionSI']  = {1609.344, 'Q11573'} -- 1 ማይል = 1609,344 ሜተር
	},
	['Q828224'] = {
		['label']    = 'ኪሎሜተር',
		['abbreviation'] = 'ኪ.ሜ.',
		['conversionSI'] = { 1000, 'Q11573'}, -- 1 ኪሎሜተር = 1000 ሜተር	
		['unit SI'] = true,
	},
	['Q3710'] = {
		['label']    = 'ጫማ',
		--['abbreviation'] = 'ft',
		['conversionSI']  = {0.3048006, 'Q11573'}, -- 1 ጫማ = 0,3048006 ሜተር
		['link']    = 'ጫማ (ኣሃዱ)',
	},
	['Q218593'] = {
		['label']    = 'ኢንች',
		--['abbreviation'] = 'in',
		['conversionSI']  = {0.0254, 'Q11573'} -- 1 ኢንች = 0,0254 ሜተር
	},
	['Q174728'] = {
		['label']    = 'ሰንቲሜተር',
		['abbreviation'] = 'ሰንቲሜተር',
		['conversionSI']  = {0.01, 'Q11573'}, -- 1 ሰንቲሜተር = 0.01 ሜተር
		['unit SI'] = true,
	},
	['Q174789'] = {
		['label']    = 'ሚሊሜተር',
		['abbreviation'] = 'ሚሊሜተር',
		['conversionSI']  = {0.001, 'Q11573'}, -- 1 ሚሊሜተር = 0.001 ሜተር
		['unit SI'] = true,		
	},
	['Q175821'] = {
		['label']    = 'ማይክሮሜተር',
		['abbreviation'] = 'ማይክሮሜተር',
		['conversionSI']  = {0.000001, 'Q11573'}, -- 1 ማይክሮሜተር = 0.000001 ሜተር
		['unit SI'] = true,		
	},
	['Q178674'] = {
		['label']    = 'ናኖሜተር',
		['abbreviation'] = 'ናኖሜተር',
		['conversionSI']  = {0.000000001, 'Q11573'}, -- 1 ናኖሜተር = 0,000000001 ሜተር
		['unit SI'] = true,		
	},
    -- Mass units
	['Q11570'] = {
		['label']    = 'ኪሎግራም',
		['abbreviation'] = 'ኪሎግራም',
		--['unit change'] = {
		--	{'Q828224', 1000, '>'}, -- ኪሎግራም
		--},
		['unit SI'] = true,
	},
}

local function formatMagnitude(amount, unit, options)
	if not unitConversion and unit['unit change'] then
		for k, v in pairs( unit['unit change'] ) do
			if amount >= v[2] then
				return formatMagnitude(amount / v[2], units[v[1]], options)
			end
		end
	end
	
	--if true then return require('Module:Tables').tostring(options) end
	
	local precision = options['precision'] or 2
	
	unit.link = unit.link or unit.label

	--if true then return require('Module:Tables').tostring(options) end
	
	-- Round to two decimal places, format and add the linked unit if applicable
	if unit.abbreviation then
		return ti:formatNum(round(amount, precision)) .. ' ' .. toLink(unit.link, unit.abbreviation, nil, options)		
	elseif amount == 1 or not unit.plural then
		return ti:formatNum(round(amount, precision)) .. ' ' .. toLink(unit.link, unit.label, nil, options)
	else
		return ti:formatNum(round(amount, precision)) .. ' ' .. toLink(unit.link, unit.plural, nil, options)
	end
end

function p.showValue(value, originalUnit, options)
	if not value then
		return
	end
	
	if not originalUnit then
		return value
	end
	
	options = options or {}
	
	--if true then return value end
	
	local originalUnitData = units[originalUnit]
	
	if not originalUnitData then
		originalUnitData = {}
		local entity = mw.wikibase.getEntityObject(originalUnit)
		--if true then return require('Module:Tables').tostring(entity) end
		originalUnitData.label = tableElement(entity, 'labels', 'ti', 'value')
		originalUnitData.link   = tableElement(entity, 'sitelinks', 'tiwiki', 'title')
		originalUnitData.conversionSI = {
			   [1]=tonumber((tableElement(entity,'claims','P2370',1,'mainsnak','datavalue','value','amount'):gsub('+','') or ''))
			 , [2]=tableElement(entity,'claims','P2370',1,'mainsnak','datavalue','value','unit'):gsub('^.-/(Q%d+)$', '%1')
		}
		
		--if true then return require('Module:Tables').tostring(originalUnitData) end
	end
	
	--if true then return require('Module:Tables').tostring(options) end
	
	local conversionSI = originalUnitData.conversionSI
	
	if options.unitConversion and options.unitConversion ~= '' then
	elseif not conversionSI then
		return formatMagnitude(tonumber(value), originalUnitData, options)
	elseif conversionSI and originalUnitData['unit SI'] then
		return formatMagnitude(tonumber(value) * conversionSI[1], units[conversionSI[2]], options)
	else
		return formatMagnitude(tonumber(value), originalUnitData, options) .. 
		' (' .. formatMagnitude(tonumber(value) * conversionSI[1], units[conversionSI[2]], options) .. ')'		
	end
	
end
	
function p.show(frame)
	local args = frame.args
	
	return p.showValue(args[1], args[2], {unitConversion=args[3], ['precision']=args[4], disp=args.disp,link=args.link})
end

return p