"ഘടകം:Autotranslate" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം

Schoolwiki സംരംഭത്തിൽ നിന്ന്
(വിക്കിമീഡിയ കോമ്മൺസിൽ നിന്നും ഇറക്കുമതി ചെയ്തു)
 
(rewrite proposed by User:Tacsipacsi using more efficient test for existence of language subtemplate)
വരി 1: വരി 1:
--[[
  --[[
   __  __          _      _          _        _        _                      _      _       
   __  __          _      _          _        _        _                      _      _       
  |  \/  | ___  __| |_  _| | ___ _  / \  _  _| |_ ___ | |_ _ __ __ _ _ __  ___| | __ _| |_ ___  
  |  \/  | ___  __| |_  _| | ___ _  / \  _  _| |_ ___ | |_ _ __ __ _ _ __  ___| | __ _| |_ ___  
വരി 49: വരി 49:
-- get language fallback list
-- get language fallback list
if not args.lang or not mw.language.isSupportedLanguage(args.lang) then
if not args.lang or not mw.language.isSupportedLanguage(args.lang) then
args.lang = frame:callParserFunction( "int", "lang" )           -- get user's chosen language  
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language  
end
end
local langList = mw.language.getFallbacksFor(args.lang)
local langList = mw.language.getFallbacksFor(args.lang)
table.insert(langList,1,args.lang)
table.insert(langList,1,args.lang) -- user's language will be the first one to check


-- find base page
-- find base page
local base = args.base
local base = args.base
args.base = nil
args.base = nil -- blank it so it is not passed to language sub-templates
assert(base and  #base>0, 'Base page not provided for autotranslate' )
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
-- Local function for expanding a template that can be pcall()ed: call the template
-- with the same template arguments as the ones passed to {{autotranslate}} template.
local function expandTemplate(title)
return frame:expandTemplate{ title = title, args = args }
end
end
-- find base template language subpage
-- find base template language subpage
local page = args.default  -- default page if provided or nil otherwise
local success, res
for _, language in ipairs(langList) do
for _, language in ipairs(langList) do
if mw.title.new(base .. '/' .. language).exists then
success, res = pcall(expandTemplate, base .. '/' .. language)
page =  base .. '/' .. language -- returns only the page
if success then
break
break
end
end
end
end
assert(page, string.format('No fallback page found for autotranslate (base=[[%s]], lang=%s)', base, args.lang))
local err_msg = 'No fallback page found for autotranslate (base=[[:%s]], lang=%s)'
if (not success) then
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
assert(args.default, string.format(err_msg, base, args.lang))  
return frame:expandTemplate{ title = page, args = args}
success, res = pcall(expandTemplate, args.default)
assert(success, string.format(err_msg, base, args.default))
end
 
-- If this if the base page being translateda
if (mw.title.getCurrentTitle().fullText==base) then
-- English language is the last fallback language for most languages, if
-- nothing else there should be at least an english subpage
local en_exist = mw.title.new(base .. '/en').exists
assert(en_exist, string.format(err_msg, base, 'en'))
if (mw.site.siteName=='Wikimedia Commons') then
res = res .. '\n[[Category:Autotranslated templates|' .. base .. ']]'
end
end
return res
end
end


return p
return p

18:59, 18 ഏപ്രിൽ 2024-നു നിലവിലുണ്ടായിരുന്ന രൂപം

ഈ ഘടകത്തിന്റെ വിവരണം ഘടകം:Autotranslate/വിവരണം എന്ന താളിൽ നിർമ്മിക്കാവുന്നതാണ്

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

-- local function to help normalize input arguments
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

-- initialize object to be returned
local p = {}

--[[
autotranslate
 
This function is the core part of the Autotranslate template. 
 
Usage from a template:
{{#invoke:autotranslate|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) -- user's language will be the first one to check

	-- find base page
	local base = args.base
	args.base  = nil -- blank it so it is not passed to language sub-templates
	assert(base and  #base>0, 'Base page not provided for autotranslate' )

	-- Local function for expanding a template that can be pcall()ed: call the template 
	-- with the same template arguments as the ones passed to {{autotranslate}} template.
	local function expandTemplate(title)
		return frame:expandTemplate{ title = title, args = args }
	end
	
	-- find base template language subpage
	local success, res
	for _, language in ipairs(langList) do
		success, res = pcall(expandTemplate, base .. '/' .. language)
		if success then
			break
		end
	end
	local err_msg = 'No fallback page found for autotranslate (base=[[:%s]], lang=%s)'
	if (not success) then
		assert(args.default, string.format(err_msg, base, args.lang)) 
		success, res = pcall(expandTemplate, args.default)
		assert(success, string.format(err_msg, base, args.default))
	end

	-- If this if the base page being translateda
	if (mw.title.getCurrentTitle().fullText==base) then
		-- English language is the last fallback language for most languages, if
		-- nothing else there should be at least an english subpage
		local en_exist = mw.title.new(base .. '/en').exists
		assert(en_exist, string.format(err_msg, base, 'en'))
		if (mw.site.siteName=='Wikimedia Commons') then
			res = res .. '\n[[Category:Autotranslated templates|' .. base .. ']]'
		end 
	end
	return res
end

return p
"https://schoolwiki.in/index.php?title=ഘടകം:Autotranslate&oldid=2539998" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്