Module:TranslateThis

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation

[edit]

This module powers Template:TranslateThis; see there for usage.

Code

local fb = require('Module:Fallback')
local dir = require('Module:Dir')

return { main = function( frame )
	
	local 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 args = shallowcopy(frame:getParent().args)
	local lang = args.lang or frame:callParserFunction("int", "lang") -- I don't know of any other way to get the user's language...
	args.lang = nil
	
	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 dir="' .. dir.select(lang, 'rtl', 'ltr') .. '" lang="' .. lang .. '">' .. result .. '</span>'
	else
		local default = {}
		for i, v in pairs( args ) do
			if v ~= "" then
				table.insert(default, '<span dir="' .. dir.select(i, 'rtl', 'ltr') .. '" lang="' .. i .. '">' .. v .. '</span> <small>(' .. i .. ')</small>')
			end
		end
		default = table.concat(default, ' / ')
		args.default=default
		local newlang
		result, newlang = fb._langSwitch(args, lang)
		if not newlang then
			result = ''
		elseif newlang~='default' then
			result = '<span dir="' .. dir.select(newlang, 'rtl', 'ltr') .. '" lang="' .. newlang .. '">' .. result .. '</span> <small>(' .. newlang .. ')</small>'
		end
		local langname = mw.language.fetchLanguageName( lang, lang )
		if langname ~= "" and newlang then
			result = result .. " – " .. frame:expandTemplate( { title = "Help translate", args = { langname, lang = lang } } )
		end
	end
	
	return anchor .. result
end}