Module:Intersect categories

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage[edit]

Logic for {{Intersect categories}}

{{#invoke:Intersect categories|intersect_categories}}
require('Module:Intersect categories')._intersect_categories


Code

require('strict')

local getArgs = require('Module:Arguments').getArgs
local langSwitch = require('Module:LangSwitch')._langSwitch
local error_message = require('Module:Error')['error']

local p = {}

local function translate_icon(lang, targetLang)
	if lang == targetLang then
		return ''
	else
		return ' [[File:Translation Aあ.svg|32x10px|link=https://commons.wikimedia.org/w/index.php?title=Module:Intersect_categories&action=edit]]'
	end
end

function p._intersect_categories(args)
	local cat1 = args[1]
	local cat2 = args[2]
	local frame = mw.getCurrentFrame()
	local lang = frame:callParserFunction('int', 'lang') -- get user's chosen language 
	
	if not cat1 or not cat2 then
		return error_message({langSwitch({
			['en'] = frame:preprocess('Incorrect syntax. The correct syntax is <syntaxhighlight lang="wikitext" inline>{{Intersect categories|Category A|Category B}}</syntaxhighlight>.')
		}, lang)})
	end
	
	local cat1link = '[[:Category:' .. mw.ustring.upper(mw.ustring.sub(cat1, 1, 1)) .. mw.ustring.sub(cat1, 2) .. ']]'
	local cat2link = '[[:Category:' .. mw.ustring.upper(mw.ustring.sub(cat2, 1, 1)) .. mw.ustring.sub(cat2, 2) .. ']]'
	
	local text_table = {
		['en'] = "Files which are both '''directly''' in " .. cat1link .. " and '''directly''' in " .. cat2link .. " should be moved to this category." .. translate_icon(lang, 'en'),
		['fr'] = "Les fichiers qui sont à la fois '''directement''' dans " .. cat1link .. "  et '''directement''' dans " .. cat2link .. " doivent être déplacés vers cette catégorie." .. translate_icon(lang, 'fr'),
		['nl'] = "Bestanden die zowel '''direct''' in " .. cat1link .. " als '''direct''' in " .. cat2link .. " staan, moeten naar deze categorie worden verplaatst." .. translate_icon(lang, 'nl'),
		['pl'] = "Pliki, które są jednocześnie '''bezpośrednio''' w " .. cat1link .. " i '''bezpośrednio''' w " .. cat2link .. ", powinny zostać przeniesione do tej kategorii." .. translate_icon(lang, 'pl'),
		['pt'] = "Os ficheiros que estão '''diretamente''' na " .. cat1link .. " e '''diretamente''' na " .. cat2link .. " devem ser movidos para esta categoria." .. translate_icon(lang, 'pt'),
		['sl'] = "Datoteke, ki so '''neposredno''' v " .. cat1link .. " in '''neposredno''' v " .. cat2link .. ", je treba prestaviti v to kategorijo." .. translate_icon(lang, 'sl')
	}
	
	return frame:expandTemplate {
		['title'] = 'Mbox',
		['args'] = {
			['style'] = args['mbox-style'],
			['type'] = args['mbox-type'] or 'move',
			['lang'] = lang,
			['image'] = '[[File:Venn diagram for subtractive RG color.svg|50px|link=Template:Intersect categories]]',
			['text'] = langSwitch(text_table, lang)
		}
	}
end

function p.intersect_categories(frame)
	return p._intersect_categories(getArgs(frame))
end

return p