Jump to content

Module:RelevantLanguage

From translatewiki.net

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

--
-- Port [[Template:RelevantLanguage]] to Lua
--
require( 'strict' )
local p = {}

local currentTitle = mw.title.getCurrentTitle()

local function isEmpty( str )
	return str == nil or str == ''
end

function p._main( frame )
	frame = frame or mw.getCurrentFrame()
	
	-- Default to user language
	local lang = frame:preprocess( '{{USERLANGUAGE}}' )

	if currentTitle:inNamespace( 'Template' ) then
		return lang
	elseif currentTitle:inNamespace( 'Portal' ) then
		-- For portals, use {{BASEPAGENAME}}
		lang = mw.getContentLanguage():lcfirst( currentTitle.baseText )
	else
		-- {{#translation:}} returns /en etc. if it is a translation page
		local trLang = frame:preprocess( '{{#translation:}}' ):gsub( '^%/', '' )
		if isEmpty( trLang ) then
			-- Try to also use {{SUBPAGENAME}} if it matches a known language
			trLang = currentTitle.subpageText
			if not mw.language.isSupportedLanguage( trLang ) then
				trLang = nil
			end
		end
		
		-- Do not use translation language for qqq/qqx subpages
		if trLang == 'qqq' or trLang == 'qqx' then
			trLang = nil
		end
		
		if not isEmpty( trLang ) then
			lang = trLang
		end
	end
	
	return lang
end

function p.main( frame )
	return p._main( frame )
end

return p