Module:labels

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

This module supports Module:labels/templates, which in turn is used by the template {{label}}. See Module:labels/data and its submodules Module:labels/data/regional and Module:labels/data/topical for lists of defined labels, and for labels that are aliases (or "redirects") for other labels.

Many implementation details of this module are subject to change in the near future, so it should be considered under construction. This only affects the internals of the module and of the labels themselves, not the way it is used from within entries.

The module takes a list of labels and will process them as follows:

  1. If the label is listed in Module:labels/data or one of its submodules as an alias of another label, replace its name with the name it redirects to, and continue with the next steps.
  2. If the label is defined in Module:labels/data as a label, use that, as long as the label is not restricted to particular languages by the "language" or "languages" fields.
  3. Otherwise, just show the label's name unaltered.

Testcases

A label specific to "grc" (Ancient Greek)
code result
{{label|grc|Attic}} (Attic)
{{label|en|Attic}} (Attic)

Conflicts

labels
  • Advanced Mandarin
  • African American Vernacular English
  • Akhmimic
  • Anglo-Norman
  • Ararat
  • Australian English
  • Balhae
  • Balkar
  • Beginning Mandarin
  • Birmingham
  • Bohairic
  • Bokmål
  • Border Scots
  • Canadian English
  • Canadian spelling
  • Cantonese
  • Chakavian
  • Cham Albanian
  • Cockney
  • Cois Fharraige
  • Dari
  • De'kwana
  • Digor
  • East and West Flanders
  • East Anglian English
  • Eastern Armenian
  • Eastern Catalan
  • East Frisian
  • East Germanic
  • East Midlands English
  • East Palatine
  • Ecclesiastical Latin
  • Ekavian
  • Elementary Mandarin
  • European French
  • Fayyumic
  • Gan
  • Geordie
  • Gheg
  • Gottscheerisch
  • Guardiol
  • Hakka
  • Hamburgisch
  • Hamshen
  • Helsinki slang
  • Hessian
  • Hulu Pahang
  • Ikavian
  • Indian English
  • Ingilo
  • Inner Mbugu
  • Intermediate Mandarin
  • Inuktun
  • Inuttut
  • Irish English
  • Iron
  • Itivimuit
  • Kajkavian
  • Kansai
  • Karabakh
  • Karachay
  • Kölsch
  • Kromanti
  • Late Latin
  • Latin America
  • Lorraine Franconian
  • Louisiana French
  • Lowlands Scots
  • Low Prussian
  • Lycopolitan
  • Maastrichtian
  • Mecklenburgisch Low German
  • Medieval Hebrew
  • Medieval Latin
  • Midlands English
  • Min
  • Min Bei
  • Min Dong
  • Min Nan
  • Min Zhong
  • Mishnaic Hebrew
  • Mistralian
  • Mooring
  • Moselle Franconian
  • Multicultural London English
  • New Latin
  • New Zealand English
  • Non-Oxford British spelling
  • Normal Mbugu
  • Northern Dutch
  • Northern England English
  • Northern Finnic
  • North Germanic
  • Northwest Germanic
  • Nynorsk
  • Old East Norse
  • Old West Norse
  • Oxford British spelling
  • Oxyrhynchite
  • Palatine German
  • Pennsylvania Dutch English
  • Pomeranian Low German
  • Potteries
  • Puter
  • Ripuarian
  • Rumantsch Grischun
  • Sahidic
  • Scottish English
  • Shapsug
  • Shopski
  • Silla
  • Sistani
  • Skiri Pawnee
  • South African English
  • Southern Dutch
  • Southern England English
  • Southern US English
  • South Scots
  • Surmiran
  • Sursilvan
  • Sussex
  • Sutsilvan
  • Switzerland and Liechtenstein spelling
  • Tosk
  • Tyneside
  • Ulster Scots
  • Vallader
  • Vivaro-Alpine
  • West Arabian
  • West Country English
  • Western Armenian
  • Western Catalan
  • Western Malayo-Polynesian
  • Western Pomeranian Low German
  • Western Samic
  • West Germanic
  • West Midlands English
  • Wu
  • Xiang
  • Ye'kwana

local m_labeldata = mw.loadData("Module:labels/data")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")

local export = {}

local function show_categories(data, lang, script, sort_key, script2, sort_key2, term_mode)
	local categories = {}
	local categories2 = {}
	
	local lang_code = lang:getCode()
	local canonical_name = lang:getCanonicalName()
	
	local topical_categories = data.topical_categories or {}
	local sense_categories = data.sense_categories or {}
	local pos_categories = data.pos_categories or {}
	local regional_categories = data.regional_categories or {}
	local plain_categories = data.plain_categories or {}
	
	for i, cat in ipairs(topical_categories) do
		table.insert(categories, lang_code .. ":" .. cat)
		
		if script then
			table.insert(categories, lang_code .. ":" .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, lang_code .. ":" .. cat .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(sense_categories) do
		cat = (term_mode and cat .. " terms" ) or "terms with " .. cat .. " senses"
		table.insert(categories, canonical_name .. " " .. cat)
		
		if script then
			table.insert(categories, canonical_name .. " " .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, canonical_name .. " " .. cat .. " in " .. script2 .. " script")
		end
	end

	for i, cat in ipairs(pos_categories) do
		table.insert(categories, canonical_name .. " " .. cat)
		
		if script then
			table.insert(categories, canonical_name .. " " .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, canonical_name .. " " .. cat .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(regional_categories) do
		table.insert(categories, cat .. " " .. canonical_name)
		
		if script then
			table.insert(categories, cat .. " " .. canonical_name .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, cat .. " " .. canonical_name .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(plain_categories) do
		table.insert(categories, cat)
		
		if script then
			table.insert(categories, cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, cat .. " in " .. script2 .. " script")
		end
	end
	
	return m_utilities.format_categories(categories, lang, sort_key) .. m_utilities.format_categories(categories2, lang, sort_key2)
end

function export.show_labels(labels, lang, script, script2, sort_key, sort_key2, nocat, term_mode)
	if #labels < 1 then
		if mw.title.getCurrentTitle().nsText == "Template" then
			labels = {"example"}
		else
			error("You must specify at least one label.")
		end
	end
	
	-- Show the labels
	local omit_preComma = false
	local omit_postComma = true
	local omit_preSpace = false
	local omit_postSpace = true
	
	local lang_code = lang:getCode()
	local canonical_name = lang:getCanonicalName()
	
	local alias
	
	for i, label in ipairs(labels) do
		omit_preComma = omit_postComma
		omit_postComma = false
		omit_preSpace = omit_postSpace
		omit_postSpace = false
		
		local deprecated = false
		if m_labeldata.deprecated[label] then
			deprecated = true
		end
		if m_labeldata.aliases[label] then
			alias = label
			label = m_labeldata.aliases[label]
		end
		if m_labeldata.deprecated[label] then
			deprecated = true
		end
		
		local data = m_labeldata.labels[label] or {}
		
		if data.track then
			require("Module:debug").track("labels/label/" .. label)
		end
		
		local languages = data.languages
		if languages and not languages[lang_code] then
			-- [[Special:WhatLinksHere/Template:tracking/labels/incorrect-language]]
			require("Module:debug").track("labels/incorrect-language")
			mw.log(lang_code .. ":" .. label)
			--[[	Do not use the data in the table if the current language
					is not in the "languages" list.	]]
			label = alias or label
			data = {}
		end
		
		if data.special_display then
			local function add_language_name(str)
				if str == "canonical_name" then
					return canonical_name
				else
					return ""
				end
			end
			
			label = mw.ustring.gsub(data.special_display, "<([^>]+)>", add_language_name)
		else
			if data.glossary then
				glossary_entry = type(data.glossary) == "string" and data.glossary or label
				label = "[[Appendix:Glossary#" .. glossary_entry .. "|" .. label .. "]]"
			elseif data.Wikipedia then
				Wikipedia_entry = type(data.Wikipedia) == "string" and data.Wikipedia or label
				label = "[[w:" .. Wikipedia_entry .. "|" .. label .. "]]"
			else
				label = data.display or label
			end
		end
		
		local omit_comma = omit_preComma or data.omit_preComma
		omit_postComma = data.omit_postComma
		local omit_space = omit_preSpace or data.omit_preSpace
		omit_postSpace = data.omit_postSpace
		
		if deprecated then
			label = '<span class="deprecated-label">' .. label .. '</span>'
			if not nocat then
				label = label .. m_utilities.format_categories({ "Entries with deprecated labels" }, lang)
			end
		end
		
		label = (omit_comma and "" or '<span class="ib-comma">,</span>') .. (omit_space and "" or "&#32;") .. label
		
		labels[i] = label .. (nocat and "" or show_categories(data, lang, script, sort_key, script2, sort_key2, term_mode))
	end
	
	return
		"<span class=\"ib-brac\">(</span><span class=\"ib-content\">" ..
		table.concat(labels, "") ..
		"</span><span class=\"ib-brac\">)</span>"
end

function export.printConflicts(frame)
	local replacements = m_labeldata.replacements
	
	output = {"; labels:"}
	
	local labels = {}
	
	for label, data in pairs(replacements.labels) do
		local lang
		if data.languages then
			langCode = data.languages[1]
			lang = require("Module:languages").getByCode(langCode)
		end
		
		local canonicalName
		if lang then
			canonicalName = lang.getCanonicalName()
		end
		
		table.insert(labels, label)
		
		if langCode or canonicalName then
			table.insert(output, " (")
		end
		
		if langCode then
			table.insert(output, "<code>" .. langCode .. "</code>")
		end
		
		if canonicalName then
			if langCode then
				table.insert(output, ", ")
			end
			
			table.insert(output, canonicalName)
		end
		
		if langCode or canonicalName then
			table.insert(output, ")")
		end
	end
	
	table.sort(labels)
	
	for _, label in ipairs(labels) do
		table.insert(output, "\n* " .. label)
	end
	
	return table.concat(output)
end

return export