feat(reason): translator report reasons.

This commit is contained in:
LinkinStars 2023-05-16 11:48:35 +08:00
parent b564e975a8
commit 952d846340
6 changed files with 63 additions and 35 deletions

View File

@ -250,41 +250,44 @@ backend:
upload:
unsupported_file_format:
other: Unsupported file format.
report:
reason:
spam:
name:
other: spam
desc:
other: This post is an advertisement, or vandalism. It is not useful or relevant
to the current topic.
rude:
other: This post is an advertisement, or vandalism. It is not useful or relevant to the current topic.
rude_or_abusive:
name:
other: rude or abusive
desc:
other: A reasonable person would find this content inappropriate for respectful
discourse.
duplicate:
a_duplicate:
name:
other: a duplicate
desc:
other: This question has been asked before and already has an answer.
not_answer:
placeholder:
other: Enter the existing question link
not_a_answer:
name:
other: not an answer
desc:
other: This was posted as an answer, but it does not attempt to answer the
question. It should possibly be an edit, a comment, another question,
or deleted altogether.
not_need:
no_longer_needed:
name:
other: no longer needed
desc:
other: This comment is outdated, conversational or not relevant to this post.
other:
something:
name:
other: something else
desc:
other: This post requires staff attention for another reason not listed above.
placeholder:
other: Let us know specifically what you are concerned about
question:
close:
duplicate:

View File

@ -243,37 +243,41 @@ backend:
upload:
unsupported_file_format:
other: 不支持的文件格式。
report:
reason:
spam:
name:
other: 垃圾信息
desc:
other: 这个帖子是一个广告,或是破坏性行为。它对当前的主题没有用处,也不相关。
rude:
rude_or_abusive:
name:
other: 粗鲁或辱骂的
desc:
other: 一个有理智的人都会认为这种内容不适合进行尊重性的讨论。
duplicate:
a_duplicate:
name:
other: 重复信息
desc:
other: 此问题以前就有人问过,而且已经有了答案。
not_answer:
placeholder:
other: 请输入重复的问题的网址
not_a_answer:
name:
other: 不是答案
desc:
other: 此帖子是作为一个答案发布的,但它并没有试图回答这个问题。总之,它可能应该是个编辑,评论,另一个问题或者被删除。
not_need:
no_longer_needed:
name:
other: 不再需要
desc:
other: 此评论已过时,对话或与此帖子无关。
other:
something:
name:
other: 其他原因
desc:
other: 此帖子需要工作人员关注,因为是上述所列以外的其他理由。
placeholder:
other: 让我们具体了解你所关注的内容
question:
close:
duplicate:

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/config"
"github.com/answerdev/answer/internal/service/reason_common"
@ -21,43 +22,37 @@ func NewReasonRepo(configRepo config.ConfigRepo) reason_common.ReasonRepo {
}
}
func (rr *reasonRepo) ListReasons(ctx context.Context, objectType, action string) (resp []schema.ReasonItem, err error) {
var (
reasonAction = fmt.Sprintf("%s.%s.reasons", objectType, action)
reasonKeys []string
cfgValue string
)
resp = []schema.ReasonItem{}
func (rr *reasonRepo) ListReasons(ctx context.Context, objectType, action string) (resp []*schema.ReasonItem, err error) {
lang := handler.GetLangByCtx(ctx)
reasonAction := fmt.Sprintf("%s.%s.reasons", objectType, action)
resp = make([]*schema.ReasonItem, 0)
reasonKeys, err = rr.configRepo.GetArrayString(reasonAction)
reasonKeys, err := rr.configRepo.GetArrayString(reasonAction)
if err != nil {
return
return nil, err
}
for _, reasonKey := range reasonKeys {
var (
reasonType int
reason = schema.ReasonItem{}
)
cfgValue, err = rr.configRepo.GetString(reasonKey)
cfgValue, err := rr.configRepo.GetString(reasonKey)
if err != nil {
log.Error(err)
continue
}
err = json.Unmarshal([]byte(cfgValue), &reason)
reason := &schema.ReasonItem{}
err = json.Unmarshal([]byte(cfgValue), reason)
if err != nil {
log.Error(err)
continue
}
reasonType, err = rr.configRepo.GetConfigType(reasonKey)
reason.Translate(reasonKey+".", lang)
reason.ReasonType, err = rr.configRepo.GetConfigType(reasonKey)
if err != nil {
log.Error(err)
continue
}
reason.ReasonType = reasonType
resp = append(resp, reason)
}
return
return resp, nil
}

View File

@ -1,5 +1,10 @@
package schema
import (
"github.com/answerdev/answer/internal/base/translator"
"github.com/segmentfault/pacman/i18n"
)
type ReasonItem struct {
ReasonType int `json:"reason_type"`
Name string `json:"name"`
@ -14,3 +19,24 @@ type ReasonReq struct {
// Action
Action string `validate:"required" form:"action" json:"action"`
}
func (r *ReasonItem) Translate(keyPrefix string, lang i18n.Language) {
trField := func(fieldName, fieldData string) string {
// If fieldData is empty, means no need to translate
if len(fieldData) == 0 {
return fieldData
}
key := keyPrefix + fieldName
fieldTr := translator.Tr(lang, key)
if fieldTr != key {
// If i18n key exists, return i18n value
return fieldTr
}
// If i18n key not exists, return fieldData original value
return fieldData
}
r.Name = trField("name", r.Name)
r.Description = trField("desc", r.Description)
r.Placeholder = trField("placeholder", r.Placeholder)
}

View File

@ -17,6 +17,6 @@ func NewReasonService(reasonRepo reason_common.ReasonRepo) *ReasonService {
}
}
func (rs ReasonService) GetReasons(ctx context.Context, req schema.ReasonReq) (resp []schema.ReasonItem, err error) {
func (rs ReasonService) GetReasons(ctx context.Context, req schema.ReasonReq) (resp []*schema.ReasonItem, err error) {
return rs.reasonRepo.ListReasons(ctx, req.ObjectType, req.Action)
}

View File

@ -7,5 +7,5 @@ import (
)
type ReasonRepo interface {
ListReasons(ctx context.Context, objectType, action string) (resp []schema.ReasonItem, err error)
ListReasons(ctx context.Context, objectType, action string) (resp []*schema.ReasonItem, err error)
}