உள்ளடக்கத்துக்குச் செல்

Module:Fallback

விக்கிமூலம் இலிருந்து

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

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

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

local p = {}

--[[
_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 the list of accepetable language (lang + those in lang's fallback chain) and check their content
	assert(lang, 'LangSwitch Error: no lang')
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList,1,lang)
	table.insert(langList,math.max(#langList,2),'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
	local lang = args.lang
	if not lang or not mw.language.isSupportedLanguage(lang) then
		lang = frame:callParserFunction( "int", "lang" )           -- get user's chosen language 
	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) 

	-- 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          -- 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
	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.
	return frame:expandTemplate{ title = page, args = args}
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

local 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 langlist = require('Module:Fallbacklist')
	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
"https://ta.wikisource.org/w/index.php?title=Module:Fallback&oldid=943229" இலிருந்து மீள்விக்கப்பட்டது