Module:Lang links

Revision as of 20:15, 21 July 2024 by wikimedia>Tacsipacsi (use Module:Arguments to get Template:lle parameters, otherwise they’re not found (frame.args is the parameters passed by the wrapper templates, which are always none); probably needed for the other function as well, but I don’t feel like figuring it out and testing it)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Lang links/doc

 --  __  __           _       _        _                        _ _       _        
 -- |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _  | (_)_ __ | | _____ 
 -- | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | '_ \ / _` | | | | '_ \| |/ / __|
 -- | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| | | | | | | |   <\__ \
 -- |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, | |_|_|_| |_|_|\_\___/
                                                     -- |___/                      
require('strict') -- used for debugging purposes as it detects cases of unintended global variables

local p = {}

local function lang_list()
	-- This list was generated with help of query https://quarry.wmcloud.org/query/83535 which
	-- collected all the sub-templates on commons. That list was cross checked against a list of
	-- language codes supported by MediaWiki. Afterwards each language was given a rank based on 
	-- how many subtemplates do we have for that language code. Maximum rank is 231.
	-- This list is sorted by the language code (see https://en.wikipedia.org/wiki/Wikipedia:Language_order_poll)
	return {'af/72', 'ak/180', 'als/79', 'am/163', 'an/110', 'ang/201', 'ar/10', 'arq/106', 
		'ary/164', 'arz/107', 'as/105', 'ast/81', 'av/202', 'az/58', 'azb/203', 'ba/181', 'ban/150', 
		'bar/47', 'bcl/117', 'be/66', 'be-tarask/33', 'be-x-old/165', 'bg/42', 'bgn/137', 'bh/182', 
		'bho/144', 'bjn/204', 'blk/142', 'bn/21', 'bo/205', 'br/73', 'bs/87', 'ca/26', 'ce/108', 
		'ceb/138', 'ch/166', 'ckb/121', 'co/183', 'cs/17', 'cu/167', 'cy/69', 'da/34', 'dag/114', 
		'de/4', 'de-at/101', 'de-ch/100', 'de-formal/59', 'dga/206', 'diq/64', 'dsb/207', 'dv/184', 
		'el/40', 'en/1', 'en-ca/80', 'en-gb/71', 'en-us/208', 'eo/35', 'es/6', 'et/44', 'eu/49', 
		'ext/185', 'fa/29', 'fi/19', 'fo/209', 'fr/3', 'frr/168', 'fy/83', 'ga/75', 'gd/151', 'gl/30', 
		'grc/210', 'gsw/103', 'gu/74', 'ha/131', 'haw/169', 'he/38', 'hi/41', 'hr/36', 'hsb/118', 
		'ht/170', 'hu/14', 'hy/52', 'ia/143', 'id/31', 'ie/211', 'ig/145', 'io/88', 'is/86', 'it/12', 
		'ja/9', 'jbo/152', 'jv/124', 'ka/55', 'kaa/132', 'kcg/146', 'khw/212', 'kjp/213', 'kk/78', 
		'kk-cyrl/147', 'km/97', 'kn/85', 'ko/27', 'ko-kp/111', 'ksh/156', 'ku/90', 'ku-arab/214', 
		'ku-latn/215', 'ky/98', 'la/112', 'lb/57', 'lmo/157', 'lo/158', 'lt/54', 'lv/62', 'lzh/153', 
		'mai/216', 'mg/92', 'mi/171', 'min/154', 'mk/2', 'ml/22', 'mn/148', 'mnw/186', 'mr/65', 'ms/48', 
		'ms-arab/217', 'mt/82', 'mwl/139', 'my/67', 'myv/187', 'mzn/122', 'nah/159', 'nan/188', 'nap/125', 
		'nb/45', 'nds/46', 'nds-nl/218', 'ne/76', 'new/219', 'nl/7', 'nl-informal/189', 'nn/56', 'no/51', 
		'nso/190', 'ny/140', 'oc/77', 'or/128', 'os/160', 'pa/126', 'pam/191', 'pap/220', 'pcd/172', 
		'pih/221', 'pl/13', 'pms/222', 'ps/93', 'pt/8', 'pt-br/23', 'qu/161', 'rm/141', 'ro/39', 'ru/5', 
		'rw/173', 'sah/192', 'sc/134', 'scn/24', 'sco/115', 'sd/119', 'se/102', 'sh/129', 'si/96', 
		'simple/116', 'sk/50', 'sl/18', 'sm/193', 'smn/223', 'sn/162', 'so/123', 'sq/63', 'sr/43', 
		'sr-ec/224', 'sr-el/225', 'st/194', 'stq/226', 'su/127', 'sv/16', 'sw/120', 'syl/174', 'szl/135', 
		'ta/68', 'tcy/130', 'te/109', 'tg/104', 'tg-cyrl/195', 'th/37', 'tk/196', 'tl/70', 'tn/227', 
		'to/228', 'tok/197', 'tpi/175', 'tr/28', 'tt/99', 'tt-cyrl/229', 'ug/198', 'uk/11', 'ur/61', 
		'uz/84', 'vec/95', 'vi/32', 'vo/176', 'wa/230', 'war/155', 'wuu/177', 'xh/199', 'yi/133', 
		'yo/91', 'yue/94', 'zgh/231', 'zh/15', 'zh-classical/178', 'zh-cn/60', 'zh-hans/20', 'zh-hant/25', 
		'zh-hk/89', 'zh-mo/149', 'zh-my/136', 'zh-sg/113', 'zh-tw/53', 'zh-yue/200', 'zu/179'}
end

function p.lang_links(frame)
	local title, subpage
	local output = {}
    local page    = frame.args[1]
    local max_num = tonumber(frame.args[2] or '70')
	if (page) then
		title = mw.title.new(page)
	else
		title = mw.title.getCurrentTitle()
		page  = tostring( title.basePageTitle )
	end
	
	-- if /lang subpage found transclude it and we are done
	subpage = page .. '/lang'
	if mw.title.new(subpage).exists then
		return frame:expandTemplate{ title = subpage }
	end

	local list = lang_list()
	local output = {}
	for _, key in pairs( list ) do
		local v = mw.text.split( key, '/' )
		local lang = v[1]
		local rank = tonumber(v[2])
		if rank<=max_num then
			subpage = page .. '/' .. lang
			if mw.title.new(subpage).exists then
				local lname = mw.language.fetchLanguageName( lang, lang )
				table.insert(output, '[[' .. subpage .. '|<bdi lang="'..lang..'">'.. lname..'</bdi>]]')
			elseif lang=='en' then -- some pages have English version in the main template. Assume that is the case if /en subpage is missing.
				table.insert(output, '[[' .. page .. '|<bdi lang="en">English</bdi>]]')
			end
		end
	end 
	
	list = table.concat(output, '&nbsp;<b>∙</b>&#32;')
	return '<span class="plainlinks" style="font-size:small;line-height:1.4">'.. list.. '</span>'
end

function p.lang_links_external(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local title, subpage, doc
	local output = {}
    local page    = args[1]
    local max_num = tonumber(args[2] or '500')
	if (page) then
		title = mw.title.new(page)
	else
		title = mw.title.getCurrentTitle()
		page  = tostring( title.basePageTitle )
	end
	doc = '<noinclude>\n\n[[Category:Language link templates|' .. tostring( title.baseText ) .. ']]\n</noinclude>'

	local list = lang_list()
	local output = {}
	for _, key in pairs( list ) do
		local v = mw.text.split( key, '/' )
		local lang = v[1]
		local rank = tonumber(v[2])
		if rank<=max_num then
			subpage = page .. '/' .. lang
			title = mw.title.new(subpage)
			if title.exists then
				local lname = mw.language.fetchLanguageName( lang, lang )
				table.insert(output, '[' .. title:fullUrl() .. ' <bdi class="language lang-'..lang..'" lang="'..lang..'">'.. lname..'</bdi>]')
			end
		end
	end 
	title = mw.title.new(page .. '/lang')
	table.insert(output, '{{edit|' .. title.prefixedText .. '}}')
	list = table.concat(output, '&nbsp;<b>∙</b>&#32;<!--\n-->')
	return '<span style="font-size:small;line-height:1.4" class="plainlinks noprint"><!--\n-->'.. list.. '<!--\n--></span>' .. doc
end

return p