Module:etymology language

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

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:getCode()
	return self._code
end
 
 
function EtymologyLanguage:getCanonicalName()
	return self._rawData.names[1]
end
 
 
function EtymologyLanguage:getOtherNames()
	if not self._otherNames then
		self._otherNames = {}
 
		for i, val in ipairs(self._rawData.names) do
			if i > 1 then
				table.insert(self._otherNames, val)
			end
		end
	end
 
	return self._otherNames
end
 
 
function EtymologyLanguage:getAllNames()
	return self._rawData.names
end
 
 
function EtymologyLanguage:getCategoryName()
	return self:getCanonicalName()
end
 
 
function EtymologyLanguage:getType()
	return "etymology language"
end
 
 
function EtymologyLanguage:getParentCode()
	return self._rawData.parent
end
 
 
function EtymologyLanguage:getWikipediaArticle()
	return self._rawData.wikipedia_article or self._rawData.names[1]
end
 
 
function EtymologyLanguage:toJSON()
	local ret = {
		canonicalName = self:getCanonicalName(),
		categoryName = self:getCategoryName(),
		code = self._code,
		otherNames = self:getOtherNames(),
		parent = self._rawData.parent,
		type = self:getType(),
		}
 
	return require("Module:JSON").toJSON(ret)
end
 
 
function EtymologyLanguage:getRawData()
	return self._rawData
end
 
 
EtymologyLanguage.__index = EtymologyLanguage
 
 
function export.makeObject(code, data)
	return data and setmetatable({ _rawData = data, _code = code }, EtymologyLanguage) or nil
end
 
 
function export.getByCode(code)
	return export.makeObject(code, mw.loadData("Module:etymology language/data")[code])
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