feat(template/timeformat): backend time render synchronous with frontend

This commit is contained in:
kumfo 2022-12-05 14:56:18 +08:00
parent 4983482b32
commit aee0baba92
3 changed files with 19 additions and 36 deletions

View File

@ -187,16 +187,6 @@ backend:
other: "Your answer has been deleted"
your_comment_was_deleted:
other: "Your comment has been deleted"
dates:
long_date: "Jan 2"
long_date_with_year: "Jan 2, 2006"
long_date_with_time: "Jan 2, 2006 at 15:04"
now: now
x_seconds_ago: "{{count}}s ago"
x_minutes_ago: "{{count}}m ago"
x_hours_ago: "{{count}}h ago"
hour: hour
day: day
# The following fields are used for interface presentation(Front-end)
ui:
@ -384,10 +374,10 @@ ui:
slug_name:
label: URL Slug
description: 'Must use the character set "a-z", "0-9", "+ # - ."'
msg:
empty: URL slug cannot be empty.
range: URL slug up to 35 characters.
character: URL slug contains unallowed character set.
msg: "todo"
# empty: URL slug cannot be empty.
# range: URL slug up to 35 characters.
# character: URL slug contains unallowed character set.
description:
label: Description (optional)
btn_cancel: Cancel

View File

@ -171,14 +171,6 @@ backend:
other: "你的答案已被删除"
your_comment_was_deleted:
other: "你的评论已被删除"
dates:
long_date: "01月02日"
long_date_with_year: "2006年01月02日"
long_date_with_time: "2006年01月02日 15:04"
now: 刚刚
x_seconds_ago: '{{count}} 秒前'
x_minutes_ago: '{{count}} 分钟前'
x_hours_ago: '{{count}} 小时前'
# The following fields are used for interface presentation(Front-end)
ui:
how_to_format:
@ -361,10 +353,10 @@ ui:
slug_name:
label: URL 固定链接
description: '必须由 "a-z", "0-9", "+ # - ." 组成'
msg:
empty: 不能为空
range: 不能超过 35 个字符
character: 包含非法字符
msg: "todo"
# empty: 不能为空
# range: 不能超过 35 个字符
# character: 包含非法字符
description:
label: 标签描述(可选)
btn_cancel: 取消

View File

@ -3,6 +3,7 @@ package server
import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/day"
"html/template"
"io/fs"
"math"
@ -74,8 +75,8 @@ func NewHTTPServer(debug bool,
return time.Unix(timestamp, 0).Format("2006-01-02T15:04:05.000Z")
},
"translatorTimeFormatLongDate": func(la i18n.Language, tz string, timestamp int64) string {
trans := translator.GlobalTrans.Tr(la, "dates.long_date_with_time")
return time.Unix(timestamp, 0).Format(trans)
trans := translator.GlobalTrans.Tr(la, "ui.dates.long_date_with_time")
return day.Format(timestamp, trans, tz)
},
"translatorTimeFormat": func(la i18n.Language, tz string, timestamp int64) string {
var (
@ -89,35 +90,35 @@ func NewHTTPServer(debug bool,
}
if between <= 1 {
return translator.GlobalTrans.Tr(la, "dates.now")
return translator.GlobalTrans.Tr(la, "ui.dates.now")
}
if between > 1 && between < 60 {
trans = translator.GlobalTrans.Tr(la, "dates.x_seconds_ago")
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_seconds_ago")
return strings.ReplaceAll(trans, "{{count}}", converter.IntToString(between))
}
if between >= 60 && between < 3600 {
min := math.Floor(float64(between / 60))
trans = translator.GlobalTrans.Tr(la, "dates.x_minutes_ago")
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_minutes_ago")
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(min, 'f', 0, 64))
}
if between >= 3600 && between < 3600*24 {
h := math.Floor(float64(between / 60))
trans = translator.GlobalTrans.Tr(la, "dates.x_hours_ago")
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_hours_ago")
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(h, 'f', 0, 64))
}
if between >= 3600*24 &&
between < 3600*24*366 &&
time.Unix(timestamp, 0).Format("2006") == time.Unix(now, 0).Format("2006") {
trans = translator.GlobalTrans.Tr(la, "dates.long_date")
return time.Unix(timestamp, 0).Format(trans)
trans = translator.GlobalTrans.Tr(la, "ui.dates.long_date")
return day.Format(timestamp, trans, tz)
}
trans = translator.GlobalTrans.Tr(la, "dates.long_date_with_year")
return time.Unix(timestamp, 0).Format(trans)
trans = translator.GlobalTrans.Tr(la, "ui.dates.long_date_with_year")
return day.Format(timestamp, trans, tz)
},
"wrapComments": func(comments []*schema.GetCommentResp, la i18n.Language, tz string) map[string]interface{} {
return map[string]interface{}{