Module:etymology languages: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Add check for language code
Line 17: Line 17:
if lang then
if lang then
lang = require("Module:languages").getLanguageByCode(lang)
lang = require("Module:languages").getLanguageByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
end
end

Revision as of 14:14, 30 March 2014


This module provides etymology-only languages. It has a separate data module, Module:etymology languages/data, which stores information for etymology-only language codes.

It exports only three functions that behave similarly to the ones in Module:languages: getByCode, getByCanonicalName, and makeObject().

However, do not use these functions directly under normal circumstances. Instead, use getByCode(code, ..., true), getByCanonicalName(name, ..., true) in Module:languages, where the third parameter indicates that etymology-only languages can be returned, and if the second parameter is non-nil, an error is thrown if the specified code or name isn't recognized.

Etymology-only language objects returned by getByCode(), getByCanonicalName(), or makeObject() or by the corresponding methods in Module:languages function, for all intents and purposes, exactly like full Language objects. In particular, they support all the same methods, meaning they can for the most part be used as drop-in replacements for full Language objects. The only significant differences are:

  1. Etymology-only languages are nested underneath a full language (or, in rare cases, a full family), i.e. their parent (or parent's parent, etc.) is a full language (or family). This can be returned using getFull(). If you need the code or canonical name of the full language or family under which a given etymology-only language is nested, it is more efficient to use getFullCode() or getFullName() rather than calling getFull():getCode() or similar. (All three methods getFull(), getFullCode() and getFullName() exist on full languages, but the first one just returns back the same object and the others are equivalent to calling getCode() and getName(), respectively.)
  2. The getParent() method on an etymology-only language returns the direct containment parent (a full language, etymology-only language or family) of the etymology-only language. When called on full languages, it returns {nil}.
  3. Some modules that accept language objects are not prepared to accept etymology-only language objects. This particularly relates to categories, because currently only full languages can form poscatboiler or topic categories beginning with a language name or code. Modules that accept etymology-only language objects must be prepared to call getFull() or similar if they construct their own category names.

local export = {}

function export.show(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	
	local source = args[1] or (NAMESPACE == "Template" and "und") or error("Parameter 1 (source language/family code) has not been specified.")
	local lang = args[2]; if lang == "" then lang = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	
	-- Empty language means English, but "-" means no language. Yes, confusing...
	if not lang then
		lang = "en"
	elseif lang == "-" then
		lang = nil
	end
	
	if lang then
		lang = require("Module:languages").getLanguageByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	end
	
	return format(source, lang, sort_key)
end


function 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").getLanguageByCode(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").getFamilyByCode(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 = mw.loadData("Module:etymology language/data")[source]
	
	if source_info then
		return {
			display = "[[w:" .. (source_info.wikipedia_article or source_info.names[1]) .. "|" .. source_info.names[1] .. "]]",
			cat_name = source_info.names[1]}
	end
	
	-- Code doesn't exist; show an error
	error("The source language/family code \"" .. source .. "\" is not valid.")
end


-- Look up an item from the table of language data, and return it.
-- This function allows templates to access this data.
-- Returns an empty string if the item does not exist.
function export.lookup_etym_language(frame)
	local languages = mw.loadData("Module:etymology language/data")
	
	local args = frame.args
	local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
	local langinfo = languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
	
	-- The item that the caller wanted to look up
	local itemname = args[2] or error("Type of information to look up has not been specified. Please pass parameter 2 to the module invocation.")
	local item = langinfo[itemname] or ""
	
	if type(item) == "table" then
		return item[tonumber(args[3] or 1)] or ""
	else
		return item or ""
	end
end


function export.etym_language_exists(frame)
	local languages = mw.loadData("Module:etymology language/data")
	
	local args = frame.args
	local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
	
	if languages[lang] then
		return "1"
	else
		return ""
	end
end

return export