Module:I18nmaintenance

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code

-- mostly a test, requires a consistent structure with a /synonyms and an /en module, with the same variable names
-- data sructure has to be fairly simple
-- for an example see Template:Technique:Maintenance

local p = {}
local linguistic = require('Module:Linguistic')
local fallback = require('Module:LangSwitch')
local i18nmessages = {
	['broken header'] = {
		['en'] = 'Broken redirects found for this module: ',
		['zh-hans'] = '用于此模板的受损重定向:',
		['zh-hant'] = '用於此模板的受損重新導向:',
		},
	['none found'] = {
		['en'] = 'No value found',
		['fr'] = 'Aucune valeur trouvée',
		['zh-hans'] = '找不到值',
		['zh-hant'] = '查無可用值',
		},
}
local function translate(message, lang)
	local text = fallback._langSwitch(i18nmessages[message], lang)
	return text
end

function p.brokenredirects(frame) 
	local brokenlist = {}
	local langoftext = 	frame.args.lang
	if not langoftext then
		langoftext = frame:preprocess( "{{int:lang}}" )
	end
	local mymodule = mw.text.trim(frame.args[1])
	local synlist = require ('Module:' .. mymodule .. '/synonyms')
	if not synlist then return
		"error, list of redirects not found"
	end
	local englist = require ('Module:' .. mymodule .. '/en')
	if not englist then return
		"error, English version of the template not found"
	end
	for i, j in pairs(synlist) do
		if type(j) == 'table' then
			for k, l in pairs(j) do
				if not englist[i] then
					return "error: no variable " .. i .. 'found in [[Module:' .. mymodule .. ']]'
				elseif not englist[i][l] then
					table.insert(brokenlist, i .. ": '" .. k .. "'  -> '" .. l .. "'")
				end
			end
		elseif type(j) == 'string' then
			if not englist[j] then 
				table.insert(brokenlist, i)
			end
		end
	end
	if #brokenlist > 0 then
		brokenlist = mw.text.listToText(brokenlist, '<br />', '<br />')
		return translate('broken header', langoftext) ..  brokenlist
	else 
		return translate('none found', langoftext)
	end
end
function p.listmismatch(mymodule, tested, reference, exclude, langoftext) -- checks that all keys in tested are also in reference
	local mymodule = mw.text.trim(mymodule)
	local header = "Terms that appear in "  .. mw.language.fetchLanguageName(reference, "en") .. " but not in "  .. mw.language.fetchLanguageName(tested, "en") .. ':'
	
	if not mw.title.new('Module:' .. mymodule .. '/' .. reference).exists then
		return "No module found in " .. mw.language.fetchLanguageName(reference, "en")
	end
	reference = require('Module:' .. mymodule .. '/' .. reference)

	if not mw.title.new('Module:' .. mymodule .. '/' .. tested).exists then
		return "No module found in " .. mw.language.fetchLanguageName(tested, "en")
	end	
	tested = require('Module:' .. mymodule .. '/' .. tested)

	local missing = {}
	if exclude then
		exclude = exclude .. ',' -- hack to exclude lang specific variables
	end
	for i, j in pairs(reference) do
		if exclude and string.find(exclude, i .. ',') then 
			--do nothing
		elseif type(j) == 'table' then
			for k in pairs(j) do 
				if not tested[i][k] then
					table.insert(missing, i .. ": '" .. k .. "'")
				end
			end
		elseif type(i) == 'string' then
			if not tested[i] then
				table.insert(missing, i)
			end
		end
	end
	if #missing > 0 then 
		missing = mw.text.listToText(missing, '<br />', '<br />')
		return header .. '<br />'  ..  missing
	else 
		return header .. '<br />'  .. translate('none found', langoftext)
	end
end

function p.strangelocals(frame) -- find terms in the local list but not in English
	local langtocheck = frame.args.langtocheck
	local langoftext = frame.args.langoftext
	if not langtocheck then
		langtocheck = frame:preprocess( "{{int:lang}}" )
	end
	if not langoftext then
		langoftext = frame:preprocess( "{{int:lang}}" )
	end
	local lang = frame.args.lang
	if not lang then
		lang = frame:preprocess( "{{int:lang}}" )
	end
	if lang == 'en' then
		return "The English list is there reference version, please choose anoher language"
	else
		return p.listmismatch(frame.args.module, 'en', langtocheck, frame.args.exclude, langoftext)
	end
end

function p.missingtranslations(frame)
	local langtocheck = frame.args.langtocheck
	local langoftext = frame.args.langoftext
	if not langtocheck then
		langtocheck = frame:preprocess( "{{int:lang}}" )
	end
	if not langoftext then
		langoftext = frame:preprocess( "{{int:lang}}" )
	end
	if langoftext == 'en' then
		return "The English list is there reference version, please choose anoher language"
	else
		return p.listmismatch(frame.args.module, langtocheck, 'en', frame.args.exclude, langoftext)
	end
end
function p.list(frame) 
	local lang = frame.args.lang
	local mymodule = 'Module:' .. mw.text.trim(frame.args.module)
	if not lang then
		lang = frame:preprocess( "{{int:lang}}" )
	end
	local engpage = require(mymodule .. '/en')
	local localpage = require(fallback.fallbackpage(mymodule, lang))
	local synonyms = nil
	if mw.title.new('Module:' .. mymodule .. '/' .. 'synonyms').exists then 
		synonyms = require('Module:' .. mymodule .. '/' .. 'synonyms')
	end
	local transtable = ''
	for i, j in pairs(engpage) do
		if type(j) == 'string' then
			transtable = transtable .. j
		end
		local synos = ''
		if synonyms and synonyms[i] and type(synonyms[i]) == 'string' then 
			synos = synonyms[i]
			transtable = transtable .. "''<br /> synonyms: " .. synos .. "''"
		end
		local translation = "??" 
		if locpalpage.synonyms[i] and type(locpalpage.synonyms[i]) == string then
			translation = synonyms[i] 
			transtable = transtable .. lang .. ':' .. locpalpage.synonyms[i]
		end
	end
	return transtable
end

local function langSwitchstats(list, lang)
	local translated = {}
	local untranslated = {}
	local langlabel =  mw.language.fetchLanguageName(lang, "en")
	local translatedtext = ''
	local untranslatedtext = ''
	for i, j in pairs(list) do
		if i ~= 'lang' then -- lang should be already removed from langSwitchfromWiki but it does not seem to work
			local testedpage = require(j)
			if testedpage[lang] then 
				table.insert(translated, '[[' .. mw.text.trim(j) .. ']]')
			else
				table.insert(untranslated, '[[' .. mw.text.trim(j ).. ']]' )
			end
		end
	end
	if #translated == 0 then
		translatedtext = "None of the modules analyzed has been translated into " .. langlabel
	else	
		translatedtext = "Modules translated into " .. langlabel .. ":<br />"  .. mw.text.listToText(translated, '<br />', '<br />')
	end
	if #untranslated == 0 then
		untranslatedtext = "All analyed modules have been translated into " .. langlabel
	else	
		untranslatedtext = "Modules not translated into " .. langlabel .. ":<br />"  .. mw.text.listToText(untranslated, '<br />', '<br />')
	end	
	return untranslatedtext  .. "<br /><br />".. translatedtext
end

function p.langSwitchfromWiki(frame)
	local args = frame.args
	local lang = args.lang
	if not lang then
		lang = frame:preprocess( "{{int:lang}}" )
	end
	args.lang = nil -- does not seem to work
	return langSwitchstats(args, lang)
end
return p