Module:Month translator

require('strict')
local getArgs = require ('Module:Arguments').getArgs;

local data = mw.loadData ('Module:month translator/data');


--[[--------------------------< _ M O N T H _ X L A T E >------------------------------------------------------

{{#invoke:Month translator|month_xlate|<date>}}

]]

local function _month_xlate (args_t)
	local t = {};
	local day, month, year;
	
	if 'dump' == args_t[1] then													-- args_t[1] = 'dump' to dump <month_names_t> table; 
		return mw.dumpObject (data.month_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 (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 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
				local df = table.concat ({pattern[2], pattern[3], pattern[4]}, '');	-- extract date format from pattern table (pattern[2], pattern[3], pattern[4])

				if 'dmy' == df then												-- for dmy dates
					return table.concat ({day, month, year}, ' ');				-- assemble an English language dmy date
				elseif 'my' == df then											-- for month year dates
					return table.concat ({month, year}, ' ');					-- assemble an English language dmy date
				elseif 'mdy' == df then											-- for mdy 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 args_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


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	month_xlate = month_xlate,
	_month_xlate = _month_xlate
	};

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.