Module:TranslateThis

From Hablon
Revision as of 14:29, 17 April 2021 by Jojit fb (talk | contribs) (Created page with "return { main = function( frame ) function shallowcopy(orig) if type(orig) ~= 'table' then return orig end local result = {} for key, value in pairs(orig) do...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

return { main = function( frame )
	
	function shallowcopy(orig)
		if type(orig) ~= 'table' then
			return orig
		end
		
		local result = {}
		for key, value in pairs(orig) do
			result[key] = value
		end
		return result
	end
	
	local lang = frame:preprocess("{{int:lang}}") -- I don't know of any other way to get the user's language...
	local args = shallowcopy(frame:getParent().args)
	
	local anchor = args.anchor
	args.anchor = nil
	if (anchor and args[anchor]) then
		anchor = args[anchor]
	end
	anchor = anchor and frame:expandTemplate( { title = "Anchor", args = { anchor } } ) or ''
	
	local result = args[ lang ]
	if result and result ~= '' then
		result = '<span lang="' .. lang .. '">' .. result .. '</span>'
	else
		result = ''
		for i, v in pairs( args ) do
			if v ~= "" and i ~= "source" then
				result = result .. ' / <span lang="' .. i .. '">' .. v .. '</span> <small>(' .. i .. ')</small>'
			end
		end
		if args["source"] then
			result = result:sub( 4 ) .. " – " ..  "Want to help translate? [[" .. args["source"] .. "|Translate the missing messages.]]"
		end
	end
	
	return anchor .. result
end}