Skip to content

Commit

Permalink
Version1.1.12
Browse files Browse the repository at this point in the history
 BugFixes
  1. 修复国际化Bug;补充了MessageId类型;
  • Loading branch information
KrisYu committed Jul 26, 2024
1 parent 142eff1 commit d17a427
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sdk/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (e *Api) OK(ctx *gin.Context, data any) {

func (e *Api) Error(ctx *gin.Context, businessStatus response.Status, errMsg ...i18n.MyError) {
statusMsg := translate(ctx, businessStatus.Message)
var msg string
msg := statusMsg
if len(errMsg) > 0 {
errorMsg := translate(ctx, errMsg[0].Message)
msg = strings.Join([]string{statusMsg, errorMsg}, ": ")
Expand Down
6 changes: 1 addition & 5 deletions sdk/api/response/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ type Status struct {
}

var (
OK = Status{Code: 2000, Message: i18n.Message{ID: "response.ok", DefaultMessage: "OK"}}
RequestParamsError = Status{Code: 4000, Message: i18n.Message{ID: "response.badRequest", DefaultMessage: "Request Parameters Error"}}
Unauthorized = Status{Code: 4001, Message: i18n.Message{ID: "response.unauthorized", DefaultMessage: "Unauthorized Request"}}
InternalServerError = Status{Code: 5000, Message: i18n.Message{ID: "response.internalServerError", DefaultMessage: "Internal Server Error"}}
ServiceError = Status{Code: 5001, Message: i18n.Message{ID: "response.serviceError", DefaultMessage: "Service Error"}}
OK = Status{Code: 2000, Message: i18n.Message{ID: "response.ok", DefaultMessage: "OK"}}
)
14 changes: 14 additions & 0 deletions sdk/i18n/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ func NewMyError(err error) MyError {
Message: Message{DefaultMessage: err.Error()},
}
}

func NewMyErrorWithMessageID(mi MessageID, arg ...any) MyError {
myError := MyError{
Err: nil,
Message: Message{
ID: mi.ID,
DefaultMessage: mi.DefaultMessage,
},
}
if len(arg) > 0 {
myError.Message.Args = arg[0]
}
return myError
}
5 changes: 5 additions & 0 deletions sdk/i18n/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ type Message struct {
DefaultMessage string `json:"defaultMessage"` // 默认消息,翻译失败时,使用
Args any `json:"args"` // 消息参数
}

type MessageID struct {
ID string `json:"ID"`
DefaultMessage string `json:"defaultMessage"`
}

0 comments on commit d17a427

Please sign in to comment.