Module:Autotranslate: Difference between revisions
Appearance
fallback |
تست جدید |
||
| Line 47: | Line 47: | ||
args = normalize_input_args(frame.args, args) | args = normalize_input_args(frame.args, args) | ||
-- Get a valid language code | |||
local lang = args.lang | |||
if type(lang) ~= 'string' | |||
or not mw.language.isSupportedLanguage(lang) then | |||
lang = frame:callParserFunction('int', 'lang') | |||
end | |||
-- Use wiki content language if the message is missing | |||
if type(lang) ~= 'string' | |||
or not mw.language.isSupportedLanguage(lang) then | |||
lang = mw.language.getContentLanguage():getCode() | |||
end | |||
-- Final fallback | |||
if not mw.language.isSupportedLanguage(lang) then | |||
lang = 'en' | |||
end | |||
args.lang = lang | |||
-- Build language fallback list | |||
local langList = mw.language.getFallbacksFor(lang) | |||
table.insert(langList, 1, lang) | |||
-- find base page | -- find base page | ||
local base = args.base | |||
args.base = nil | |||
assert( | |||
base and type(base) == 'string' and #base > 0, | |||
'Base page not provided for autotranslate' | |||
) | |||
-- Resolve unprefixed titles in the Template namespace | |||
local baseTitle = mw.title.new(base, 10) | |||
assert( | |||
baseTitle, | |||
'Invalid base page for autotranslate: ' .. base | |||
) | |||
base = baseTitle.prefixedText | |||
-- Local function for expanding a template that can be pcall()ed: call the template | -- Local function for expanding a template that can be pcall()ed: call the template | ||
Latest revision as of 19:59, 12 October 2026
Documentation for this module may be created at Module: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)
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 a valid language code
local lang = args.lang
if type(lang) ~= 'string'
or not mw.language.isSupportedLanguage(lang) then
lang = frame:callParserFunction('int', 'lang')
end
-- Use wiki content language if the message is missing
if type(lang) ~= 'string'
or not mw.language.isSupportedLanguage(lang) then
lang = mw.language.getContentLanguage():getCode()
end
-- Final fallback
if not mw.language.isSupportedLanguage(lang) then
lang = 'en'
end
args.lang = lang
-- Build language fallback list
local langList = mw.language.getFallbacksFor(lang)
table.insert(langList, 1, lang)
-- find base page
local base = args.base
args.base = nil
assert(
base and type(base) == 'string' and #base > 0,
'Base page not provided for autotranslate'
)
-- Resolve unprefixed titles in the Template namespace
local baseTitle = mw.title.new(base, 10)
assert(
baseTitle,
'Invalid base page for autotranslate: ' .. base
)
base = baseTitle.prefixedText
-- 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
local fallback_lang = 'en'
local fallback_page = base .. '/' .. fallback_lang
if not args.default then
if mw.title.new(fallback_page).exists then
args.default = fallback_page
end
end
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