Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
This module exports the following functions.

enum

enum([options])

Parameters

Returns

  • Array of language codes. The array order represents the order that the languages are to be listed by tables such as Nomenclature. Table also includes a reference property, so this can be used in template documentation and validation.

Examples

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

printLanguage

printLanguage(code, [langOnly])

Parameters

  • code
    One of the language codes returned by enum().
  • [langOnly]
    If true returns only the name of the language, without any associated lect or flag.

Returns

  • Display text for the language. Includes a tooltip note when rendering a lect such as British English or Brazilian Portuguese.
  • Flag thumbnail representing the language or lect.

Examples

#InputOutputResultStatus
Language with no regional variant.
3
printLanguage("ja")
"Japanese"
Japanese
Green check
'<span title="Japan" class="explain">[[File:Japan Flag.png|20px|Japan]]</span>'
Japan
Green check
Language with regional variant.
4
printLanguage("enBr")
Expected
'English<sup><span title="British" class="explain" style="color:yellow">BR</span></sup>'
Actual
'English<sup><span title="British" class="explain" style="color:gold">BR</span></sup>'
EnglishBR
TFH Red Link desperate
'<span title="United Kingdom" class="explain">[[File:United Kingdom Flag.png|20px|United Kingdom]]</span>'
United Kingdom
Green check
5
printLanguage("enAm")
Expected
'English<sup><span title="American" class="explain" style="color:yellow">AM</span></sup>'
Actual
'English<sup><span title="American" class="explain" style="color:gold">AM</span></sup>'
EnglishAM
TFH Red Link desperate
'<span title="North America" class="explain">[[File:United States of America Flag.png|20px|North America]]</span>'
North America
Green check
langOnly
6
printLanguage("enBr", true)
"English"
English
Green check
nil
Green check
Invalid language code
7
printLanguage("foo")
"[[Category:Articles Using Invalid Arguments in Template Calls]]"
Green check
"[[Category:Articles Using Invalid Arguments in Template Calls]]"
Green check

printLect

printLect(code, [options])

Parameters

Returns

  • The full name of the given lect, or simply the language name if the language code has no lect associated to it.

Examples

#InputOutputResultStatus
8
printLect("enBr")
"British English"
British English
Green check
9
printLect("enAm")
"American English"
American English
Green check
10
printLect("enBr", { langFirst = true })
"English (British)"
English (British)
Green check
11
printLect("pl")
"Polish"
Polish
Green check
12
printLect("invalid")
nil
Green check
13
printLect(nil)
nil
Green check

local p = {}
local h = {}

local utilsError = require("Module:UtilsError")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsRegion = require("Module:UtilsRegion")
local utilsTable = require("Module:UtilsTable")

local Constants = mw.loadData("Module:Constants/Data")
local Data = mw.loadData("Module:UtilsLanguage/Data")

function p.enum(options)
	options = options or {}
	local languages = Data.languages
	if options.includeWikiLanguage then
		languages = utilsTable.concat({Data.wikiLanguage}, languages)
	end
	local codes = utilsTable.map(languages, "code")
	codes.reference = "[[Module:UtilsLanguage/Data]]"
	return codes
end

function p.printLanguage(code, langOnly)
	local lang = Data.languageLookup[code]
	if not lang then
		utilsError.warn(string.format("Invalid language code <code>%s</code>. For a list of supported language codes, see [[Module:UtilsLanguage/Data]].", code))
		local cat = "[[Category:"..Constants.category.invalidArgs.."]]"
		return cat, cat
	end
	if langOnly then
		return h.text(lang, langOnly)
	end
	return h.text(lang), utilsRegion.flag(lang.region)
end

function h.text(lang, langOnly)
	local text = lang.name
	if not langOnly and lang.lect then
		text = text .. "<sup>" .. utilsMarkup.tooltip(lang.lect.abbr, lang.lect.name, "highlight") .. "</sup>"
	end
	return text
end

function p.printLect(code, options)
	if not code then
		return code
	end
	options = options or {}
	
	local lang = Data.languageLookup[code]
	if not lang then
		return nil
	elseif lang.lect then
		if options.langFirst then
			return string.format("%s (%s)", lang.name, lang.lect.name)
		else
			return string.format("%s %s", lang.lect.name, lang.name) 
		end
	else
		return lang.name
	end
end

function p.Data(frame)
	local utilsLayout = require("Module:UtilsLayout")
	
	local tableData = {
		sortable = true,
		headers = {"Code", "Language", "Flag"},
		rows = {}
	}
	for i, code in ipairs(p.enum({includeWikiLanguage = true})) do
		local text, flag = p.printLanguage(code)
		table.insert(tableData.rows, {code, text, flag})
	end
	return utilsLayout.table(tableData)
end

p.Schemas = {
	Data = {
		type = "record",
		properties = {
			{
				name = "flagSize",
				type = "string",
				required = true,
				flagSize = "string",
			},
			{
				name = "languages",
				type = "array",
				required = true,
				items = {
					_ref = "#/definitions/language"
				}
			},
			{
				name = "wikiLanguage",
				_ref = "#/definitions/language"
			},
			{
				name = "languageLookup",
				type = "map",
				keys = {
					type = "string"
				},
				values = {
					_ref = "#/definitions/language",
					_hideSubkeys = true,
				},
			},
		},
		definitions = {
			language = {
				type = "record",
				properties = {
					{
						name = "code",
						type = "string",
						required = true,
					},
					{
						name = "name",
						type = "string",
						required = true,
					},
					{
						name = "region",
						type = "string",
						required = true,
						enum = utilsRegion.enum(),
						desc = "The region associated to the language or lect. Must be a valid code from [[Module:UtilsRegion/Data]]",
					},
					{
						name = "lect",
						type = "record",
						properties = {
							{
								name = "abbr",
								type = "string",
								required = true,
							},
							{
								name = "name",
								type = "string",
								required = true,
							},
						},
					},
				}
			}
		}
	},
	enum = {
		options = {
			type = "record",
			properties = {
				{
					name = "includeWikiLanguage",
					type = "boolean",
					desc = "If <code>true</code>, the array of languages will also include American English as the first entry.",
				},
			},
		},
	},
	printLanguage = {
		code = {
			type = "string",
			required = true,
			desc = "One of the language codes returned by <code>enum()</code>."
		},
		langOnly = {
			type = "boolean",
			desc = "If <code>true</code> returns only the name of the language, without any associated lect or flag."
		}
	},
	printLect = {
		code = {
			type = "string",
			required = true,
			desc = "A language code.",
		},
		options = {
			type = "record",
			properties = {
				{
					name = "langFirst",
					type = "boolean",
					desc = "If <code>true</code>, print the language first, then the lect in parentheses.",
				},
			},
		},
	},
}

p.Documentation = {
	enum = {
		params = {"options"},
		returns = "Array of language codes. The array order represents the order that the languages are to be listed by tables such as [[Template:Nomenclature|Nomenclature]]. Table also includes a <code>reference</code> property, so this can be used in [[Module:Documentation/Template|template documentation]] and [[Module:UtilsArg|validation]].",
		cases = {
			{
				args = {}
			},
			{
				args = { { includeWikiLanguage = true } }
			},
		},
	},
	printLanguage = {
		params = {"code", "langOnly"},
		returns = {
			"Display text for the language. Includes a tooltip note when rendering a lect such as British English or Brazilian Portuguese.",
			"Flag thumbnail representing the language or lect.",
		},
		cases = {
			{
				desc = "Language with no regional variant.",
				args = {"ja"},
				expect = {"Japanese", '<span title="Japan" class="explain">[[File:Japan Flag.png|20px|Japan]]</span>'},
			},
			{
				desc = "Language with regional variant.",
				args = {"enBr"},
				expect = {
					'English<sup><span title="British" class="explain" style="color:yellow">BR</span></sup>', 
					'<span title="United Kingdom" class="explain">[[File:United Kingdom Flag.png|20px|United Kingdom]]</span>'
				},
			},
			{
				args = {"enAm"},
				expect = {
					'English<sup><span title="American" class="explain" style="color:yellow">AM</span></sup>', 
					'<span title="North America" class="explain">[[File:United States of America Flag.png|20px|North America]]</span>'
				}
			},
			{
				desc = "langOnly",
				args = {"enBr", true},
				expect = {"English", nil},
			},
			{
				desc = "Invalid language code",
				args = {"foo"},
				expect = {"[[Category:Articles Using Invalid Arguments in Template Calls]]", "[[Category:Articles Using Invalid Arguments in Template Calls]]"},
			},
		},
	},
	printLect = {
		params = {"code", "options"},
		returns = {
			"The full name of the given lect, or simply the language name if the language code has no lect associated to it.",
		},
		cases = {
			{
				args = {"enBr"},
				expect = {"British English"},
			},
			{
				args = {"enAm"},
				expect = {"American English"},
			},
			{
				args = {"enBr", { langFirst = true }},
				expect = {"English (British)"},
			},
			{
				args = {"pl"},
				expect = {"Polish"},
			},
			{
				args = {"invalid"},
				expect = {nil},
			},
			{
				args = {nil},
				expect = {nil},
			},
		},
	}
}

return p