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

Schoolwiki സംരംഭത്തിൽ നിന്ന്
(ചെ.) (ഒരു പതിപ്പ് ഇറക്കുമതി ചെയ്തു)
 
(ചെ.) (ഒരു പതിപ്പ് ഇറക്കുമതി ചെയ്തു)
 
(2 ഉപയോക്താക്കൾ ചെയ്ത ഇടയ്ക്കുള്ള 2 നാൾപ്പതിപ്പുകൾ പ്രദർശിപ്പിക്കുന്നില്ല)
വരി 1: വരി 1:
--[[
local p = {}
  __  __          _      _        _____    _ _ _                _   
|  \/  | ___  __| |_  _| | ___ _|  ___|_ _| | | |__  __ _  ___| | __
| |\/| |/ _ \ / _` | | | | |/ _ (_) |_ / _` | | | '_ \ / _` |/ __| |/ /
| |  | | (_) | (_| | |_| | |  __/_|  _| (_| | | | |_) | (_| | (__|  <
|_|  |_|\___/ \__,_|\__,_|_|\___(_)_|  \__,_|_|_|_.__/ \__,_|\___|_|\_\
Authors and maintainers:
* User:Zolo  - original version
* User:Jarekt
]]


local function normalize_input_args(input_args, output_args)
-- List the full fallback chain from a language to default (usually English)
for name, value in pairs( input_args ) do
function p.fblist(lang)
if value ~= '' then -- nuke empty strings
local fbtable = mw.language.getFallbacksFor(lang)
if type(name)=='string' then name=string.lower(name) end -- convert to lower case
table.insert(fbtable, 1, lang)
output_args[name] = value
--[[
end
Take a translation from "Mediawiki:<Message-ID>/<language-code>" namespace
end
or from a loaded i18 resource bundle in MediaWiki for its UI messages (also
return output_args
used by the "{{Int:<Message-ID>}}" parser function), before using the
provided default value. Requires args.message = 'Message-ID', instead of
args.message = 'actual translated message'.
--]]
table.insert(fbtable, 'message')
table.insert(fbtable, 'default')
return fbtable
end
end


local p = {}
--[==[
 
Return an error if there is not default and no English version, otherwise
--[[
return the message in the most appropriate, plus the lang code as a second value.
_langSwitch
--]==]
function p._langSwitch(args, lang)
This function is the core part of the LangSwitch template.
-- args: table of translations
if not args.en and not args.default and not args.message and args.nocat ~= '1' then
Example usage from Lua:
return error("langSwitch error: no default")
text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
end
-- Get language (either stated one or user's default language).
Parameters:
if not lang then
  args - table with translations by language
return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
  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
end
-- Get the list of acceptable language (lang + those in lang's fallback chain) and check their content.
-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
for _, code in ipairs(p.fblist(lang)) do
assert(lang, 'LangSwitch Error: no lang')
local msg = args[code]
local langList = mw.language.getFallbacksFor(lang)
if msg then
table.insert(langList,1,lang)
-- Trim the assigned message value before testing it.
table.insert(langList,math.max(#langList,2),'default')
msg = mw.text.trim(msg)
for _, language  in ipairs(langList) do
if msg ~= '' then
if args[language ] == '~' then
if code == 'message' then
return ''
-- If this is an UI message. See [[mw:Manual:Messages API]].
elseif args[language] and args[language] ~= '' then  
msg = mw.message.new(args.message):inLanguage(lang)
return args[language]
--[==[ If this message name does not exist, converting it to
a string would not return an actual message, but this name
within curved angle brackets U+29FC/U+29FD '⧼/⧽',
part of mathematical symbols). The UI message may also be
disabled administratively if it causes problems.
--]==]
if msg:exists() and not msg:isDisabled() then
--[==[FIXME: In which language is this message?
This may be in some fallback language and not lang.
Note that some UI messages may have placeholders like '%s'
but there's no way to replace them here by actual values.
--]==]
return tostring(msg), lang
end
elseif msg == '~' then
return nil, code
else
return msg, code
end
end
end
end
end
end
return nil
end
end


--[[
--[==[
langSwitch
Version to be used from wikitext.
--]==]
This function is the core part of the LangSwitch template.  
function p.langSwitch(frame)
local args = frame.args
Example Usage from a template:
-- If no expected args provided than check parent template/module args.
{{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
if not args.en and not args.default and not args.nocat then
args = frame:getParent().args
Parameters:
end
  frame.args - table with translations by language
local lang
  frame.args.lang - desired language (often user's native language)
if args.lang and args.lang ~= '' then
 
lang = args.lang
Error Handling:
args.lang = nil
 
else -- Get user's chosen language.
]]
lang = frame:preprocess("{{Int:Lang}}")
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
end
local lang = args.lang
--[==[
if not lang or not mw.language.isSupportedLanguage(lang) then
if args.zh ~= '' and args['zh-hans'] == '' and args['zh-hant'] == '' then
lang = frame:callParserFunction( "int", "lang" )          -- get user's chosen language
else
end
end
args.lang = nil
--]==]
return p._langSwitch(args, lang)
local str, language = p._langSwitch(args, lang)
return str -- Get the first value of the langSwitch, (the text) not the second (the language).
end
end


--[[
function p.fallbackpage(base, lang, formatting)
autotranslate
local languages = p.fblist(lang)
for i, lng in ipairs(languages) do
This function is the core part of the Autotranslate template.  
if mw.title.new(base .. '/' .. lng).exists then
if formatting == 'table' then
Usage from a template:
return {base .. '/' .. lng, lng} -- Returns name of the page + name of the language.
{{#invoke:fallback|autotranslate|base=|lang= }}
else
return base .. '/' .. lng -- Returns only the page.
Parameters:
end
  frame.args.base - base page name
end
  frame.args.lang - desired language (often user's native language)
end
 
return base
Error Handling:
end
 
]]
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
Logic for [[Template:Autotranslate]].
args.lang = frame:callParserFunction( "int", "lang" )           -- get user's chosen language  
]==]
function p.autotranslate(frame)
local args = frame.args
if not args.lang or args.lang == '' then
args.lang = frame:preprocess("{{Int:Lang}}") -- Get user's chosen language.
end
end
local langList = mw.language.getFallbacksFor(args.lang)
-- Find base page.
table.insert(langList,1,args.lang)
-- find base page
local base = args.base
local base = args.base
args.base = nil
if not base or base == '' then
assert(base and  #base>0, 'Base page not provided for autotranslate' )
return '<strong class="error">Base page not provided for autotranslate</strong>'
if not mw.ustring.find(base,':') then   -- if base page does not indicate namespace
end
base = 'Template:' .. base         -- than assume it is a template
if string.sub(base, 2, 9) ~= 'emplate:' then
base = 'Template:' .. base -- Base provided without 'Template:' part.
end
end
-- Find base template language subpage.
-- find base template language subpage
local page = p.fallbackpage(base, args.lang) --
local page = nil
if (not page and base ~= args.base) then
for _, language in ipairs(langList) do
-- Try the original args.base string. This case is only needed if base is not in template namespace.
if mw.title.new(base .. '/' .. language).exists then
page = p.fallbackpage(args.base, args.lang)
page = base .. '/' .. language -- returns only the page
end
break
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 newargs = {}
for field, value in pairs(args) do
if field ~= 'base' then
newargs[field] = value
end
end
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.
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
return frame:expandTemplate{ title = page, args = args}
return frame:expandTemplate{ title = page, args = newargs }
end
end


--[[
--[==[
translatelua
Translate data stored in a module.
]==]
Allows easy translation or internalization of pages in Lua.  
function p.translate(page, key, lang)
if type(page) == 'string' then -- If the requested translation table is not yet loaded.
Example Usage from a template:
page = require('Module:' .. page)
{{#invoke: fallback|translatelua| i18n/oil on canvas|lang={{{lang|}}}}}
end
local val
Parameters:
if page[key] then
  frame.args.1 - name of translation module
val = page[key]
frame.args.2 - field name of the structure in Module:[frame.args.1] to use
elseif page.keys and page.keys[key] then -- Key 'keys" is an index of all keys, including redirects, see [[Module:i18n/datatype]].
  frame.args.lang - desired language (often user's native language)
val = page.keys[key]
 
end
Error Handling:
if not val then
return '<' .. key .. '>'
end
return p._langSwitch(val, lang)
end


]]
function p.translatelua(frame)
function p.translatelua(frame)
local lang = frame.args.lang
local lang = frame.args.lang
local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations
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
if not lang or mw.text.trim(lang) == '' then
lang = frame:callParserFunction( "int", "lang" )
lang = frame:preprocess("{{Int:Lang}}")
end
end
if frame.args[2] then
if frame.args[2] then
വരി 171: വരി 171:
end
end


--[[
-- This test does not work ('Module:Fallback/tests/fallbacks' is missing)
fblist
function p.runTests()
local toFallbackTest = require('Module:Fallback/tests/fallbacks')
Similar to  mw.language.getFallbacksFor(lang) but uses Commons old fallback chain
local result = true
mw.log('Testing fallback chains')
Parameters:
for i, t in ipairs(toFallbackTest) do
  lang - desired language (often user's native language)
local fbtbl = table.concat(p.fblist(t.initial), ', ')
 
local expected = table.concat(t.expected, ', ')
Error Handling:
local ret = (fbtbl == expected)
 
mw.log(i, ret and 'passed' or 'FAILED', t.initial, (not ret) and ('FAILED\nis >>' .. fbtbl .. '<<\nbut should be >>' .. expected .. '<<\n') or '')
]]
result = result and ret
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
end
return -1
return result
end
end


function p.fallbackloop(fbtable)  --list of fallback languages in string format (more convenient than tables)
--[==[
local langlist = require('Module:Fallbacklist')
List all input arguments of the template that calls "{{#invoke:Fallback|showTemplateArguments}}"
local changes = false
]==]
for i, j in ipairs(fbtable) do
function p.showTemplateArguments(frame)
local seq = langlist[j]
local str = ''
if seq then
for name, value in pairs( frame:getParent().args ) do
for k, l in ipairs(seq) do
if str == '' then
if _inArray(l, fbtable) == -1 then
str = string.format('%s=%s', name, value)         -- argument #1
table.insert(fbtable, l)
else
changes = true
str = string.format('%s, %s=%s', str, name, value) -- the rest
end
end
end
end
end
end
if changes then
return str
return p.fallbackloop(fbtable)
end
return fbtable
end
end


return p
return p

17:32, 27 ജൂൺ 2024-നു നിലവിലുള്ള രൂപം

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

local p = {}

-- List the full fallback chain from a language to default (usually English)
function p.fblist(lang)
	local fbtable = mw.language.getFallbacksFor(lang)
	table.insert(fbtable, 1, lang)
	--[[
	Take a translation from "Mediawiki:<Message-ID>/<language-code>" namespace
	or from a loaded i18 resource bundle in MediaWiki for its UI messages (also
	used by the "{{Int:<Message-ID>}}" parser function), before using the
	provided default value. Requires args.message = 'Message-ID', instead of
	args.message = 'actual translated message'.
	--]]
	table.insert(fbtable, 'message')
	table.insert(fbtable, 'default')
	return fbtable
end

--[==[
Return an error if there is not default and no English version, otherwise
return the message in the most appropriate, plus the lang code as a second value.
--]==]
function p._langSwitch(args, lang)
	-- args: table of translations
	if not args.en and not args.default and not args.message and args.nocat ~= '1' then
		return error("langSwitch error: no default")
	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 acceptable language (lang + those in lang's fallback chain) and check their content.
	for _, code in ipairs(p.fblist(lang)) do
		local msg = args[code]
		if msg then
			-- Trim the assigned message value before testing it.
			msg = mw.text.trim(msg)
			if msg ~= '' then
				if code == 'message' then
					-- If this is an UI message. See [[mw:Manual:Messages API]].
					msg = mw.message.new(args.message):inLanguage(lang)
					--[==[ If this message name does not exist, converting it to
					a string would not return an actual message, but this name
					within curved angle brackets U+29FC/U+29FD '⧼/⧽', 
					part of mathematical symbols). The UI message may also be
					disabled administratively if it causes problems.
					--]==]
					if msg:exists() and not msg:isDisabled() then
						--[==[FIXME: In which language is this message?
						This may be in some fallback language and not lang.
						Note that some UI messages may have placeholders like '%s'
						but there's no way to replace them here by actual values.
						--]==]
						return tostring(msg), lang
					end
				elseif msg == '~' then
					return nil, code
				else
					return msg, code
				end
			end
		end
	end
	return nil
end

--[==[
Version to be used from wikitext.
--]==]
function p.langSwitch(frame)
	local args = frame.args
	-- If no expected args provided than check parent template/module args.
	if not args.en and not args.default and not args.nocat then
		args = frame:getParent().args
	end
	local lang
	if args.lang and args.lang ~= '' then
		lang = args.lang
		args.lang = nil
	else -- Get user's chosen language.
		lang = frame:preprocess("{{Int:Lang}}")
	end
--[==[
	if args.zh ~= '' and args['zh-hans'] == '' and args['zh-hant'] == '' then
	else
	end
--]==]
	local str, language = p._langSwitch(args, lang)
	return str -- Get the first value of the langSwitch, (the text) not the second (the language).
end

function p.fallbackpage(base, lang, formatting)
	local languages = p.fblist(lang)
	for i, lng in ipairs(languages) do
		if mw.title.new(base .. '/' .. lng).exists then
			if formatting == 'table' then
				return {base .. '/' .. lng, lng} -- Returns name of the page + name of the language.
			else
				return base .. '/' .. lng -- Returns only the page.
			end
		end
	end
	return base
end

--[==[
Logic for [[Template:Autotranslate]].
]==]
function p.autotranslate(frame)
	local args = frame.args
	if not args.lang or args.lang == '' then
		args.lang = frame:preprocess("{{Int:Lang}}") -- Get user's chosen language.
	end
	-- 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 string.sub(base, 2, 9) ~= 'emplate:' then
		base = 'Template:' .. base -- Base provided without 'Template:' part.
	end
	-- Find base template language subpage.
	local page = p.fallbackpage(base, args.lang) --
	if (not page and base ~= args.base) then
		-- Try the original args.base string. This case is only needed if base is not in template namespace.
		page = p.fallbackpage(args.base, args.lang)
	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 newargs = {}
	for field, value in pairs(args) do
		if field ~= 'base' then
			newargs[field] = value
		end
	end
	-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
	return frame:expandTemplate{ title = page, args = newargs }
end

--[==[
Translate data stored in a module.
]==]
function p.translate(page, key, lang)
	if type(page) == 'string' then -- If the requested translation table is not yet loaded.
		page = require('Module:' .. page)
	end
	local val
	if page[key] then
		val = page[key]
	elseif page.keys and page.keys[key] then -- Key 'keys" is an index of all keys, including redirects, see [[Module:i18n/datatype]].
		val = page.keys[key]
	end
	if not val then
		return '<' .. key .. '>'
	end
	return p._langSwitch(val, lang)
end

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:preprocess("{{Int:Lang}}")
	end
	if frame.args[2] then
		page = page[mw.text.trim(frame.args[2])]
	end
	return p._langSwitch(page, lang)
end

 -- This test does not work ('Module:Fallback/tests/fallbacks' is missing)
function p.runTests()
	local toFallbackTest = require('Module:Fallback/tests/fallbacks')
	local result = true
	mw.log('Testing fallback chains')
	for i, t in ipairs(toFallbackTest) do
		local fbtbl = table.concat(p.fblist(t.initial), ', ')
		local expected = table.concat(t.expected, ', ')
		local ret = (fbtbl == expected)
		mw.log(i, ret and 'passed' or 'FAILED', t.initial, (not ret) and ('FAILED\nis >>' .. fbtbl .. '<<\nbut should be >>' .. expected .. '<<\n') or '')
		result = result and ret
	end
	return result
end

--[==[
List all input arguments of the template that calls "{{#invoke:Fallback|showTemplateArguments}}"
]==]
function p.showTemplateArguments(frame)
	local str = ''
	for name, value in pairs( frame:getParent().args ) do
		if str == '' then
			str = string.format('%s=%s', name, value)          -- argument #1
		else
			str = string.format('%s, %s=%s', str, name, value) -- the rest
		end
	end
	return str
end

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