From 952d8463406584217ab560d987c4986948f041ca Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Tue, 16 May 2023 11:48:35 +0800 Subject: [PATCH] feat(reason): translator report reasons. --- i18n/en_US.yaml | 19 +++++++------ i18n/zh_CN.yaml | 16 ++++++----- internal/repo/reason/reason_repo.go | 33 ++++++++++------------- internal/schema/reason_schema.go | 26 ++++++++++++++++++ internal/service/reason/reason_service.go | 2 +- internal/service/reason_common/reason.go | 2 +- 6 files changed, 63 insertions(+), 35 deletions(-) diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index ee8d7088..a87f9174 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -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: diff --git a/i18n/zh_CN.yaml b/i18n/zh_CN.yaml index 778026af..c34afafc 100644 --- a/i18n/zh_CN.yaml +++ b/i18n/zh_CN.yaml @@ -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: diff --git a/internal/repo/reason/reason_repo.go b/internal/repo/reason/reason_repo.go index 171b4d85..1340a2b8 100644 --- a/internal/repo/reason/reason_repo.go +++ b/internal/repo/reason/reason_repo.go @@ -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 } diff --git a/internal/schema/reason_schema.go b/internal/schema/reason_schema.go index a6a833b2..ca48fbe6 100644 --- a/internal/schema/reason_schema.go +++ b/internal/schema/reason_schema.go @@ -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) +} diff --git a/internal/service/reason/reason_service.go b/internal/service/reason/reason_service.go index daa9cb54..d35f807f 100644 --- a/internal/service/reason/reason_service.go +++ b/internal/service/reason/reason_service.go @@ -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) } diff --git a/internal/service/reason_common/reason.go b/internal/service/reason_common/reason.go index 870f9ee9..43670ad0 100644 --- a/internal/service/reason_common/reason.go +++ b/internal/service/reason_common/reason.go @@ -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) }