Module:Wdnl

Uit WikiWoordenboek

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Wdnl/doc

-- Module to implement getDescription and getLabel
-- just for nl wikitionary without language fallbacks

p = {}


-------------------------------------------------------------------------------
-- findLang takes a "langcode" parameter if supplied and valid
-- otherwise it tries to create it from the user's set language ({{int:lang}})
-- failing that it uses the wiki's content language.
-- It returns a language object
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
local findLang = function(langcode)
	local langobj
	langcode = mw.text.trim(langcode or "")
	if mw.language.isKnownLanguageTag(langcode) then
		langobj = mw.language.new( langcode )
	else
		langcode = mw.getCurrentFrame():preprocess( '{{int:lang}}' )
		if mw.language.isKnownLanguageTag(langcode) then
			langobj = mw.language.new( langcode )
		else
			langobj = mw.language.getContentLanguage()
		end
	end
	return langobj
end

-------------------------------------------------------------------------------
-- getDescription has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- (it defaults to the associated qid of the current article if omitted)
-- It returns the article description for the Wikidata entity
-- It takes a lang parameter
-- Nothing is returned if the description doesn't exist in that language.
p.getDescription = function(frame)
	local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
	local langcode = findLang(frame.args.lang).code
	local entity = mw.wikibase.getEntity(itemID)
	if not entity then return nil end
	local descriptions = entity.descriptions
	if not descriptions then return nil end
	local desc
	for k, v in pairs(descriptions) do
		if v.language == langcode then
			desc = v.value
			break
		end
	end
	return desc
end

-------------------------------------------------------------------------------
-- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- (it defaults to the associated qid of the current article if omitted)
-- It takes a lang parameter
-- It returns the Wikidata label in that language as plain text.
-- If there is no label in that language, it returns nothing.
p.getLabel = function(frame)
	local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
	local langcode = findLang(frame.args.lang).code
	local label = mw.wikibase.getLabelByLang(itemID, langcode)
	return label
end

return p