Module:Languages/table

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

local p = {}
local langs = require("Module:Languages")
local config = mw.loadData("Module:Languages/config")

local function setFromList(list)
	local set = {}
	for i, key in ipairs(list) do
		set[key] = true
	end
	return set
end

local listedCodes = setFromList(config.languageCodes)
local unlistedCodes = setFromList(config.minorLanguageCodes)

--- Quickly uppercases the first character of the string, disregarding Unicode.
local function ucfirst(s)
	return s:sub(1, 1):upper() .. s:sub(2, -1)
end

local languages = {}
local languageDetails = {}
for code, name in pairs(config.languageNamesByCode) do
	local status
	if listedCodes[code] then
		status = "Listed"
	elseif unlistedCodes[code] then
		status = "Unlisted"
	elseif config.deprecatedLanguageCodes[code] then
		status = "Deprecated"
	end
	
	if status then
		table.insert(languages, code)
	end
	languageDetails[code] = {
		code = code,
		nativeName = name,
		namespace = config.namespacesByLanguage[code],
		status = status,
		replacementCode = config.deprecatedLanguageCodes[code],
	}
end
for oldCode, newCode in pairs(config.deprecatedLanguageCodes) do
	if not config.languageNamesByCode[oldCode] then
		table.insert(languages, oldCode)
		languageDetails[oldCode] = mw.clone(languageDetails[newCode])
		languageDetails[oldCode].status = "Deprecated"
		languageDetails[oldCode].replacementCode = newCode
	end
end
table.sort(languages)

function getNamesByCode(lang)
	local namesByCode = mw.language.fetchLanguageNames(lang, "all")
	namesByCode["sr-cyrl"] = namesByCode["sr-ec"]
	namesByCode["sr-latn"] = namesByCode["sr-el"]
	return namesByCode
end

function p.json(frame)
	local namesByCode = getNamesByCode("en", "all")
	for code, details in pairs(languageDetails) do
	    details["name"] = namesByCode[code]
	end
	return mw.text.jsonEncode(languageDetails)
end

function p.table(frame)
	local currentTitle = mw.title.getCurrentTitle()
	local currentLang = langs.languageFromTitle(currentTitle)
	
	local namesByCode = getNamesByCode(currentLang)
	
	local rows = {}
	for i, code in ipairs(languages) do
		local details = languageDetails[code]
		
		local styleRules = {}
		if details.namespace then
			table.insert(styleRules, "font-weight: bold")
		end
		if details.status and details.status == "Deprecated" then
			table.insert(styleRules, "text-decoration: line-through")
		end
		
		local name = namesByCode[code] or details.nativeName
		local wikidataTitle = "d:Special:ItemByTitle/enwiki/ISO 639:" .. code:gsub("-.*", "")
		local namespaceoid = details.namespace or ucfirst(code)
		local replacement = details.replacementCode and
			"→ <code>" .. details.replacementCode .. "</code>" or ""
		
		local statusTemplate = {Listed="Yes", Unlisted="Partial", Deprecated="No"}
		
		local cells = {
			"<code>" .. code .. "</code>",
			"[[" .. wikidataTitle .. "|" .. name .. "]]",
			"[[w:" .. code .. ":|" .. details.nativeName .. "]]",
			"[[Special:PrefixIndex/" .. namespaceoid .. ":|" .. namespaceoid .. ":]]",
			details.status and frame:expandTemplate {
				title = statusTemplate[details.status],
				args = {
					(frame.args[details.status] or details.status) .. replacement,
				}
			} or "",
		}
		
		local style = #styleRules > 0 and " style='" .. table.concat(styleRules, "; ") .. "'" or ""
		table.insert(rows, "|-" .. style .. "\n| " .. table.concat(cells, " || "))
	end
	return table.concat(rows, "\n")
end

return p