-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NewFeature 1. 添加了国际化支持;修改api组件,适配国际化消息传参;添加了国际化引入的例子;
- Loading branch information
KrisYu
committed
Jul 26, 2024
1 parent
dc78b31
commit 142eff1
Showing
16 changed files
with
381 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package header | ||
|
||
const ( | ||
AcceptLanguageFlag = "Accept-Language" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package response | ||
|
||
import "github.com/MrKrisYu/koi-go-common/sdk/i18n" | ||
|
||
type Status struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Code int `json:"code"` | ||
Message i18n.Message `json:"message"` | ||
} | ||
|
||
var ( | ||
OK = Status{Code: 2000, Message: "OK"} | ||
RequestParamsError = Status{Code: 4000, Message: "Request Parameters Error"} | ||
Unauthorized = Status{Code: 4001, Message: "Unauthorized Request"} | ||
NotLoginUserOperation = Status{Code: 4002, Message: "Error for overstepping your authority"} | ||
InternalServerError = Status{Code: 5000, Message: "Internal Server Error"} | ||
ServiceError = Status{Code: 5001, Message: "Service Error"} | ||
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"}} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package i18n | ||
|
||
type MyError struct { | ||
Err error | ||
Message Message `json:"message"` | ||
} | ||
|
||
func (e MyError) Error() string { | ||
return e.Err.Error() | ||
} | ||
|
||
func NewMyError(err error) MyError { | ||
return MyError{ | ||
Err: err, | ||
Message: Message{DefaultMessage: err.Error()}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package example | ||
|
||
import ( | ||
"fmt" | ||
"github.com/MrKrisYu/koi-go-common/sdk/api/header" | ||
"github.com/gin-gonic/gin" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
const ( | ||
LanguageReqQueryKey = "lang" | ||
) | ||
|
||
func GetLang(ctx *gin.Context) language.Tag { | ||
matchedTag := DefaultLanguage | ||
acceptLanguage := ctx.GetHeader(header.AcceptLanguageFlag) | ||
if len(acceptLanguage) == 0 { | ||
acceptLanguage, _ = ctx.GetQuery(LanguageReqQueryKey) | ||
} | ||
if len(acceptLanguage) == 0 { | ||
fmt.Printf("[Middleware-AcceptLanguage] got lang=%s, match=%s \n\n", acceptLanguage, matchedTag.String()) | ||
return matchedTag | ||
} | ||
matcher := language.NewMatcher(AllowedLanguage) | ||
_, index, confidence := matcher.Match(language.Make(acceptLanguage)) | ||
if confidence != language.No { | ||
matchedTag = AllowedLanguage[index] | ||
} | ||
fmt.Printf("[Middleware-AcceptLanguage] got lang=%s, match=%s \n\n", acceptLanguage, matchedTag.String()) | ||
return matchedTag | ||
} | ||
|
||
func AcceptLanguage() gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
lang := GetLang(ctx) | ||
ctx.Set(header.AcceptLanguageFlag, lang) | ||
ctx.Next() | ||
} | ||
} |
Oops, something went wrong.