Module:Wikidata label

local p = {}

function p._getLabel(item, lang, link) local entity, langList, sitelink, label if type(item)=='string' then entity = mw.wikibase.getEntity(item); else entity = item end lang = lang or 'en' langList = mw.language.getFallbacksFor(lang) table.insert(langList, 1, lang) -- choose label for i, language in ipairs(langList) do 		label = entity:getLabel(language) if label then break end end if not label then label = item end

-- allow different link formats link = string.lower(link or '') if link=='-' then return label elseif link=='wikidata' then return mw.ustring.format('%s', item, label) elseif link=='commons' then sitelink = entity:getSitelink('commonswiki') if sitelink==label then return mw.ustring.format('%s', sitelink) elseif sitelink then return mw.ustring.format('%s', sitelink, label) end end -- apply default "Wikipadia" link format for i, language in ipairs(langList) do sitelink = entity:getSitelink(language .. 'wiki') if sitelink then lang = language break end end if not sitelink then return mw.ustring.format('%s', item, label) -- no wiki sitelink, so link to wikidata else return mw.ustring.format('%s', lang, sitelink, label) end end

--[[ getLabel This function returns a label translated to desired language, created based on wikidata Usage:

Parameters 1: wikidata's item's q-code (required) 2: language (optional; default ) 3: link style: "wikipedia" (default), "Wikidata", "Commons", or "-" (no link) Error Handling: Bad q-code will result in displayed error ]] function p.getLabel(frame) local args = frame.args if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language end if (not args.link) or (mw.text.trim(args.link) == "") then args.link = "wikipedia" end args.item = mw.text.trim(args.item or '') return p._getLabel(args.item, args.lang, args.link) end

return p