Module:etymology language

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:etymology language/documentation. [edit]

This module provides the functionality of the template {{etyl}}. It has a separate data module, Module:etymology language/data, which stores information for etymology-only language codes.


local export = {}
 
local EtymologyLanguage = {}
 
function EtymologyLanguage:getRawData()
	return self._rawData
end
 
function EtymologyLanguage:getCode()
	return self._code
end
 
function EtymologyLanguage:getCanonicalName()
	return self._rawData.names[1]
end
 
function EtymologyLanguage:getAllNames()
	return self._rawData.names
end
 
function EtymologyLanguage:getParentCode()
	return self._rawData.parent
end
 
function EtymologyLanguage:getWikipediaArticle()
	return self._rawData.wikipedia_article or self._rawData.names[1]
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("Module: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)
	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("Module: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() .. " terms derived from " .. info.cat_name}, lang, sort_key)
		end
	end
 
	return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
end
 
 
function get_info(source)
	-- What type of code is the source?
	if source == "und" then
		return {
			display = "undetermined",
			cat_name = "other languages"}
	end
 
	-- Is it a normal language code?
	local source_info = require("Module:languages").getByCode(source)
 
	if source_info then
		return {
			display = "[[w:" .. source_info:getCategoryName() .. "|" .. source_info:getCanonicalName() .. "]]",
			cat_name = source_info:getCanonicalName()}
	end
 
	-- Is it a family code?
	source_info = require("Module:families").getByCode(source)
 
	if source_info then
		return {
			display = "[[w:" .. source_info:getCategoryName() .. "|" .. source_info:getCanonicalName() .. "]]",
			cat_name = 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 = source_info:getCanonicalName()}
	end
 
	-- Code doesn't exist; show an error
	error("The source language/family code \"" .. source .. "\" is not valid.")
end
 
return export