Modul:etymology language

Vun Wiktionary
Dissen Vörlaag hef dokumentatsie neudig.
Maak de dokumentatsie veur dissen modul ovver t doel en gebroek op de dokumentatsieziede.

This module is what backs {{etyl}} (q.v.). It most likely should not be used except by {{etyl}}. (?)


local export = {}

local EtymologyLanguage = {}

function EtymologyLanguage:getRawData()
	return self._rawData
end

function EtymologyLanguage:getCode()
	return self._code
end

function EtymologyLanguage:getType()
	return "etymology language"
end

function EtymologyLanguage:getCanonicalName()
	return self._rawData.canonicalName
end

function EtymologyLanguage:getOtherNames()
	return self._rawData.otherNames or {}
end
 
function EtymologyLanguage:getCategoryName()
	return self:getCanonicalName()
end
 
function EtymologyLanguage:getParentCode()
	return self._rawData.parent
end

function EtymologyLanguage:getWikipediaArticle()
	return self._rawData.wikipedia_article or self._rawData.canonicalName
end


EtymologyLanguage.__index = EtymologyLanguage

--[[	The object cache implements memoisation, and is used to avoid duplication
		of objects. If you request the same family code twice, you should also get
		the same object twice, not two different objects with identical data.
		It might also speed things up a bit.	--]]
local object_cache = {}

function export.getByCode(code)
	if object_cache[code] then
		return object_cache[code]
	end
	
	local rawData = mw.loadData("Modul:etymology language/data")[code]
	
	if not rawData then
		return nil
	end
	
	local object = setmetatable({ _rawData = rawData, _code = code }, EtymologyLanguage)
	object_cache[code] = object
	return object
end


function export.format(source, lang, sort_key, i)
	local info = get_info(source)
	
	-- Add the categories, but only if there is a current language
	local categories = ""
	
	if lang then
		local m_utilities = require("Modul:utilities")
		
		categories = {}
		
		if lang:getCode() == source then
			categories = m_utilities.format_categories({lang:getCanonicalName() .. " twice-borrowed terms"}, lang, sort_key)
		else
			categories = m_utilities.format_categories({lang:getCanonicalName() .. " Begrepen ut " .. info.cat_name}, lang, sort_key)
		end
	end
	
	-- zugefügt und geändert, damit 30 Tochtersprachen kategoriesiert werden können, ohne dass die Ursprungssprache mehr als 1 x in der Zeile, in der die Vörlaag:etyl aufgerufen wird, ausgegeben

	if i == "" then
		return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
	else
		if i == 2 then
			return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
		else
			return "<span class=\"etyl\">" .. categories .. "</span>"
		end
	end
end


function get_info(source)
	-- What type of code is the source?
	if source == "und" then
		return {
			display = "undetermined",
			cat_name = "annere Spraken"}
	end
	
	-- Is it a normal language code?
	local source_info = require("Modul:languages").getByCode(source)
	
	if source_info then
		return {
			display = "[[w:" .. source_info:getCategoryName() .. "|" .. source_info:getCanonicalName() .. "]]",
			cat_name = "dat " .. source_info:getCanonicalName()}
	end
	
	-- Is it a family code?
	source_info = require("Modul:families").getByCode(source)
	
	if source_info then
		return {
			display = "[[w:" .. source_info:getCategoryName() .. "|" .. source_info:getCanonicalName() .. "]]",
			cat_name = "de " .. source_info:getCategoryName()}
	end
	
	-- Is it an etymology-only code?
	source_info = export.getByCode(source)
	
	if source_info then
		return {
			display = "[[w:" .. source_info:getWikipediaArticle() .. "|" .. source_info:getCanonicalName() .. "]]",
			cat_name = "dat " .. source_info:getCanonicalName()}
	end
	
	-- Code doesn't exist; show an error
	error("The source language/family code \"" .. source .. "\" is not valid.")
end

return export