Module:I18n/objects

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code behind {{I18n/objects}}. See documentation there.

Relies on:

Code

local wikidata_label = require('Module:Wikidata label')._getLabel
local LookupTable = mw.loadData('Module:I18n/objects/data')

local p = {}

function p.simpleObject(object, lang)
	local wikitext = ''
	local item = LookupTable[string.lower(object)] -- try to find an item in the dictionary
	if item then
	-- if an item was found use it
		wikitext = wikidata_label(item, lang)
	elseif object and object ~= '' then
	-- set maintenance category and fall back to the input string
		wikitext = '[[Category:Unsupported object|' .. object .. ']]' .. object
	end
	return wikitext, item
end

function p._object(object, relObject, lang)
	local relText, relItem, relQs
	local wikitext, mainItem = p.simpleObject(object, lang)
	if relObject and relObject ~= '' then
		relText, relItem = p.simpleObject(relObject, lang)
		wikitext = string.format('%s (%s)', wikitext, relText)
	end
	-- create non-visible language independent marking in QuickStatements-like format
	local qsText = ''
	local qs = ''
	if mainItem then
		if relItem then
			relQs = ',P642,' .. relItem -- property "of" (P642), as a qualifier to P31
		else
			relQs = ''
		end
		qsText = '<div style="display: none;">object_type QS:P31,' .. mainItem .. relQs .. '</div>'
		 -- property "instance of" (P31)
	end
	return wikitext .. qsText
end

function p.object(frame)
	local args = frame.args
	return p._object(args.object, args.relObject, args.lang)
end

return p