2022-09-27 17:59:05 +08:00
|
|
|
package schema
|
|
|
|
|
2023-05-16 11:48:35 +08:00
|
|
|
import (
|
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
|
|
|
"github.com/segmentfault/pacman/i18n"
|
|
|
|
)
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
type ReasonItem struct {
|
|
|
|
ReasonType int `json:"reason_type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ContentType string `json:"content_type"`
|
2022-09-30 11:49:05 +08:00
|
|
|
Placeholder string `json:"placeholder"`
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReasonReq struct {
|
|
|
|
// ObjectType
|
|
|
|
ObjectType string `validate:"required" form:"object_type" json:"object_type"`
|
|
|
|
// Action
|
|
|
|
Action string `validate:"required" form:"action" json:"action"`
|
|
|
|
}
|
2023-05-16 11:48:35 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-05-16 15:22:46 +08:00
|
|
|
key := keyPrefix + "." + fieldName
|
2023-05-16 11:48:35 +08:00
|
|
|
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
|
2023-05-16 15:22:46 +08:00
|
|
|
return fieldData + "没翻译"
|
2023-05-16 11:48:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Name = trField("name", r.Name)
|
|
|
|
r.Description = trField("desc", r.Description)
|
|
|
|
r.Placeholder = trField("placeholder", r.Placeholder)
|
|
|
|
}
|