Module:Autotranslate

De BerryRocket
Révision datée du 5 février 2024 à 02:21 par meta>Jarekt (raise an error if English subpage is missing no matter what language is being displayed)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

La documentation pour ce module peut être créée à Module:Autotranslate/doc

Erreur de script : Erreur Lua : erreur interne : l’interpréteur s’est arrêté avec le signal « -129 ».

  --[[
  __  __           _       _           _         _        _                       _       _       
 |  \/  | ___   __| |_   _| | ___ _   / \  _   _| |_ ___ | |_ _ __ __ _ _ __  ___| | __ _| |_ ___ 
 | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \
 | |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | ||  __/
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_/   \_\__,_|\__\___/ \__|_|  \__,_|_| |_|___/_|\__,_|\__\___|
 
 Authors and maintainers:
* User:Zolo   - original version
* User:Jarekt 
]]

-- local function to help normalize input arguments
local function normalize_input_args(input_args, output_args)
	for name, value in pairs( input_args ) do 
		if value ~= '' then -- nuke empty strings
			if type(name)=='string' then name=string.lower(name) end -- convert to lower case
			output_args[name] = value
		end
	end
	return output_args
end

-- initialize object to be returned
local p = {}

--[[
autotranslate
 
This function is the core part of the Autotranslate template. 
 
Usage from a template:
{{#invoke:autotranslate|autotranslate|base=|lang= }}
 
Parameters:
  frame.args.base - base page name
  frame.args.lang - desired language (often user's native language)

 Error Handling:

]]
function p.autotranslate(frame) 

	-- switch to lowercase parameters to make them case independent
	local args = {}
	args = normalize_input_args(frame:getParent().args, args)
	args = normalize_input_args(frame.args, args)

	-- get language fallback list
	if not args.lang or not mw.language.isSupportedLanguage(args.lang) then
		args.lang = frame:callParserFunction( "int", "lang" )           -- get user's chosen language 
	end
	local langList = mw.language.getFallbacksFor(args.lang)
	table.insert(langList,1,args.lang)

	-- find base page
	local base = args.base
	args.base = nil
	assert(base and  #base>0, 'Base page not provided for autotranslate' )
	if not mw.ustring.find(base,':') then   -- if base page does not indicate namespace
		base = 'Template:' .. base          -- then assume it is a template 
	end
 
	-- English language is the last fallback language for most languages, if nothing else there 
	-- should be at least an english subpage
	local en_exist = mw.title.new(base .. '/en').exists
	assert(en_exist, string.format('No fallback page found for autotranslate (base=[[%s]], lang=%s)', base, 'en'))

	-- find base template language subpage
	local page = args.default  -- default page if provided or nil otherwise
	for _, language in ipairs(langList) do
		if mw.title.new(base .. '/' .. language).exists then
			page =  base .. '/' .. language -- returns only the page
			break
		end
	end
	
	-- last check for errors. This one would be vary rarely used maybe a language which "isSupportedLanguage" but does 
	-- not have any fallback languages, like qqx
	assert(page, string.format('No fallback page found for autotranslate (base=[[%s]], lang=%s)', base, args.lang))
 
	-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
	local res = frame:expandTemplate{ title = page, args = args}
	
	-- If we are at commons and this if the base page being translated
	if (mw.title.getCurrentTitle().fullText==base and mw.site.server:match('//commons.wikimedia.org$')) then
		res = res .. '\n[[Category:Autotranslated templates|' .. base .. ']]'
	end
	return res
end

return p