モジュール:サンドボックス/しまあじ/年月翻訳

モジュールの解説[作成]
local p = {}
local q = {}
local t = {}
local usub = mw.ustring.sub



function p.checkdate(frame)
    local timestamp = mw.text.trim(frame.args[1] or "")
    local default = mw.text.trim(frame.args["default"] or "")

    return q.checkdate(timestamp, default)
end



function q.checkdate(timestamp, default)
    if mw.ustring.match(timestamp, "#") then
        return default
    end
    local r = mw.ustring.lower(mw.ustring.gsub(timestamp, "[0-9]", "#"))
    local f = t[r]
    if f then
        return f(timestamp, default)
    else
        if mw.ustring.match(r, "^####年#月") then
            return usub(timestamp, 1, 7)
        end
        if mw.ustring.match(r, "^####[年%-]##月") then
            return t["####年##月"](timestamp, default)
        end
        if mw.ustring.match(r, "^####%-#月") then
            return t["####-#月"](timestamp, default)
        end
    end

    return default
end



t["####年#月"] = function (timestamp, default)
    return timestamp
end

t["####年##月"] = function (timestamp, default)
    local n = tonumber(usub(timestamp, 6, 7))
    if 1 <= n and n <= 12 then
        return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月"
    end

    return default
end

t["####年#月以前"] = t["####年#月"]

t["####年##月以前"] = function (timestamp, default)
    local n = tonumber(usub(timestamp, 6, 7))
    if 1 <= n and n <= 12 then
        return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月以前"
    end

    return default
end

t["####年#月以後"] = t["####年#月"]

t["####年##月以後"] = function (timestamp, default)
    local n = tonumber(usub(timestamp, 6, 7))
    if 1 <= n and n <= 12 then
        return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月以後"
    end

    return default
end

t["####-#月"] = function (timestamp, default)
    return usub(timestamp, 1, 4) .. "年" .. usub(timestamp, 6, 6) .. "月"
end

t["####-##月"] = t["####年##月"]

t["####-#月以前"] = function (timestamp, default)
    return usub(timestamp, 1, 4) .. "年" .. usub(timestamp, 6)
end

t["####-##月以前"] = t["####年##月以前"]
t["####-#月以後"] = t["####-#月以前"]
t["####-##月以後"] = t["####年##月以後"]

t["####-#-#"] = t["####-#月"]
t["####-#-##"] = t["####-#月"]
t["####-##-#"] = t["####年##月"]
t["####-##-##"] = t["####年##月"]



t["####-#"] = function (timestamp, default)
    return usub(timestamp, 1, 4) .. "年" .. usub(timestamp, -1) .. "月"
end

t["####-##"] = function (timestamp, default)
    local n = tonumber(usub(timestamp, -2))
    if 1 <= n and n <= 12 then
        return usub(timestamp, 1, 4) .. "年" .. tostring(n) .. "月"
    end

    return default
end

t["#####"] = t["####-#"]
t["######"] = t["####-##"]



t["january ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年1月"
end

t["february ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年2月"
end

t["march ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年3月"
end

t["april ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年4月"
end

t["may ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年5月"
end

t["june ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年6月"
end

t["july ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年7月"
end

t["august ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年8月"
end

t["september ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年9月"
end

t["october ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年10月"
end

t["november ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年11月"
end

t["december ####"] = function (timestamp, default)
    return usub(timestamp, -4) .. "年12月"
end

t["january, ####"] = t["january ####"]
t["february, ####"] = t["february ####"]
t["march, ####"] = t["march ####"]
t["april, ####"] = t["april ####"]
t["may, ####"] = t["may ####"]
t["june, ####"] = t["june ####"]
t["july, ####"] = t["july ####"]
t["august, ####"] = t["august ####"]
t["september, ####"] = t["august ####"]
t["october, ####"] = t["october ####"]
t["november, ####"] = t["november ####"]
t["december, ####"] = t["december ####"]

return p