优化逻辑
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
package langx
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
const ctxLangKey string = "ctxLang"
|
||||||
|
|
||||||
|
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||||
|
return context.WithValue(ctx, ctxLangKey, lang)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCtxLang(ctx context.Context) string {
|
||||||
|
lang, ok := ctx.Value(ctxLangKey).(string)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return lang
|
||||||
|
}
|
||||||
@@ -5,10 +5,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LangError interface {
|
type LangError interface {
|
||||||
Error() string // 实现error接口&获取翻译后的错误信息
|
Error() string // 实现error接口&获取翻译后的错误信息
|
||||||
GetCode() int // 获取翻译后的Code
|
GetCode() int // 获取翻译后的Code
|
||||||
GetKey() string // 获取原Key值
|
GetKey() string // 获取原Key值
|
||||||
GetFormat() map[string]string // 获取附加数据
|
GetFormat() map[string]string // 获取附加数据
|
||||||
|
SetLang(lang string) // 设置语言
|
||||||
}
|
}
|
||||||
|
|
||||||
type langError struct {
|
type langError struct {
|
||||||
@@ -17,15 +18,8 @@ type langError struct {
|
|||||||
format map[string]string
|
format map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type errorConst string
|
func (l *langError) Error() string {
|
||||||
|
return GetFormat(GetCtxLang(l.ctx), l.key, l.format)
|
||||||
func (e *langError) Error() string {
|
|
||||||
errLang := e.ctx.Value(errorLang)
|
|
||||||
l := ""
|
|
||||||
if errLang != nil {
|
|
||||||
l = string(errLang.(errorConst))
|
|
||||||
}
|
|
||||||
return GetFormat(l, e.key, e.format)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *langError) GetCode() int {
|
func (e *langError) GetCode() int {
|
||||||
@@ -43,7 +37,11 @@ func (e *langError) GetFormat() map[string]string {
|
|||||||
return e.format
|
return e.format
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewErrorFormat(ctx context.Context, key string, format map[string]string) error {
|
func (e *langError) SetLang(lang string) {
|
||||||
|
e.ctx = SetCtxLang(e.ctx, lang)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewErrorf(ctx context.Context, key string, format map[string]string) error {
|
||||||
return &langError{
|
return &langError{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
key: key,
|
key: key,
|
||||||
@@ -57,9 +55,3 @@ func NewError(ctx context.Context, key string) error {
|
|||||||
key: key,
|
key: key,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const errorLang errorConst = "errorLang"
|
|
||||||
|
|
||||||
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
|
||||||
return context.WithValue(ctx, errorLang, lang)
|
|
||||||
}
|
|
||||||
|
|||||||
+7
-1
@@ -40,13 +40,19 @@ func TestError(t *testing.T) {
|
|||||||
t.Log(val.GetCode())
|
t.Log(val.GetCode())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = langx.NewErrorFormat(ctx, "username", map[string]string{
|
err = langx.NewErrorf(ctx, "username", map[string]string{
|
||||||
"name": "yuninks",
|
"name": "yuninks",
|
||||||
})
|
})
|
||||||
t.Log(err.Error())
|
t.Log(err.Error())
|
||||||
val, ok = err.(langx.LangError)
|
val, ok = err.(langx.LangError)
|
||||||
if ok {
|
if ok {
|
||||||
t.Log(val.GetCode())
|
t.Log(val.GetCode())
|
||||||
|
|
||||||
|
// 设置输出语言
|
||||||
|
val.SetLang("en")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Log(val.Error())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ func (l Language) Error() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l Language) Errorf(format map[string]string) error {
|
func (l Language) Errorf(format map[string]string) error {
|
||||||
return langx.NewErrorFormat(context.Background(), l.String(), format)
|
return langx.NewErrorf(context.Background(), l.String(), format)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# 导入资源
|
||||||
|
|
||||||
|
# 通过embed导入
|
||||||
|
|
||||||
|
# 通过文件导入
|
||||||
|
|
||||||
|
# 追加导入
|
||||||
@@ -273,10 +273,9 @@ func GetDefaultLang() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getLangFromCtx(ctx context.Context) string {
|
func getLangFromCtx(ctx context.Context) string {
|
||||||
ctxVal := ctx.Value(l.ops.ctxLangKey)
|
lang := GetCtxLang(ctx)
|
||||||
lang := l.ops.defaultLang
|
if lang == "" {
|
||||||
if ctxVal != nil {
|
lang = l.ops.defaultLang
|
||||||
lang = ctxVal.(string)
|
|
||||||
}
|
}
|
||||||
return lang
|
return lang
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -4,7 +4,7 @@ type options struct {
|
|||||||
defaultCode int
|
defaultCode int
|
||||||
defaultLang string
|
defaultLang string
|
||||||
replaceKey string
|
replaceKey string
|
||||||
ctxLangKey string
|
// ctxLangKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() *options {
|
func defaultOptions() *options {
|
||||||
@@ -12,7 +12,7 @@ func defaultOptions() *options {
|
|||||||
defaultCode: 200,
|
defaultCode: 200,
|
||||||
defaultLang: "zh",
|
defaultLang: "zh",
|
||||||
replaceKey: "#%s#",
|
replaceKey: "#%s#",
|
||||||
ctxLangKey: "language",
|
// ctxLangKey: "language",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +33,11 @@ func SetDefaultCode(code int) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 从ctx里面获取语言的key
|
// 从ctx里面获取语言的key
|
||||||
func SetCtxLangKey(key string) Option {
|
// func SetCtxLangKey(key string) Option {
|
||||||
return func(o *options) {
|
// return func(o *options) {
|
||||||
o.ctxLangKey = key
|
// o.ctxLangKey = key
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 默认语言
|
// 默认语言
|
||||||
func SetDefaultLanguage(lang string) Option {
|
func SetDefaultLanguage(lang string) Option {
|
||||||
|
|||||||
Reference in New Issue
Block a user