Module:DateI18n: Difference between revisions
Jump to navigation
Jump to search
Richardpruen (talk | contribs) m (1 revision imported) |
Richardpruen (talk | contribs) m (1 revision imported) |
||
Line 27: | Line 27: | ||
return val | return val | ||
elseif type(val) == 'number' then | elseif type(val) == 'number' then | ||
if val == 1 then | if val==1 then | ||
return true | return true | ||
elseif val == 0 then | elseif val==0 then | ||
return false | return false | ||
end | end | ||
elseif type(val) == 'string' then | elseif type(val) == 'string' then | ||
val = | val = mw.ustring.lower(val) -- put in lower case | ||
if val == 'no' or val == 'n' or val == 'false' or tonumber(val) == 0 then | if val == 'no' or val == 'n' or val == 'false' or tonumber(val) == 0 then | ||
return false | return false | ||
Line 66: | Line 66: | ||
-- down date-time component strings or numbers | -- down date-time component strings or numbers | ||
-- OUTPUT: | -- OUTPUT: | ||
-- * datecode - a code specifying content of the array where Y' is year, 'M' is month, 'D' is day, | -- * datecode - a code specifying content of the array where Y' is year, 'M' is month, | ||
-- 'D' is day, 'H' is hour, 'M' minute, 'S' is second. output has to be one of YMDHMS, YMDHM, YMD, YM, MD, Y | |||
-- * datenum - same array but holding only numbers or nuls | -- * datenum - same array but holding only numbers or nuls | ||
local function parserDatevec(datevec) | local function parserDatevec(datevec) | ||
-- if month is not a number than check if it is a month name in project's language | |||
local month = datevec[2] | |||
if month and month~='' and not tonumber(month) then | |||
datevec[2] = mw.getContentLanguage():formatDate( "n", month) | |||
end | |||
-- create datecode based on which variables are provided and check for out-of-bound values | -- create datecode based on which variables are provided and check for out-of-bound values | ||
local maxval = { | local maxval = {nil, 12, 31, 23, 59, 59, 23, 59} -- max values for year, month, ... | ||
local minval = { | local minval = {nil, 1, 1, 0, 0, 0, -23, 0} -- min values for year, month, ... | ||
local | local c = {'Y', 'M', 'D', 'H', 'M', 'S', '', ''} | ||
local datecode = '' -- a string signifying which combination of variables was provided | local datecode = '' -- a string signifying which combination of variables was provided | ||
local datenum = {} -- date-time encoded as a vector = [year, month, ... , second | local datenum = {} -- date-time encoded as a vector = [year, month, ... , second] | ||
for i = 1, 8 do | for i = 1,8 do | ||
datenum[i] = tonumber(datevec[i]) | |||
if datenum[i] and (i==1 or (datenum[i]>=minval[i] and datenum[i]<=maxval[i])) then | |||
datecode = datecode .. c[i] | |||
datecode = datecode .. c | |||
end | end | ||
end | end | ||
Line 107: | Line 94: | ||
-- process datevec | -- process datevec | ||
-- INPUT: | -- INPUT: | ||
-- * datecode - a code specifying content of the array where Y' is year, 'M' is month, | -- * datecode - a code specifying content of the array where Y' is year, 'M' is month, | ||
-- 'D' is day, 'H' is hour, ' | -- 'D' is day, 'H' is hour, 'M' minute, 'S' is second. output has to be one of YMDHMS, YMDHM, YMD, YM, MD, Y | ||
-- * datenum - Array of {year,month,day,hour,minute,second, tzhour, tzmin} as numbers or nuls | -- * datenum - Array of {year,month,day,hour,minute,second, tzhour, tzmin} as numbers or nuls | ||
-- OUTPUT: | -- OUTPUT: | ||
-- * timeStamp - date string in the format taken by mw.language:formatDate lua function and {{#time}} | -- * timeStamp - date string in the format taken by mw.language:formatDate lua function and {{#time}} perser function | ||
-- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.language:formatDate | -- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.language:formatDate | ||
-- https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time | -- https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time | ||
-- * datecode - with possible corrections | -- * datecode - with possible corrections | ||
local function getTimestamp(datecode, datenum) | local function getTimestamp(datecode, datenum) | ||
-- create | -- create time stamp string (for example 2000-02-20 02:20:20) based on which variables were provided | ||
local timeStamp | local timeStamp | ||
if datecode == 'YMDHMS' then | |||
if datecode == ' | |||
timeStamp = string.format('%04i-%02i-%02i %02i:%02i:%02i', datenum[1], datenum[2], datenum[3], datenum[4], datenum[5], datenum[6] ) | timeStamp = string.format('%04i-%02i-%02i %02i:%02i:%02i', datenum[1], datenum[2], datenum[3], datenum[4], datenum[5], datenum[6] ) | ||
elseif datecode == ' | elseif datecode == 'YMDHM' then | ||
timeStamp = string.format('%04i-%02i-%02i %02i:%02i', datenum[1], datenum[2], datenum[3], datenum[4], datenum[5] ) | timeStamp = string.format('%04i-%02i-%02i %02i:%02i', datenum[1], datenum[2], datenum[3], datenum[4], datenum[5] ) | ||
elseif datecode:sub(1,3)=='YMD' then | elseif datecode:sub(1,3)=='YMD' then | ||
timeStamp = string.format('%04i-%02i-%02i', datenum[1], datenum[2], datenum[3] ) | timeStamp = string.format('%04i-%02i-%02i', datenum[1], datenum[2], datenum[3] ) | ||
datecode = 'YMD' -- ' | datecode = 'YMD' -- 'YMD', 'YMDHMS' and 'YMDHM' are the only supported format starting with 'YMD'. All others will be converted to 'YMD' | ||
elseif datecode | elseif datecode == 'YM' then | ||
timeStamp = string.format('%04i-%02i', datenum[1], datenum[2] ) | timeStamp = string.format('%04i-%02i', datenum[1], datenum[2] ) | ||
elseif datecode:sub(1,1)=='Y' then | elseif datecode:sub(1,1)=='Y' then | ||
timeStamp = string.format('%04i', datenum[1] ) | timeStamp = string.format('%04i', datenum[1] ) | ||
datecode = 'Y' | datecode = 'Y' | ||
elseif datecode == 'M' then | |||
elseif datecode== ' | timeStamp = string.format('%04i-%02i-%02i', 2000, datenum[2], 1 ) | ||
timeStamp = string.format('%04i-%02i- | elseif datecode == 'MD' then | ||
elseif datecode | |||
timeStamp = string.format('%04i-%02i-%02i', 2000, datenum[2], datenum[3] ) | timeStamp = string.format('%04i-%02i-%02i', 2000, datenum[2], datenum[3] ) | ||
else | else | ||
timeStamp = nil -- format not supported | timeStamp = nil -- format not supported | ||
Line 182: | Line 159: | ||
-- Look up proper format string to be passed to {{#time}} parser function | -- Look up proper format string to be passed to {{#time}} parser function | ||
-- INPUTS: | -- INPUTS: | ||
-- * datecode: | -- * datecode: YMDHMS, YMDHM, YMD, YM, MD, Y, or M | ||
-- * day : Number between 1 and 31 (not needed for most languages) | -- * day : Number between 1 and 31 (not needed for most languages) | ||
-- * lang : language | -- * lang : language | ||
Line 190: | Line 167: | ||
local function parseFormat(dFormat, day) | local function parseFormat(dFormat, day) | ||
if dFormat:find('default') and #dFormat>10 then | if dFormat:find('default') and #dFormat>10 then | ||
-- | -- special (and messy) case of dFormat code depending on a day number | ||
-- | -- then json contains a string with more json containing "default" field and 2 digit day keys | ||
-- | -- if desired day is not in that json than use "default" case | ||
-- to straight | dFormat = dFormat:gsub('”','"') -- change fancy double quote to a straight one, used for json marking | ||
local D = mw.text.jsonDecode | local D = mw.text.jsonDecode( dFormat ) --com = mw.dumpObject(D) | ||
day = string.format('d%02i',day) -- create day key | |||
dFormat = D[day] or D.default | |||
dFormat = dFormat:gsub("'", '"') -- change single quote to a double quote, used for {{#time}} marking | |||
end | end | ||
return dFormat | return dFormat | ||
end | end | ||
Line 211: | Line 185: | ||
T[id] = msg | T[id] = msg | ||
end | end | ||
local dFormat = T[datecode] | local dFormat = T[datecode] | ||
if dFormat == 'default' and (datecode == ' | if dFormat=='default' and (datecode=='YMDHMS' or datecode=='YMDHM') then | ||
-- | -- for most languages adding hour:minute:second is done by adding ", HH:MM:SS to the | ||
-- day precission date, those languages are skipped in DateI18n.tab and default to | -- day precission date, those languages are skipped in DateI18n.tab and default to | ||
-- English which stores word "default" | -- English which stores word "default" | ||
dFormat = parseFormat(T['YMD'], day).. ', H:i' | dFormat = parseFormat(T['YMD'], day).. ', H:i' | ||
if datecode == ' | if datecode=='YMDHMS' then | ||
dFormat = dFormat .. ':s' | dFormat = dFormat .. ':s' | ||
end | end | ||
Line 275: | Line 244: | ||
lang = mw.getCurrentFrame():callParserFunction( "int", "lang" ) -- get user's chosen language | lang = mw.getCurrentFrame():callParserFunction( "int", "lang" ) -- get user's chosen language | ||
end | end | ||
if lang == 'be- | if lang == 'be-tarsk' then | ||
lang = 'be-x-old' | lang = 'be-x-old' | ||
end | end | ||
Line 296: | Line 265: | ||
-- phrases as it is done in [[c:Module:Complex date]] | -- phrases as it is done in [[c:Module:Complex date]] | ||
case = case or '' | case = case or '' | ||
if (lang=='qu' or lang=='qug') and case=='nom' then | if (lang=='qu' or lang=='qug') and (case=='nom') then | ||
-- Special case related to Quechua and Kichwa languages. The form in the I18n is | -- Special case related to Quechua and Kichwa languages. The form in the I18n is | ||
-- Genitive case with suffix "pi" added to month names provided by {#time}} | -- Genitive case with suffix "pi" added to month names provided by {#time}} | ||
Line 302: | Line 271: | ||
-- see https://commons.wikimedia.org/wiki/Template_talk:Date#Quechua from 2014 | -- see https://commons.wikimedia.org/wiki/Template_talk:Date#Quechua from 2014 | ||
dFormat = dFormat:gsub('F"pi"', 'F') | dFormat = dFormat:gsub('F"pi"', 'F') | ||
elseif case == 'gen' then | elseif (case=='gen') then | ||
dFormat = strReplace(dFormat, "F", "xg") | dFormat = strReplace(dFormat, "F", "xg") | ||
elseif case == 'nom' then | elseif (case=='nom') then | ||
dFormat = strReplace(dFormat, "xg", "F") | dFormat = strReplace(dFormat, "xg", "F") | ||
elseif case ~= '' then | elseif (case ~= '') then | ||
-- see is page [[Data:I18n/MonthCases.tab]] on Commons have name of the month | -- see is page [[Data:I18n/MonthCases.tab]] on Commons have name of the month | ||
-- in specific gramatic case in desired language. If we have it than replace | -- in specific gramatic case in desired language. If we have it than replace | ||
Line 317: | Line 286: | ||
end | end | ||
-- Translate the date using specified format | -- Translate the date using specified format | ||
-- See https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.language:formatDate and | -- See https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.language:formatDate and | ||
-- https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##time for explanation of the format | -- https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##time for explanation of the format | ||
local | local datestr = mw.language.new(lang):formatDate( dFormat, timeStamp) -- same as using {{#time}} parser function | ||
-- Special case related to Thai solar calendar: prior to 1940 new-year was at different time of year, | -- Special case related to Thai solar calendar: prior to 1940 new-year was at different | ||
-- time of year, so just year (datecode=='Y') is ambiguous and is replaced by "YYYY or YYYY" phrase | |||
if lang=='th' and datecode=='Y' and year<=1940 then | if lang=='th' and datecode=='Y' and year<=1940 then | ||
datestr = string.format('%04i หรือ %04i', year+542, year+543 ) | datestr = string.format('%04i หรือ %04i', year+542, year+543 ) | ||
end | end | ||
-- If year < 1000 than either keep the date padded to the length of 4 digits or trim it | -- If year<1000 than either keep the date padded to the length of 4 digits or trim it | ||
-- | -- decide if the year will stay padded with zeros (for years in 0-999 range) | ||
if year and year < 1000 then | if year and year<1000 then | ||
trim_year = | if type(trim_year)=='nil' then | ||
if type(trim_year) == 'string' then | trim_year = '100-999' | ||
-- | end | ||
-- | local trim = yesno(trim_year,nil) -- convert to boolean | ||
if trim==nil and type(trim_year)=='string' then | |||
-- if "trim_year" not a simple True/False than it is range of dates | |||
-- for example '100-999' means to pad one and 2 digit years to be 4 digit long, while keeping 3 digit years as is | |||
local YMin, YMax = trim_year:match( '(%d+)-(%d+)' ) | local YMin, YMax = trim_year:match( '(%d+)-(%d+)' ) | ||
trim = (YMin~=nil and year>=tonumber(YMin) and year<=tonumber(YMax)) | |||
end | end | ||
if | if trim==true then | ||
datestr = trimYear(datestr, year, lang) -- in datestr replace long year with trimmed one | datestr = trimYear(datestr, year, lang) -- in datestr replace long year with trimmed one | ||
end | end | ||
end | end | ||
-- | -- append timezone if present | ||
if datenum[7] and (datecode | if datenum[7] and (datecode == 'YMDHMS' or datecode == 'YMDHM') then | ||
-- | -- use {{#time}} parser function to create timezone string, so that we use correct character set | ||
local sign = (datenum[7]<0) and '−' or '+' | local sign = (datenum[7]<0) and '−' or '+' | ||
timeStamp = string.format("2000-01-01 %02i:%02i:00", math.abs(datenum[7]), datenum[8] or 0) | timeStamp = string.format("2000-01-01 %02i:%02i:00", math.abs(datenum[7]), datenum[8] or 0) | ||
local timezone = | local timezone = mw.language.new(lang):formatDate( 'H:i', timeStamp) -- same as using {{#time}} parser function | ||
datestr = string.format("%s %s%s", datestr, sign, timezone ) | datestr = string.format("%s %s%s", datestr, sign, timezone ) | ||
end | end | ||
-- | -- html formating and tagging of date string | ||
if class and class ~= '' and datecode | if class and class ~= '' and datecode~='M' and datecode~='MD'then | ||
local DateHtmlTags = '<span style="white-space:nowrap"><time class="%s" datetime="%s">%s</time></span>' | |||
datestr = DateHtmlTags:format(class, timeStamp, datestr) | |||
end | end | ||
return datestr | return datestr |