Jump to content

Module:IPA/category documentation

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nardog (talk | contribs) at 02:53, 19 September 2023 (ignore error). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')

local p = {}
local data = mw.loadData('Module:IPA/data')
local mLang = require('Module:Lang')

function p.main()
	local langName = mw.title.getCurrentTitle().text:sub(12, -5)
	local fullLangCode, langCode, regionCode, langData, dialectData
	for code, lang in pairs(data.langs) do
		if lang.name == langName then
			fullLangCode = code
			langCode = code
			langData = lang
			break
		end
		if lang.dialects then
			for diaCode, dialect in pairs(lang.dialects) do
				if dialect.name == langName then
					fullLangCode = code .. '-' .. diaCode
					langCode = code
					regionCode = diaCode
					langData = lang
					dialectData = dialect
					break
				end
			end
			if fullLangCode then
				break
			end
		end
	end
	if not fullLangCode then
		fullLangCode = mLang._tag_from_name({ langName })
		if fullLangCode:sub(1, 5) == '<span' then
			fullLangCode = nil
		else
			langCode = fullLangCode:gsub('%-.*', '')
			regionCode = fullLangCode:match('%-(.+)')
			regionCode = regionCode and regionCode:upper()
			langData = data.langs[langCode]
			if langData and langData.dialects and regionCode then
				dialectData = langData.dialects[regionCode]
			end
		end
	end
	local isCollective = langName:sub(-10) == ' languages'
	local langLink = dialectData and dialectData.link or
		langData and langData.link or
		isCollective and langName or
		langName .. ' language'
	local key = dialectData and dialectData.key or langData and langData.key
	if not key and not fullLangCode then
		local keyTitle = mw.title.new('Help:IPA/' .. langName)
		local keyRedir = keyTitle.redirectTarget
		local keyName = (keyRedir or keyTitle).fullText
		if keyName ~= 'Help:IPA' then
			key = keyName
		end
	end
	local langCat = fullLangCode and mLang._category_from_tag({ fullLangCode })
	if langCat and langCat:sub(1, 5) == '<span' then
		langCat = nil
	end
	local parentLangName = regionCode and (
		langData.name or mLang._name_from_tag({ langCode })
	)
	if parentLangName and parentLangName:sub(1, 5) == '<span' then
		parentLangName = nil
	end
	local cat = parentLangName
		and 'Pages with ' .. parentLangName .. ' IPA'
		or 'Pages with IPA'
	local ret = {
		string.format(
			'The following pages contain [[International Phonetic Alphabet|IPA]] transcriptions of [[%s|%s]] using %s. This category should never be added manually.',
			langLink,
			langName,
			fullLangCode
				and string.format('<code>{{[[Template:IPA|IPA]]|%s|...}}</code>', fullLangCode)
				or '[[Template:IPA]]'
		)
	}
	if key then
		table.insert(ret, string.format(
			'By default, the transcriptions are linked to the [[%s]] key. Transcriptions that do not adhere to the conventions of the key must have <code>|generic=yes</code>.',
			key
		))
	end
	if isCollective then
		table.insert(ret, string.format(
			'The %s are a language collective. Please replace the code%s with those of more specific languages where possible.',
			langName,
			fullLangCode and ' (' .. fullLangCode .. ')'
		))
	end
	if not fullLangCode then
		table.insert(ret, string.format(
			'<small>Documentation error: language code for "%s" not found.</small>',
			langName
		))
	end
	if langCat then
		table.insert(ret, string.format('==See also==\n*[[:%s]]', langCat))
	end
	table.insert(ret, string.format('[[Category:%s|%s]]', cat, langName))
	return table.concat(ret, '\n\n')
end

return p