Editing Module:LatestUpdate

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
local p = {}
local p = {}


-- Load JSON content from a page
local data = mw.loadJsonData("Data:LatestUpdate.json")
local data = mw.loadJsonData("Data:LatestUpdate.json")
local update = data.latest_update or {}
local update = data.latest_update or {}


-- Month name → number mapping
local month_map = {
local month_map = {
     January = 1,
     January = 1,
Line 17: Line 19:
     November = 11,
     November = 11,
     December = 12
     December = 12
}
-- Plural rules
local plural_rules = {
    -- English
    en = function(n)
        return n == 1 and 1 or 2
    end,
    -- Spanish
    es = function(n)
        return n == 1 and 1 or 2
    end,
    -- French
    fr = function(n)
        return (n == 0 or n == 1) and 1 or 2
    end,
    -- Korean (no plural distinction)
    ko = function(n)
        return 1
    end,
    -- Russian
    ru = function(n)
        if n % 10 == 1 and n % 100 ~= 11 then
            return 1
        elseif n % 10 >= 2 and n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20) then
            return 2
        else
            return 3
        end
    end,
    -- Turkish
    tr = function(n)
        return 1
    end,
    -- Czech
    cs = function(n)
        if n == 1 then
            return 1
        elseif n >= 2 and n <= 4 then
            return 2
        else
            return 3
        end
    end,
    -- Chinese (simplified)
    ["zh-hans"] = function(n)
        return 1
    end,
    -- Chinese (traditional)
    ["zh-hant"] = function(n)
        return 1
    end,
    -- uncomment when needed
    -- pl = function(n)
    --    if n == 1 then return 1
    --    elseif n % 10 >= 2 and n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20) then return 2
    --    else return 3 end
    -- end,
    -- uk = function(n)
    --    if n % 10 == 1 and n % 100 ~= 11 then return 1
    --    elseif n % 10 >= 2 and n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20) then return 2
    --    else return 3 end
    -- end,
    -- de = function(n)
    --    return n == 1 and 1 or 2
    -- end,
    -- it = function(n)
    --    return n == 1 and 1 or 2
    -- end,
    -- pt = function(n)
    --    return n == 1 and 1 or 2
    -- end
}
local translations = {
    en = {
        [1] = "%d day ago",
        [2] = "%d days ago"
    },
    es = {
        [1] = "hace %d día",
        [2] = "hace %d días"
    },
    fr = {
        [1] = "il y a %d jour",
        [2] = "il y a %d jours"
    },
    ko = {
        [1] = "%d일 전"
    },
    ru = {
        [1] = "%d день назад",
        [2] = "%d дня назад",
        [3] = "%d дней назад"
    },
    tr = {
        [1] = "%d gün önce"
    },
    cs = {
        [1] = "před %d dnem",
        [2] = "před %d dny",
        [3] = "před %d dny"
    },
    ["zh-hans"] = {
        [1] = "%d天前"
    },
    ["zh-hant"] = {
        [1] = "%d天前"
    },
    -- uncomment when needed
    -- pl = {
    --    [1] = "%d dzień temu",
    --    [2] = "%d dni temu",
    --    [3] = "%d dni temu"
    -- },
    -- uk = {
    --    [1] = "%d день тому",
    --    [2] = "%d дні тому",
    --    [3] = "%d днів тому"
    -- },
    -- de = {
    --    [1] = "vor %d Tag",
    --    [2] = "vor %d Tagen"
    -- },
    -- it = {
    --    [1] = "%d giorno fa",
    --    [2] = "%d giorni fa"
    -- },
    -- pt = {
    --    [1] = "há %d dia",
    --    [2] = "há %d dias"
    -- }
}
-- Special-case wording for 0 and 1 days, keyed by day count.
-- A language missing an entry here falls back to the plural forms above.
local relative_days = {
    en = {
        [0] = "today",
        [1] = "yesterday"
    },
    es = {
        [0] = "hoy",
        [1] = "ayer"
    },
    fr = {
        [0] = "aujourd'hui",
        [1] = "hier"
    },
    ko = {
        [0] = "오늘",
        [1] = "어제"
    },
    ru = {
        [0] = "сегодня",
        [1] = "вчера"
    },
    tr = {
        [0] = "bugün",
        [1] = "dün"
    },
    cs = {
        [0] = "dnes",
        [1] = "včera"
    },
    ["zh-hans"] = {
        [0] = "今天",
        [1] = "昨天"
    },
    ["zh-hant"] = {
        [0] = "今天",
        [1] = "昨天"
    },
    -- uncomment when needed
    -- pl = {
    --    [0] = "dzisiaj",
    --    [1] = "wczoraj"
    -- },
    -- uk = {
    --    [0] = "сьогодні",
    --    [1] = "вчора"
    -- },
    -- de = {
    --    [0] = "heute",
    --    [1] = "gestern"
    -- },
    -- it = {
    --    [0] = "oggi",
    --    [1] = "ieri"
    -- },
    -- pt = {
    --    [0] = "hoje",
    --    [1] = "ontem"
    -- }
}
}


Line 273: Line 37:
end
end


local function getCurrentTime()
    local frame = mw.getCurrentFrame()
    local now_str = frame:callParserFunction("CURRENTTIMESTAMP")
    return os.time({
        year = tonumber(string.sub(now_str, 1, 4)),
        month = tonumber(string.sub(now_str, 5, 6)),
        day = tonumber(string.sub(now_str, 7, 8)),
        hour = 0
    })
end
-- Calculate days since last update
function p.days_since()
function p.days_since()
     local month_number = getMonthNumber(update.month)
     local month_number = getMonthNumber(update.month)
Line 287: Line 64:
     })
     })


     local now = os.time()
     local now = getCurrentTime()


     local diff_seconds = os.difftime(now, update_time)
     local diff_seconds = os.difftime(now, update_time)
Line 299: Line 76:
end
end


p.days_since_date = function(frame)
-- Return formatted text (e.g. "1 day ago", "5 days ago")
 
function p.days_since_text()
    local update_time = os.time({
        year = frame.args[3],
        month = frame.args[1],
        day = frame.args[2],
        hour = 0
    })
 
    local now = os.time()
 
    local diff_seconds = os.difftime(now, update_time)
    local days = math.floor(diff_seconds / 86400)
 
    if days < 0 then
        return 0
    end
 
    return days
end
 
local function normalize_lang(lang)
    if not lang or lang == "" then
        return "en"
    end
 
    return string.gsub(lang, "^/", "")
end
 
function p.days_since_text(frame)
    local args = frame.args or {}
 
    local lang = normalize_lang(args.lang or args[1] or "en")
 
    if not translations[lang] then
        lang = "en"
    end
 
     local days = p.days_since()
     local days = p.days_since()


     local relative = relative_days[lang]
     return days .. (days == 1 and " day ago" or " days ago")
    if relative and relative[days] then
        return relative[days]
    end
 
    local plural_rule = plural_rules[lang] or plural_rules.en
    local form_index = plural_rule(days)
 
    local template = translations[lang][form_index]
 
    return string.format(template, days)
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template