Difference between pages "Module:Autotranslate" and "Module:Autotranslate/sandbox"

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
m (1 revision imported)
 
m (1 revision imported)
 
Line 1: Line 1:
 
   --[[
 
   --[[
   __  __          _      _          _        _        _                      _      _      
+
   __  __          _      _          _        _        _                      _      _
  |  \/  | ___  __| |_  _| | ___ _  / \  _  _| |_ ___ | |_ _ __ __ _ _ __  ___| | __ _| |_ ___  
+
  |  \/  | ___  __| |_  _| | ___ _  / \  _  _| |_ ___ | |_ _ __ __ _ _ __  ___| | __ _| |_ ___
 
  | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \
 
  | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \
 
  | |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | ||  __/
 
  | |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | ||  __/
 
  |_|  |_|\___/ \__,_|\__,_|_|\___(_)_/  \_\__,_|\__\___/ \__|_|  \__,_|_| |_|___/_|\__,_|\__\___|
 
  |_|  |_|\___/ \__,_|\__,_|_|\___(_)_/  \_\__,_|\__\___/ \__|_|  \__,_|_| |_|___/_|\__,_|\__\___|
+
 
 
  Authors and maintainers:
 
  Authors and maintainers:
 
* User:Zolo  - original version
 
* User:Zolo  - original version
* User:Jarekt  
+
* User:Jarekt
 
]]
 
]]
 +
 +
local checkType = require("libraryUtil").checkType
  
 
-- local function to help normalize input arguments
 
-- local function to help normalize input arguments
local function normalize_input_args(input_args, output_args)
+
local function normalize_input_args(input_args, output_args, strict_args)
for name, value in pairs( input_args ) do  
+
for name, value in pairs( input_args ) do
if value ~= '' then -- nuke empty strings
+
if strict_args then
 +
output_args[name] = value
 +
elseif value ~= '' then -- nuke empty strings
 
if type(name)=='string' then name=string.lower(name) end -- convert to lower case
 
if type(name)=='string' then name=string.lower(name) end -- convert to lower case
 
output_args[name] = value
 
output_args[name] = value
Line 27: Line 31:
 
--[[
 
--[[
 
autotranslate
 
autotranslate
+
 
This function is the core part of the Autotranslate template.  
+
This function is the core part of the Autotranslate template.
+
 
 
Usage from a template:
 
Usage from a template:
 
{{#invoke:autotranslate|autotranslate|base=|lang= }}
 
{{#invoke:autotranslate|autotranslate|base=|lang= }}
+
 
 
Parameters:
 
Parameters:
 
   frame.args.base - base page name
 
   frame.args.base - base page name
Line 40: Line 44:
  
 
]]
 
]]
function p.autotranslate(frame)  
+
function p.autotranslate(frame)
 
 
 
-- switch to lowercase parameters to make them case independent
 
-- switch to lowercase parameters to make them case independent
 
local args = {}
 
local args = {}
 
args = normalize_input_args(frame:getParent().args, args)
 
args = normalize_input_args(frame:getParent().args, args)
 
args = normalize_input_args(frame.args, args)
 
args = normalize_input_args(frame.args, args)
 +
 +
return p._autotranslate(args, frame)
 +
end
 +
 +
--[[
 +
autotranslateStrict
 +
 +
This function is the core part of the Autotranslate module.
 +
 +
Unlike `autotranslate`, this function doesn't convert parameters
 +
to lowercase and preserves blank parameters.
 +
 +
Parameters:
 +
  frame.args.base - base page name
 +
  frame.args.lang - desired language (often user's native language)
 +
]]
 +
function p.autotranslateStrict(frame)
 +
local args = {}
 +
args = normalize_input_args(frame:getParent().args, args, true)
 +
args = normalize_input_args(frame.args, args, true)
 +
 +
return p._autotranslate(args, frame)
 +
end
 +
 +
function p._autotranslate(args, frame)
 +
checkType( "p._autotranslate", 1, args, "table" )
 +
checkType( "p._autotranslate", 2, frame, "table" )
  
 
-- 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)
Line 59: Line 89:
 
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
 
if not mw.ustring.find(base,':') then  -- if base page does not indicate namespace
base = 'Template:' .. base          -- than assume it is a template  
+
base = 'Template:' .. base          -- than assume it is a template
 
end
 
end
+
 
 
-- find base template language subpage
 
-- find base template language subpage
 
local page = args.default  -- default page if provided or nil otherwise
 
local page = args.default  -- default page if provided or nil otherwise
Line 71: Line 101:
 
end
 
end
 
assert(page, string.format('No fallback page found for autotranslate (base=[[%s]], lang=%s)', base, args.lang))
 
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 = args}