Module:Labels

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

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

Code

local p = {}
local fb = require('Module:Fallback')


local function getLabelImpl(id, lang)
    local entity = mw.wikibase.getEntityObject(id)
    if entity then
        local label = entity:getLabel(lang)
        if label then
            return label
        end

        for i, j in pairs (mw.language.getFallbacksFor(lang)) do
            label = entity:getLabel(j)
            if label then
                return label
            end
        end
    end

    return id
end


function p.linkify(frame)
    local args = frame.args
    return p._linkify(args)
end
    
function p._linkify(args)
	if mw.text.trim( args.entities ) == '' then
		return
	end
    local ids  = mw.text.split(args.entities:gsub(" ", ""), ",")
    local lang = args.lang

    local labels = {}
    for i = 1, #ids do
    	local pagename = ids[i]
    	if pagename:sub(1,1) == 'P' then
    		pagename = 'Property:' .. pagename
    	end
        labels[i] = "[[" .. pagename .. "|" .. getLabelImpl(ids[i], lang) .. " <small>(" .. ids[i] .. ")</small>]]"
    end

    return table.concat(labels, ", ")
end


return p