मॉड्यूल:Autotranslate
दिखावट
इस मॉड्यूल हेतु प्रलेख मॉड्यूल:Autotranslate/doc पर बनाया जा सकता है
--[[
__ __ _ _ _ _ _ _ _
| \/ | ___ __| |_ _| | ___ _ / \ _ _| |_ ___ | |_ _ __ __ _ _ __ ___| | __ _| |_ ___
| |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \
| | | | (_) | (_| | |_| | | __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | || __/
|_| |_|\___/ \__,_|\__,_|_|\___(_)_/ \_\__,_|\__\___/ \__|_| \__,_|_| |_|___/_|\__,_|\__\___|
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)
]]
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
-- FIXED: Use content language code directly instead of parser function
if not args.lang or not mw.language.isSupportedLanguage(args.lang) then
args.lang = mw.language.getContentLanguage():getCode() -- get wiki's content 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
if not base or #base == 0 then
return '<strong class="error">Base page not provided for autotranslate</strong>'
end
-- Local function for expanding a template that can be pcall()ed
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
-- If no translation found, try default
if not success and args.default then
success, res = pcall(expandTemplate, args.default)
end
-- If still no success, try English as final fallback
if not success then
success, res = pcall(expandTemplate, base .. '/en')
end
-- If everything fails, return error message
if not success then
local err_msg = string.format(
'<strong class="error">No fallback page found for autotranslate (base=%s, lang=%s)</strong>',
base,
args.lang or 'unknown'
)
return err_msg
end
-- If this is the base page being translated
if mw.title.getCurrentTitle().fullText == base then
if mw.site.siteName == 'Wikimedia Commons' then
res = res .. '\n[[Category:Autotranslated templates|' .. base .. ']]'
end
end
return res
end
return p