Editing
Module:Autotranslate/sandbox
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
--[[ __ __ _ _ _ _ _ _ _ | \/ | ___ __| |_ _| | ___ _ / \ _ _| |_ ___ | |_ _ __ __ _ _ __ ___| | __ _| |_ ___ | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \ | | | | (_) | (_| | |_| | | __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | || __/ |_| |_|\___/ \__,_|\__,_|_|\___(_)_/ \_\__,_|\__\___/ \__|_| \__,_|_| |_|___/_|\__,_|\__\___| Authors and maintainers: * User:Zolo - original version * User:Jarekt ]] local err_msg = 'No fallback page found for autotranslate (base=[[:%s]], lang=%s)' -- 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 ------------------------------------------------------------------------- local function fallback_list(lang) -- get language fallback list if not lang or not mw.language.isSupportedLanguage(lang) then lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language end local langList = mw.language.getFallbacksFor(lang) table.insert(langList,1,lang) -- user's language will be the first one to check return langList end ------------------------------------------------------------------------- local function verify(base) res = '' if (mw.title.getCurrentTitle().namespace==10) 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 = '\n[[Category:Autotranslated templates|' .. base .. ']]' end end return res 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 default 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) local langList = fallback_list(args.lang) -- single out autotranslate template parameters and exclude them from -- parameters passed to language sub-templates local base = args.base local lang = args.lang local default = args.default args.base = nil -- blank it args.lang = nil -- blank it args.default = nil -- blank it 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 if (not success) then assert(default, string.format(err_msg, base, lang)) success, res = pcall(expandTemplate, default) assert(success, string.format(err_msg, base, default)) end return res .. verify(base) end -------------------------------------------------------------------------------- function p.autotranslate_te(frame) -- version of autotranslate that works with translation extension -- 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) local langList = fallback_list(args.lang) -- single out autotranslate template parameters and exclude them from -- parameters passed to language sub-templates local base = args.base local lang = args.lang local default = args.default args.base = nil -- blank it args.lang = nil -- blank it args.default = nil -- blank it assert(base and #base>0, 'Base page not provided for autotranslate' ) -- find base template language subpage local page = default -- default page if provided or nil otherwise for _, language in ipairs(langList) do if mw.title.new(base .. '/' .. language).exists then page = base .. '/' .. language -- returns only the page break end end -- last check for errors. This one would be vary rarely used maybe a language which "isSupportedLanguage" but does -- not have any fallback languages, like qqx assert(page, string.format(err_msg, base, lang)) -- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template. local res = frame:expandTemplate{ title = page, args = args} return res .. verify(base) end return p
Summary:
Please note that all contributions to Islcamical Commons may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
commons:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Autotranslate/sandbox/doc
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Module
Discussion
English
Views
Read
Edit source
View history
More
Search
Navigation
Main page
welcome
Community portal
village pump
participate
Upload file
Recent changes
latestfiles
Random File
contact
Special pages
Tools
What links here
Related changes
Upload file
Page information