Module:Month translator: Difference between revisions

Content deleted Content added
create;
 
Replace Module:No globals with require( "strict" )
 
(11 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:month translator/data');
local langs = {'ca', 'de', 'es', 'pl', 'pt'};
 
local en_months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};
 
--[[--------------------------< _ M O N T H _ X L A T E >--------------------------------------------------------
local month_names = {};
 
{{#invoke:Sandbox/trappist the monk/monthMonth translator|month_xlate|<date>}}
local patterns = {
{'^(%d%d?) +(%a+) +(%d%d%d%d%a?)$', 'd', 'm', 'y', 'dmy'}, -- dd Mmm yyyy
{'^(%d%d?) +de +(%a+) +de +(%d%d%d%d%a?)$', 'd', 'm', 'y', 'dmy'}, -- dd de Mmm de yyyy
{'^(%a+) +(%d%d%d%d%a?)$', 'm', 'y', nil, 'my'}, -- Mmm yyyy
{'^(%a+) +de +(%d%d%d%d%a?)$', 'm', 'y', nil, 'my'}, -- Mmm de yyyy
};
 
 
--[[--------------------------< M O N T H _ L I S T _ G E T >--------------------------------------------------
 
TODO: move this to a data module so that it can be mw.loadData()'d
 
]]
 
local function month_names_get_month_xlate (args_t)
for _, lang in ipairs (langs) do -- spin through the languages table
local lang_obj = mw.getLanguage (lang); -- make a language object for the current language
for i, en_month in ipairs (en_months) do -- spin through the English month-names table
month_names[lang_obj:formatDate('F', en_month):lower()] = en_month; -- translate the English name to the current language and store in the translations table
if 'pl' == lang then -- for polish and other languages that have nominative and genitive forms
month_names[lang_obj:formatDate('xg', en_month):lower()] = en_month; -- translate English to genitive form and save
end
end
end
end
 
 
--[[--------------------------< M O N T H _ X L A T E >--------------------------------------------------------
 
{{#invoke:Sandbox/trappist the monk/month translator|month_xlate|<date>}}
 
]]
 
local function month_xlate (frame)
local t = {};
local day, month, year;
month_names_getif (); 'dump' == args_t[1] then -- buildargs_t[1] month= names'dump' list;to TODO:dump move<month_names_t> table; creation into a data module
return mw.dumpObject (month_namesdata.month_names_t);
if 'dump' == frame.args[1] then -- frame.args[1] = 'dump' to dump month_names table;
return mw.dumpObject (month_names);
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 (framemw.argstext.trim (args_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 rest of the captures
[pattern[3] or 'x'] = c2, -- take index names from pattern table and assign sequential captures
[pattern[4] or 'x'] = c3, -- index name may be nil in pattern table so "or 'x'" spoofs a name for this index in this table
};
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_names_t[month]; -- replace non-English name with English name from translation tables
year= t.y or '';
 
if month then
if month_names[month:lower()] then -- look in translation table for non-English month name; TODO: add support for an override table for when MediaWiki is wrong
local df = table.concat ({pattern[2], pattern[3], pattern[4]}, ''); -- extract date format from pattern table (pattern[2], pattern[3], pattern[4])
if 'dmy' == pattern[5] then -- for dmy dates
 
return table.concat ({day, month_names[month:lower()], year}, ' '); -- assemble an English language dmy date
elseifif 'mydmy' == pattern[5]df then -- for month yeardmy dates
return table.concat ({month_names[day, month:lower()], year}, ' '); -- assemble an English language dmy date
elseif 'my' == df then -- for month year dates
return table.concat ({day, month_names[month:lower()], 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 'm' == df then -- must be month (only valid option remaining)
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_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 I SA T _ G E T >--------------------------------------------------------
 
{{#invoke:Month translator|month_xlate|<date>}}
 
]]
 
local function month_xlate (frame)
return _month_xlate (getArgs (frame));
end
 
Line 81 ⟶ 69:
]]
 
return {
month_xlate = month_xlate};,
_month_xlate = _month_xlate
};