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

local p = {}

local langlist = require('Module:Fallbacklist')


--[[
_langSwitch
 
This function is the core part of the LangSwitch template. 
 
Example usage from Lua:
text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
 
Parameters:
  args - table with translations by language
  lang - desired language (often user's native language)

 Error Handling:

]]

function p._langSwitch(args, lang) -- args: table of translations
	-- Return error if there is not default and no english version
	if not args.en and not args.default then
		if args.nocat == '1' then
			return '<strong class="error">LangSwitch Error: no default</strong>' 
		else
			return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]' 
		end
	end
	-- get language (either stated one or user's default language)
	if not lang then
		return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
	end
	-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList,1,lang)
	table.insert(langList, 'default')
	for _, language  in ipairs(langList) do 
		if args[language ] == '~' then 
			return ''
		elseif args[language] and args[language] ~= '' then 
			return args[language] 
		end
	end
end

--[[
langSwitch
 
This function is the core part of the LangSwitch template. 
 
Example Usage from a template:
{{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
 
Parameters:
  frame.args - table with translations by language
  frame.args.lang - desired language (often user's native language)

 Error Handling:

]]
function p.langSwitch(frame) -- version to be used from wikitext
	args = frame.args
	-- if no expected args provided than check parent template/module args
	if args.en==nil and args.default==nil and args.nocat==nil then
		args = mw.getCurrentFrame():getParent().args 
	end
	if args.lang and args.lang ~= '' then
		lang = args.lang
	else -- get user's chosen language 
		lang = frame:callParserFunction( "int", "lang" )
	end
	args.lang = nil
	return p._langSwitch(args, lang)
end

--[[
autotranslate
 
This function is the core part of the Autotranslate template. 
 
Usage from a template:
{{#invoke:fallback|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) 
	local args = frame.args
	if not args.lang or 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
	if not base or base == '' then
		return '<strong class="error">Base page not provided for autotranslate</strong>' 
	end
	if not mw.ustring.find(base,':') then -- if base page does not indicate namespace
		base = 'Template:' .. base          -- than assume it is a template 
	end
 
	-- find base template language subpage
	local page = nil
	for _, language in ipairs(langList) do
		if mw.title.new(base .. '/' .. language).exists then
			page =  base .. '/' .. language -- returns only the page
			break
		end
	end
	if not page then
		return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang)
	end
	
	-- repack args in a standard table
	local inArgs = {}
	for field, value in pairs(args) do
		inArgs[field] = value;
	end
 
	-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
	inArgs.base = nil
	return frame:expandTemplate{ title = page, args = inArgs }
end

--[[
translatelua
 
Allows easy translation or internalization of pages in Lua. 
 
Example Usage from a template:
{{#invoke: fallback|translatelua| i18n/oil on canvas|lang={{{lang|}}}}}
 
Parameters:
  frame.args.1 - name of translation module
	frame.args.2 - field name of the structure in Module:[frame.args.1] to use
  frame.args.lang - desired language (often user's native language)

 Error Handling:

]]
function p.translatelua(frame)
	local lang = frame.args.lang
	local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations
	if not lang or mw.text.trim(lang) == '' then
		lang = frame:callParserFunction( "int", "lang" )
	end
	if frame.args[2] then
		page = page[mw.text.trim(frame.args[2])]
	end
	return p._langSwitch(page, lang)
end

--[[
fblist
 
Similar to  mw.language.getFallbacksFor(lang) but uses Commons old fallback chain 
 
Parameters:
  lang - desired language (often user's native language)

 Error Handling:

]]
function p.fblist(lang) -- list the full fallback chain from a language to en
	local fbtable = p.fallbackloop{ lang:lower() }
	table.insert(fbtable, 'default')
	table.insert(fbtable, 'en')
	return fbtable
end

function _inArray(x, t)
	for i, v in ipairs(t) do
		if v == x then return i end
	end
	return -1
end

function p.fallbackloop(fbtable)  --list of fallback languages in string format (more convenient than tables)
	local changes = false
	for i, j in ipairs(fbtable) do
		local seq = langlist[j]
		if seq then
			for k, l in ipairs(seq) do
				if _inArray(l, fbtable) == -1 then
					table.insert(fbtable, l)
					changes = true
				end
			end
		end
	end
	if changes then
		return p.fallbackloop(fbtable)
	end
	return fbtable
end

return p