Module:Month translator: Difference between revisions

Content deleted Content added
No edit summary
Replace Module:No globals with require( "strict" )
 
(7 intermediate revisions by one other user not shown)
Line 1:
require ('Module:No globalsstrict')
local getArgs = require ('Module:Arguments').getArgs;
 
local data = mw.loadData ('Module:Sandbox/trappist the monk/month translator/data');
 
 
--[[--------------------------< _ M O N T H _ X L A T E >--------------------------------------------------------
 
{{#invoke:Sandbox/trappist the monk/monthMonth translator|month_xlate|<date>}}
 
]]
 
local function month_xlate_month_xlate (frameargs_t)
local t = {};
local day, month, year;
if 'dump' == frame.argsargs_t[1] then -- frame.argsargs_t[1] = 'dump' to dump month_names<month_names_t> table;
return mw.dumpObject (data.month_namesmonth_names_t);
end
if not args_t[1] then return nil end
for i, pattern in ipairs (data.patterns) do -- spin through the patterns table looking for a match
local c1, c2, c3; -- captures in the 'pattern' from the pattern table go here
 
c1, c2, c3 = mw.ustring.match (mw.text.trim (frame.argsargs_t[1]), pattern[1]); -- one or more captures set if source matches patterns[i][1])
if c1 then -- c1 always set on match
 
t = {
[pattern[2] or 'x'] = c1, -- fill the table of captures with the captures
Line 30 ⟶ 32:
day = t.d or ''; -- translate table contents to named variables;
month = mw.ustring.lower (t.m or ''); -- absent table entries are nil so set unused parts to empty string; lowercase for indexing
month = data.override_names[month] or data.month_namesmonth_names_t[month]; -- replace non-English name with English name from translation tables
year= t.y or '';
 
if month then
local df = table.concat ({pattern[2], ''pattern[3], 2pattern[4]}, ''); -- extract date format from pattern table (pattern[2], pattern[3], pattern[4])
 
-- if 'dmy' == pattern[5] then -- for dmy dates
if 'dmy' == df then -- for dmy dates
return table.concat ({day, month, year}, ' '); -- assemble an English language dmy date
-- elseif 'my' == pattern[5]df then -- for month year dates
elseif 'my' == df then -- for month year dates
return table.concat ({month, year}, ' '); -- assemble an English language dmy date
-- ifelseif 'dmymdy' == pattern[5]df then -- for dmymdy dates
return string.format ('%s %s, %s', month, day, year); -- assemble an English language mdy date
elseif 'mym' == df then -- formust be month year(only valid option datesremaining)
return month; -- none of the above, return the translated month;
end
end
break; -- and done; if here found pattern match but did not find non-English month name in month_names{}<month_names_t>
end
end
return frame.argsargs_t[1]; -- if here, couldn't translate so return the original date
end
 
 
--[[--------------------------< M O N T H _ X L A T E >--------------------------------------------------------
 
{{#invoke:Month translator|month_xlate|<date>}}
 
]]
 
local function month_xlate (frame)
return _month_xlate (getArgs (frame));
end
 
Line 53 ⟶ 69:
]]
 
return {
month_xlate = month_xlate};,
_month_xlate = _month_xlate
};