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 03:41, 19 September 2023 (list und, mis, etc. at the top). 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
		if keyRedir and keyRedir.fullText ~= 'Help:IPA' then
			keyTitle = keyRedir
		end
		if keyTitle.exists and keyTitle.fullText ~= 'Help:IPA' then
			key = keyTitle.fullText
		end
	end
	local langCat, langCatCount
	if fullLangCode then
		langCat = mLang._category_from_tag({ fullLangCode })
		if langCat:sub(1, 5) == '<span' then
			langCat = nil
		else
			langCatCount = mw.site.stats.pagesInCategory(langCat:sub(10), 'pages')
		end
	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 sortkey = langName:find('^[ %l]+$') and ' ' .. langName or langName
	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 and langCatCount ~= 0 then
		table.insert(ret, string.format(
			'==See also==\n*[[:%s]] (%s)',
			langCat,
			mw.language.new('en'):formatNum(langCatCount)
		))
	end
	table.insert(ret, string.format('[[Category:%s|%s]]', cat, sortkey))
	return table.concat(ret, '\n\n')
end

return p