Module:UtilsLanguage

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 = "" 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 .. "" .. utilsMarkup.tooltip(lang.lect.abbr, lang.lect.name, "highlight") .. ""	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 ." },		langOnly = { type = "boolean", desc = "If  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 Nomenclature. Table also includes a  property, so this can be used in template documentation and 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", ' '}, },			{				desc = "Language with regional variant.", args = {"enBr"}, expect = { 'EnglishBR', ' '				},			},			{				desc = "langOnly", args = {"enBr", true}, expect = {"English", nil}, },			{				desc = "Invalid language code", args = {"foo"}, expect = {"", ""}, },		},	}, }

return p