local export = {}
-- For testing
local force_cat = false
--[[ If language is an etymology language, iterates through parent languages
until it finds a non-etymology language. ]]
function export.getNonEtymological(lang)
while lang:getType() == "etymology language" do
local parentCode = lang:getParentCode()
local parent = require("Module:languages").getByCode(parentCode)
or require("Module:etymology languages").getByCode(parentCode)
or require("Module:families").getByCode(parentCode)
lang = parent
-- mw.log(terminfo.lang:getCode() .. " " .. terminfo.lang:getType())
end
return lang
end
local function term_error(terminfo)
if terminfo.lang:getType() == "family" then
if terminfo.term and terminfo.term ~= "-" then
require("Module:debug/track")("etymology/family/has-term")
end
terminfo.term = "-"
end
return terminfo
end
local function create_link(terminfo, template_name)
local link = ""
if terminfo.term == "-" then
--[=[
[[Special:WhatLinksHere/Template:tracking/cognate/no-term]]
[[Special:WhatLinksHere/Template:tracking/derived/no-term]]
[[Special:WhatLinksHere/Template:tracking/borrowed/no-term]]
[[Special:WhatLinksHere/Template:tracking/calque/no-term]]
]=]
require("Module:debug/track")(template_name .. "/no-term")
else
-- mw.log(terminfo.term)
link = " " .. require("Module:links").full_link(terminfo, "term", true)
end
return link
end
function export.process_and_create_link(terminfo, template_name)
terminfo.lang = export.getNonEtymological(terminfo.lang)
terminfo = term_error(terminfo)
return create_link(terminfo, template_name or "derived")
end
function export.get_display_and_cat_name(source, raw)
local display, cat_name
if source:getCode() == "und" then
display = "未知語言"
cat_name = "其他語言"
elseif source:getCode() == "mul" then
display = "跨語言"
cat_name = "跨語言"
elseif source:getCode() == "mul-tax" then
display = "[[w:en:taxonomic name|分類學名稱]]"
cat_name = "分類學名稱"
else
display = raw and source:getCanonicalName() or source:makeWikipediaLink()
cat_name = source:getDisplayForm()
end
return display, cat_name
end
function export.insert_source_cat_get_display(categories, lang, source, raw, nocat)
local display, cat_name = export.get_display_and_cat_name(source, raw)
if lang and not nocat then
-- Add the category, but only if there is a current language
if not categories then
categories = {}
end
if lang:getCode() == source:getCode() then
table.insert(categories, lang:getCanonicalName() .. "回借詞")
else
table.insert(categories, "派生自" .. cat_name .. "的" .. lang:getCanonicalName() .. "詞")
end
end
return display, categories
end
local function termError(terminfo)
if terminfo.lang:getType() == "family" then
if terminfo.term and terminfo.term ~= "-" then
require("Module:debug").track("etymology/family/has-term")
end
terminfo.term = "-"
end
return terminfo
end
local function createLink(terminfo, templateName)
local link = ""
if terminfo.term == "-" then
--[=[
[[Special:WhatLinksHere/Template:tracking/cognate/no-term]]
[[Special:WhatLinksHere/Template:tracking/derived/no-term]]
[[Special:WhatLinksHere/Template:tracking/borrowed/no-term]]
[[Special:WhatLinksHere/Template:tracking/calque/no-term]]
]=]
require("Module:debug").track(templateName .. "/no-term")
else
-- mw.log(terminfo.term)
link = " " .. require("Module:links").full_link(terminfo, "term", true)
end
return link
end
function export.format_etyl(lang, source, sort_key, categories, nocat)
local info = {}
-- [[Special:WhatLinksHere/Template:tracking/etymology/sortkey]]
if sort_key then
require("Module:debug").track("etymology/sortkey")
end
if not categories then
categories = {}
end
if source:getCode() == "und" then
info = {
display = "undetermined",
cat_name = "other languages",
}
elseif source:getCode() == "mul" then
info = {
display = "[[w:en:Translingualism|跨語言]]",
cat_name = "跨語言",
}
elseif source:getCode() == "mul-tax" then
info = {
display = "[[w:en:taxonomic name|分類學名稱]]",
cat_name = "分類學名稱",
}
else
info.display = source:makeWikipediaLink()
if source:getType() == "family" then
info.cat_name = source:getCategoryName()
else
info.cat_name = source:getCanonicalName()
end
end
-- Add the categories, but only if there is a current language
if lang and not nocat then
local m_utilities = require("Module:utilities")
if lang:getCode() == source:getCode() then
table.insert(categories, lang:getCanonicalName() .. "回借詞")
else
table.insert(categories, "派生自" .. info.cat_name .. "的" .. lang:getCanonicalName() .. "詞")
end
categories = m_utilities.format_categories(categories, lang, sort_key, nil, force_cat)
else
categories = ""
end
return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
end
-- Internal implementation of {{cognate|...}} template
function export.format_cognate(terminfo, sort_key)
return export.format_derived(nil, terminfo, sort_key, nil, "cognate")
end
-- Internal implementation of {{derived|...}} template
function export.format_derived(lang, terminfo, sort_key, nocat, templateName)
local source = terminfo.lang
terminfo.lang = export.getNonEtymological(terminfo.lang)
terminfo = termError(terminfo)
local link = createLink(terminfo, templateName or "derived")
return export.format_etyl(lang, source, sort_key, nil, nocat) .. link
end
local function has_ancestor(lang, otherlang, non_etym_otherlang)
-- First check if `lang` has (the non-etymology-language parent of) `otherlang` as its ancestor. If `lang` is e.g.
-- Italian and `otherlang` is Latin, this will be caught here. If `lang` is Italian and `otherlang` is Vulgar Latin
-- (an etymology-only language whose parent is Latin), this will also be caught here, as we use the
-- non-etymology-language parent, in this case Latin, when checking the ancestor hierarchy.
if lang:hasAncestor(non_etym_otherlang) then
return true
end
-- Second check for the Italian -> Old Italian case. This happens when (a) `otherlang` is etymology-only,
-- (b) `otherlang` is marked as "ancestral to parent", (c) `otherlang`'s parent is the same as `lang`. This is not
-- caught above because Italian is not ancestral to Italian.
if otherlang:getType() == "etymology language" and otherlang:isAncestralToParent() then
local otherlang_parent = export.getNonEtymological(otherlang)
if otherlang_parent and otherlang_parent:getCode() == lang:getCode() then
return true
end
end
return false
end
-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not.
local function check_ancestor(lang, otherlang, non_etym_otherlang)
if not has_ancestor(lang, otherlang, non_etym_otherlang) and mw.title.getCurrentTitle().nsText ~= "Template" then
-- Generate the non-ancestor error message.
local function showLanguage(lang)
local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
if lang:getType() == "etymology language" then
retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
showLanguage(export.getNonEtymological(lang)))
end
return retval
end
local postscript
local ancestors = lang:getAncestors()
local moduleLink = "[[Module:"
.. require("Module:languages").getDataModuleName(lang:getCode())
.. "]]"
if not ancestors[1] then
postscript = showLanguage(lang) .. " has no ancestors."
else
local ancestorList = table.concat(
require("Module:fun").map(
showLanguage,
ancestors),
" and ")
postscript = ("The ancestor%s of %s %s %s."):format(
ancestors[2] and "s" or "", lang:getCanonicalName(),
ancestors[2] and "are" or "is", ancestorList)
end
error(("%s is not set as an ancestor of %s in %s. %s")
:format(showLanguage(otherlang), showLanguage(lang), moduleLink, postscript))
end
end
-- Internal implementation of {{inherited|...}} template
function export.format_inherited(lang, terminfo, sort_key, nocat)
local source = terminfo.lang
local categories = {}
if not nocat then
table.insert(categories, "繼承自" .. source:getCanonicalName() .. "的" .. lang:getCanonicalName() .. "詞")
end
local link = export.process_and_create_link(terminfo, "inherited")
check_ancestor(lang, source, terminfo.lang)
return export.format_etyl(lang, source, sort_key, categories, nocat) .. link
end
-- Internal implementation of {{borrowed|...}} template
function export.format_borrowed(lang, terminfo, sort_key, nocap, notext, nocat, borrowing_type)
local source = terminfo.lang
terminfo.lang = export.getNonEtymological(terminfo.lang)
terminfo = termError(terminfo)
local text, way = "", "";
local categories = {}
if lang:getCode() == source:getCode() then
table.insert(categories, lang:getCanonicalName() .. "回借詞")
elseif source:getType() == "family" then
table.insert(categories, "借自" .. source:getCanonicalName() .. "的" .. lang:getCategoryName() .. "詞")
else
table.insert(categories, "借自" .. source:getCanonicalName() .. "的" .. lang:getCanonicalName() .. "詞")
end
if not notext then
if borrowing_type == "learned" then
text, way = "古典借詞,借自", "學術化借自" -- learned borrowing
elseif borrowing_type == "semi-learned" then
text, way = "半接觸借自", "半接觸借自" -- semi-learned borrowing
elseif borrowing_type == "orthographic" then
text, way = "形譯自", "形譯自" -- orthographic borrowing
elseif borrowing_type == "unadapted" then
text, way = "非同化借自", "非同化借自" -- unadapted borrowing
else
text, way = "借自", "借自" -- loanword
end
end
if borrowing_type ~= "plain" and lang:getCode() ~= source:getCode() then
-- For non-plain borrowings, insert extra category, unless lang and source
-- are the same (a twice-borrowed term).
local source_name = source:getType() == "family" and source:getCategoryName() or source:getCanonicalName()
table.insert(categories, way .. source_name .. "的" .. lang:getCanonicalName() .. "詞")
end
local link = createLink(terminfo, "borrowed")
return text .. export.format_etyl(lang, source, sort_key, categories, nocat) .. link
end
local function specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, template_name, category)
local result = pre_text
local source = terminfo.lang
terminfo.lang = export.getNonEtymological(terminfo.lang)
terminfo = termError(terminfo)
local categories = {}
if source:getType() == "family" then
category = category:gsub("SOURCE", source:getCategoryName())
else
category = category:gsub("SOURCE", source:getCanonicalName())
end
table.insert(categories, category .. "的" .. lang:getCanonicalName() .. "詞")
local link = createLink(terminfo, template_name)
result = result .. export.format_etyl(lang, source, sort_key, categories, nocat) .. link
return result
end
-- Internal implementation of {{calque|...}} template
function export.calque(lang, terminfo, sort_key, nocap, notext, nocat)
local pre_text = ""
if not notext then
pre_text = pre_text .. "[[Appendix:Glossary#calque|仿譯]]自"
end
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "calque", "仿譯自SOURCE")
end
-- Internal implementation of {{partial calque|...}} template
function export.partial_calque(lang, terminfo, sort_key, nocap, notext, nocat)
local pre_text = ""
if not notext then
pre_text = pre_text .. "[[Appendix:術語表#部分仿譯|部分仿譯]]自"
end
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "partial_calque", "部分仿譯自SOURCE")
end
-- Internal implementation of {{semantic loan|...}} template
function export.semantic_loan(lang, terminfo, sort_key, nocap, notext, nocat)
if nocap then
require("Module:debug").track("semantic_loan/nocap")
end
local pre_text = ""
if not notext then
pre_text = pre_text .. "[[Appendix:Glossary#semantic loan|意譯]]自"
end
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "semantic_loan", "意譯自SOURCE")
end
-- Internal implementation of {{phono-semantic matching|...}} template
function export.phono_semantic_matching(lang, terminfo, sort_key, nocap, notext, nocat)
if nocap then
require("Module:debug").track("phono_semantic_matching/nocap")
end
local pre_text = ""
if not notext then
-- FIXME, create entry in [[Appendix:Glossary]]
pre_text = pre_text .. "[[w:en:Phono-semantic matching|音義兼譯]]自"
end
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "phono_semantic_matching", "音義兼譯自SOURCE")
end
return export