Module:Language

From Zelda Wiki, the Zelda encyclopedia

The source of truth for languages relevant to The Legend of Zelda series.

This module exports the following functions.

enum

enum([options])

Parameters

Returns

Examples

#InputOutput
1
enum()
{
  "enBr",
  "ja",
  "zhT",
  "zhS",
  "cs",
  "da",
  "nl",
  "fi",
  "frC",
  "frF",
  "de",
  "hu",
  "it",
  "ko",
  "he",
  "no",
  "pl",
  "ptB",
  "ptP",
  "ru",
  "esL",
  "esS",
  "sv",
  reference = "[[Module:Language/Data]]",
}
2
enum({ includeWikiLanguage = true })
{
  "enAm",
  "enBr",
  "ja",
  "zhT",
  "zhS",
  "cs",
  "da",
  "nl",
  "fi",
  "frC",
  "frF",
  "de",
  "hu",
  "it",
  "ko",
  "he",
  "no",
  "pl",
  "ptB",
  "ptP",
  "ru",
  "esL",
  "esS",
  "sv",
  reference = "[[Module:Language/Data]]",
}

getLect

getLect(code, [options])

Parameters

Returns

  • A Lua table of information on a particular lect (a language or variant thereof), or nil if the given code does not exist in Module:Language/Data.
  • An error category if the given code does not exist in Module:Language/Data.

Examples

#InputOutputStatus
3
getLect("enBr")
{
  abbr = "English<sup>'\"`UNIQ--templatestyles-0000002C-QINU`\"'<span class=\"explain tooltip\" title=\"British\">BR</span></sup>",
  lang = "English",
  name = "British English",
  flags = {
    '<span class="tooltip" title="The United Kingdom of Great Britain and Northern Ireland">[[File:United Kingdom Flag.png|20px|The United Kingdom of Great Britain and Northern Ireland]]</span>',
  },
}
nil
Green check.svg
4
getLect("ja", { flagSize = "40px" })
{
  abbr = "Japanese",
  lang = "Japanese",
  name = "Japanese",
  flags = {
    '<span class="tooltip" title="Japan">[[File:Japan Flag.png|40px|Japan]]</span>',
  },
}
Green check.svg
nil
Green check.svg
5
getLect("zhT")
{
  abbr = "Chinese<sup>'\"`UNIQ--templatestyles-00000009-QINU`\"'<span class=\"explain tooltip\" title=\"Traditional\">TR</span></sup>",
  lang = "Chinese",
  name = "Traditional Chinese",
  flags = {
    '<span class="tooltip" title="The Republic of China">[[File:Taiwan Flag.svg|20px|The Republic of China]]</span>',
    '<span class="tooltip" title="The Hong Kong Special Administrative Region of China">[[File:Hong Kong Flag.svg|20px|The Hong Kong Special Administrative Region of China]]</span>',
    '<span class="tooltip" title="The Macao Special Administrative Region of China">[[File:Macao Flag.svg|20px|The Macao Special Administrative Region of China]]</span>',
  },
}
nil
Green check.svg
6
getLect("narnia")
nil
Green check.svg
"[[Category:Articles using invalid arguments in template calls]]"
Green check.svg

local p = {}
local Data = mw.loadData("Module:Language/Data")

local Region = require("Module:Region")

local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
local DEFAULT_FLAG_SIZE = "20px"

-- The enum is ordered according to [[Guidelines:Translations#Order_of_Languages]]
-- enAm, enBr, ja, then ordered by language name
function p.enum(options)
	local options = options or {}
	local includeWikiLanguage = options.includeWikiLanguage or false
	
	local enum = {}
	for code in pairs(Data.lects) do
		if code ~= "enAm" and code ~= "enBr" and code ~= "ja" then
			local lect = p.getLect(code)
			table.insert(enum, {
				code = code,
				name = lect.name,
				abbr = lect.abbr,
				lang = lect.lang,
			})
		end
	end

	table.sort(enum, function(a, b)
		return a.lang < b.lang
	end)

	for i, v in ipairs(enum) do
		enum[i] = v.code
	end
	table.insert(enum, 1, "ja")
	table.insert(enum, 1, "enBr")

	if includeWikiLanguage then
		table.insert(enum, 1, "enAm")
	end
	
	enum.reference = "[[Module:Language/Data]]"
	
	return enum
end

function p.getLect(code, options)
	local options = options or {}
	local flagSize = options.flagSize or DEFAULT_FLAG_SIZE
	
	local lect = Data.lects[code]
	if not lect then
		local utilsError = require("Module:UtilsError")
		utilsError.warn(string.format("Invalid language code <code>%s</code>. See [[Module:Language/Data]] for a list of supported languages.", code))
		return nil, CATEGORY_INVALID_ARGS
	end

	if lect.variant then
		name = lect.variant.name.." "..lect.lang
		local exp = mw.getCurrentFrame():expandTemplate({
			title = "Exp",
			args = {lect.variant.name, lect.variant.abbr}
		})
		abbr = lect.lang.."<sup>"..exp.."</sup>"
	else
		name = lect.lang
		abbr = lect.lang
	end
	local region = Region.getRegion(lect.flag, { flagSize = flagSize })

	return {
		abbr = abbr,
		flags = region.flags,
		lang = lect.lang,
		name = name,
	}
end

function p.Data(frame)
	local utilsLayout = require("Module:UtilsLayout")
	local utilsMarkup = require("Module:UtilsMarkup")
	
	local headers = {"Code", "Flag(s)", "Language", "{{Wp|Lect}}", "Abbr."}
	local sortable = {1, 3, 4, 5}
	local rows = {}
	for i, code in ipairs(p.enum()) do
		local lect = p.getLect(code)
		local flags = #lect.flags > 1 and utilsMarkup.list(lect.flags) or lect.flags[1]
		table.insert(rows, {"<code>"..code.."</code>", flags, lect.lang, lect.name, lect.abbr})
	end
	local wikitable = utilsLayout.table({
		headers = headers,
		sortable = sortable,
		rows = rows,
	})
	return wikitable
end

function p.Documentation(frame)
	return {
		enum = {
			params = {"options"},
			returns = "A list of language codes for use by [[Module:UtilsArg]] and [[Module:Documentation]].",
			cases = {
				outputOnly = true,
				{
					args = {},
				},
				{
					args = {
						{ includeWikiLanguage = true },
					},
				},
			},
		},
		getLect = {
			params = {"code", "options"},
			returns = {
				"A Lua table of information on a particular {{Wp|lect}} (a language or variant thereof), or <code>nil</code> if the given code does not exist in [[Module:Language/Data]].",
				"An error category if the given code does not exist in [[Module:Language/Data]].",
			},
			cases = {
				outputOnly = true,
				{
					args = {"enBr"},
					expect = {
						-- {
						-- 	abbr = 'English<sup><span class="explain" rel="tooltip" title="British" style="color:Yellow;">BR</span></sup>',
						-- 	lang = "English",
						-- 	name = "British English",
						-- 	flags = {
						-- 		'<span class="tooltip" title="The United Kingdom of Great Britain and Northern Ireland">[[File:United Kingdom Flag.png|20px|The United Kingdom of Great Britain and Northern Ireland]]</span>',
						-- 	},							
						-- },
					},
				},
				{
					args = {"ja", { flagSize = "40px" }},
					expect = {
						{
							abbr = "Japanese",
							lang = "Japanese",
							name = "Japanese",
							flags = {
								'<span class="tooltip" title="Japan">[[File:Japan Flag.png|40px|Japan]]</span>',
							},
						}
					}
				},
				{
					args = {"zhT"},
					expect = {
						-- {
						-- 	abbr = "Chinese<sup>'\"`UNIQ--templatestyles-00000009-QINU`\"'<span class=\"explain tooltip\" title=\"Traditional\">TR</span></sup>",
						-- 	lang = "Chinese",
						-- 	name = "Traditional Chinese",
						-- 	flags = {
						-- 		'<span class="tooltip" title="The Republic of China">[[File:Taiwan Flag.svg|20px|The Republic of China]]</span>',
						-- 		'<span class="tooltip" title="The Hong Kong Special Administrative Region of China">[[File:Hong Kong Flag.svg|20px|The Hong Kong Special Administrative Region of China]]</span>',
						-- 		'<span class="tooltip" title="The Macao Special Administrative Region of China">[[File:Macao Flag.svg|20px|The Macao Special Administrative Region of China]]</span>',
						-- 	},
						-- }
					}
				},
				{
					args = {"narnia"},
					expect = {
						nil,
						CATEGORY_INVALID_ARGS
					}
				},
			}
		}
	}
end

function p.Schemas(frame)
	return {
		Data = {
			type = "record",
			required = true,
			properties = {
				{
					name = "lects",
					required = true,
					type = "map",
					desc = "A list of {{Wp|lect|lects}} (languages or variants thereof) relevant to {{TLoZ|Series}}.",
					keyPlaceholder = "code",
					keys = {
						type = "string",
						desc = "A identifying code for the lect. This should be an {{Wp|ISO 639-1}} code. For variants, append a one- or two-character abbreviation.",
					},
					values = {
						type = "record",
						properties = {
							{
								name = "lang",
								required = true,
								type = "string",
								desc = "The name of the language.",
							},
							{
								name = "flag",
								required = true,
								type = "string",
								desc = "A [[Module:Region/Data|region code]] identifying a flag (or flags) to associate to the language.",
							},
							{
								name = "variant",
								type = "record",
								properties = {
									{
										name = "abbr",
										type = "string",
										required = true,
										desc = "An abbreviation of the language variant name.",
									},
									{
										name = "name",
										type = "string",
										required = true,
										desc = "A {{Wp|demonym}} representing the language variant."
									},
								},
							},
						},
					},
				},
			},
		},
		enum = {
			options = {
				type = "record",
				properties = {
					{
						name = "includeWikiLanguage",
						default = false,
						desc = "If true, the wiki's native language (<code>enAm</code>) is included in the list.",
					},
				},
			},
		},
		getLect = {
			code = {
				required = true,
				type = "string",
				desc = "A code from [[Module:Language/Data]].",
			},
			options = {
				type = "record",
				properties = {
					{
						name = "flagSize",
						type = "string",
						default = DEFAULT_FLAG_SIZE,
						desc = "An {{MediaWiki|Help:Images#Size_and_frame|image size}} for the returned flag(s).",
					},
				},
			},
		},
	}
end

return p