Bước tới nội dung

Mô đun:Ngày chuẩn

Bách khoa toàn thư mở Wikipedia
-- Mô đun có chức năng chuyển ngày tháng năm từ tiếng Anh hoặc định dạng ISO sang tiếng Việt
-- Ví dụ như các định dạng ngày như "1 January 2011", "January 1, 2011" và "2011-01-01"
-- Có thể chuyển thành dạng tiếng Việt "ngày 1 tháng 1 năm 2011" hoặc "1 tháng 1 năm 2011"
-- Hàm được nhập từ Mô đun:Citation/CS1

local contentLanguage = mw.getContentLanguage()

local p = {}

local function vi_formatdate(rawDate, dayPrefix)
	local formatStr = 'j "tháng" n "năm" Y'
	rawDate = mw.ustring.gsub(rawDate, "Tháng", "tháng", 1)
	if dayPrefix then formatStr = '"ngày" ' .. formatStr end
	
	-- Khoảng năm "c." hoặc "k."
	if mw.ustring.match(rawDate, "^%s*[ck]%.%s*%d%d%d%d?$") then
		rawDate = mw.ustring.gsub(rawDate, "c.", "k.", 1)
		return mw.text.trim(rawDate)
	end
	-- Năm
	if tonumber(rawDate) or mw.ustring.match(rawDate, "^[1-9]%d%d%d?%a?$") then
		return mw.text.trim(rawDate)
	-- Năm tháng
	elseif mw.ustring.match(rawDate, "^[Tt]háng %d%d?,? %d%d%d%d?$") or
		mw.ustring.match(rawDate, "^[Tt]háng %d%d?,? năm %d%d%d%d?$") or
		mw.ustring.match(rawDate, "^[Tt]háng %a+,? năm %d%d%d%d?$") or
		mw.ustring.match(rawDate, "^%a+%.?,? %d%d%d%d?$") or
		mw.ustring.match(rawDate, "^%d%d%d%d?%-%d%d$") then
		rawDate = mw.ustring.gsub(rawDate, ",", "")
		formatStr = '"tháng" n "năm" Y'
	-- Ngày tháng năm
	elseif mw.ustring.match(rawDate, "^[Nn]gày %d%d? tháng %d%d? năm %d%d%d%d?$") then
		local day, month, year = mw.ustring.match(rawDate, "^[Nn]gày (%d%d?) tháng (%d%d?) năm (%d%d%d%d?)$")
		rawDate = string.format("%s-%s-%s", year, month, day)
	elseif mw.ustring.match(rawDate, "^%d%d? tháng %d%d? năm %d%d%d%d?$") then
		local day, month, year = mw.ustring.match(rawDate, "^(%d%d?) tháng (%d%d?) năm (%d%d%d%d?)$")
		rawDate = string.format("%s-%s-%s", year, month, day)
	end
	
	local good, formattedDate = pcall(contentLanguage.formatDate, contentLanguage, formatStr, rawDate)
	if good then
		-- Xóa bỏ số 0 ở ngày có năm 3 chữ số như "ngày 1 tháng 1 năm 0789" thành "ngày 1 tháng 1 năm 789"
		formattedDate = mw.ustring.gsub(formattedDate, "năm 0", "năm ")
		return formattedDate
	else
		return rawDate
	end
end

function p._vi_formatdate(frame)
	local dayPrefix = frame.args.prefix == "yes"
	return vi_formatdate(frame.args[1], dayPrefix)
end

p.vi_formatdate = vi_formatdate

return p