July 25, 2020

👭 Knight Challenge #11 👬

Want to try your hand at these challenges? There's a couple of things you can do!
From writing, to research, to images, find your preferred way to contribute with our eleventh theme: Couples!

Latest Announcements

Module:UtilsLanguage

From Zelda Wiki, the Zelda encyclopedia
Jump to: navigation, search

enum

enum()

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.

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.
printLanguage("ja")
"Japanese"
Japanese
Green check.svg
'<span title="Japan" class="explain">[[File:Japan Flag.png|20px|Japan]]</span>'
Japan
Green check.svg
Language with regional variant.
printLanguage("enBr")
'English<sup><span title="British" class="explain" style="color:yellow">BR</span></sup>'
EnglishBR
Green check.svg
'<span title="United Kingdom" class="explain">[[File:United Kingdom Flag.png|20px|United Kingdom]]</span>'
United Kingdom
Green check.svg
langOnly
printLanguage("enBr", true)
"English"
English
Green check.svg
nil
Green check.svg
Invalid language code
printLanguage("foo")
"[[Category:Pages with Invalid Arguments]]"
Green check.svg
"[[Category:Pages with Invalid Arguments]]"
Green check.svg

local p = {}
local h = {}

local utilsLayout = require("Module:UtilsLayout")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsRegion = require("Module:UtilsRegion")
local utilsTable = require("Module:UtilsTable")
local utilsValidate = require("Module:UtilsValidate")

local data = mw.loadData("Module:UtilsLanguage/Data")
local languages = utilsTable.keyBy(data.languages, "code")

function p.enum()
	local codes = utilsTable.map(data.languages, "code")
	codes.reference = "[[Module:UtilsLanguage/Data]]"
	return codes
end

function p.printLanguage(code, langOnly)
	local langCodes = p.enum()
	langCodes.reference = "[[Module:UtilsLanguage/Data]]"
	local err = utilsValidate.enum(langCodes)(code, "code")
	if err then
		local cat = "[[Category:Pages with Invalid Arguments]]"
		return cat, cat
	end
	
	local lang = languages[code]
	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.Data(frame)
	local tableData = {
		sortable = true,
		headers = {"Code", "Language", "Flag"},
		rows = {}
	}
	for i, code in ipairs(p.enum()) 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 = {
					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,
								},
							},
						},
					}
				}
			}
		}
	},
	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."
		}
	}
}

p.Documentation = {
	enum = {
		params = {},
		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]].",
	},
	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>'
				},
			},
			{
				desc = "langOnly",
				args = {"enBr", true},
				expect = {"English", nil},
			},
			{
				desc = "Invalid language code",
				args = {"foo"},
				expect = {"[[Category:Pages with Invalid Arguments]]", "[[Category:Pages with Invalid Arguments]]"},
			},
		},
	},
}

return p