From 7eaf21634c1389101fce12e36ea7ca79c841d9bf Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 10 Nov 2024 22:28:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 17 +++++++++++++++ error.go | 32 +++++++++++----------------- error_test.go | 8 ++++++- example/enample2.go | 2 +- example/{ => imports}/lang/code.json | 0 example/{ => imports}/lang/en.json | 0 example/{ => imports}/lang/zh.json | 0 example/{ => imports}/main.go | 0 example/imports/readme.md | 7 ++++++ langx.go | 7 +++--- options.go | 14 ++++++------ 11 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 common.go rename example/{ => imports}/lang/code.json (100%) rename example/{ => imports}/lang/en.json (100%) rename example/{ => imports}/lang/zh.json (100%) rename example/{ => imports}/main.go (100%) create mode 100644 example/imports/readme.md diff --git a/common.go b/common.go new file mode 100644 index 0000000..510a450 --- /dev/null +++ b/common.go @@ -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 +} diff --git a/error.go b/error.go index 7abbb4e..4fb5878 100644 --- a/error.go +++ b/error.go @@ -5,10 +5,11 @@ import ( ) type LangError interface { - Error() string // 实现error接口&获取翻译后的错误信息 - GetCode() int // 获取翻译后的Code - GetKey() string // 获取原Key值 - GetFormat() map[string]string // 获取附加数据 + Error() string // 实现error接口&获取翻译后的错误信息 + GetCode() int // 获取翻译后的Code + GetKey() string // 获取原Key值 + GetFormat() map[string]string // 获取附加数据 + SetLang(lang string) // 设置语言 } type langError struct { @@ -17,15 +18,8 @@ type langError struct { format map[string]string } -type errorConst string - -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 (l *langError) Error() string { + return GetFormat(GetCtxLang(l.ctx), l.key, l.format) } func (e *langError) GetCode() int { @@ -43,7 +37,11 @@ func (e *langError) GetFormat() map[string]string { 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{ ctx: ctx, key: key, @@ -57,9 +55,3 @@ func NewError(ctx context.Context, key string) error { key: key, } } - -const errorLang errorConst = "errorLang" - -func SetCtxLang(ctx context.Context, lang string) context.Context { - return context.WithValue(ctx, errorLang, lang) -} diff --git a/error_test.go b/error_test.go index 60608e1..857d03f 100644 --- a/error_test.go +++ b/error_test.go @@ -40,13 +40,19 @@ func TestError(t *testing.T) { t.Log(val.GetCode()) } - err = langx.NewErrorFormat(ctx, "username", map[string]string{ + err = langx.NewErrorf(ctx, "username", map[string]string{ "name": "yuninks", }) t.Log(err.Error()) val, ok = err.(langx.LangError) if ok { t.Log(val.GetCode()) + + // 设置输出语言 + val.SetLang("en") + } + t.Log(val.Error()) + } diff --git a/example/enample2.go b/example/enample2.go index ad4f45e..4c49a18 100644 --- a/example/enample2.go +++ b/example/enample2.go @@ -33,7 +33,7 @@ func (l Language) Error() 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 ( diff --git a/example/lang/code.json b/example/imports/lang/code.json similarity index 100% rename from example/lang/code.json rename to example/imports/lang/code.json diff --git a/example/lang/en.json b/example/imports/lang/en.json similarity index 100% rename from example/lang/en.json rename to example/imports/lang/en.json diff --git a/example/lang/zh.json b/example/imports/lang/zh.json similarity index 100% rename from example/lang/zh.json rename to example/imports/lang/zh.json diff --git a/example/main.go b/example/imports/main.go similarity index 100% rename from example/main.go rename to example/imports/main.go diff --git a/example/imports/readme.md b/example/imports/readme.md new file mode 100644 index 0000000..d1093cf --- /dev/null +++ b/example/imports/readme.md @@ -0,0 +1,7 @@ +# 导入资源 + +# 通过embed导入 + +# 通过文件导入 + +# 追加导入 diff --git a/langx.go b/langx.go index 935216a..d115a3e 100644 --- a/langx.go +++ b/langx.go @@ -273,10 +273,9 @@ func GetDefaultLang() string { } func getLangFromCtx(ctx context.Context) string { - ctxVal := ctx.Value(l.ops.ctxLangKey) - lang := l.ops.defaultLang - if ctxVal != nil { - lang = ctxVal.(string) + lang := GetCtxLang(ctx) + if lang == "" { + lang = l.ops.defaultLang } return lang } diff --git a/options.go b/options.go index 1d69fae..ec33529 100644 --- a/options.go +++ b/options.go @@ -4,7 +4,7 @@ type options struct { defaultCode int defaultLang string replaceKey string - ctxLangKey string + // ctxLangKey string } func defaultOptions() *options { @@ -12,7 +12,7 @@ func defaultOptions() *options { defaultCode: 200, defaultLang: "zh", replaceKey: "#%s#", - ctxLangKey: "language", + // ctxLangKey: "language", } } @@ -33,11 +33,11 @@ func SetDefaultCode(code int) Option { } // 从ctx里面获取语言的key -func SetCtxLangKey(key string) Option { - return func(o *options) { - o.ctxLangKey = key - } -} +// func SetCtxLangKey(key string) Option { +// return func(o *options) { +// o.ctxLangKey = key +// } +// } // 默认语言 func SetDefaultLanguage(lang string) Option { From 046091cb350b33551830c96124ce188797ccb00a Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 10 Nov 2024 22:55:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consts.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ error.go | 4 ++++ 2 files changed, 54 insertions(+) create mode 100644 consts.go diff --git a/consts.go b/consts.go new file mode 100644 index 0000000..8274dad --- /dev/null +++ b/consts.go @@ -0,0 +1,50 @@ +package langx + +import "context" + +// 定义错误常量 + +type ErrorLanguage string + +// 生成错误常量 +func NewLanguage(uniKey string, code int, defaultValue string) ErrorLanguage { + AppendCode(map[string]int{uniKey: code}) + AppendTrans("zh_hans", map[string]string{uniKey: defaultValue}) + return ErrorLanguage(uniKey) +} + +// 获取原key +func (l ErrorLanguage) String() string { + return string(l) +} + +// Key生成错误信息 +func (l ErrorLanguage) Err() error { + return NewError(context.Background(), l.String()) +} + +// Key生成错误信息 +func (l ErrorLanguage) Errf(format map[string]string) error { + return NewErrorf(context.Background(), l.String(), format) +} + +// 获取翻译后的Code +func (l ErrorLanguage) Code() int { + return GetCode(l.String()) +} + +// 获取翻译后的错误信息 +func (l ErrorLanguage) Msg(ctx context.Context) string { + return GetFormatCtx(ctx, l.String(), nil) +} + +// 获取翻译后的错误信息 +func (l ErrorLanguage) Msgf(ctx context.Context, format map[string]string) string { + return GetFormatCtx(ctx, l.String(), format) +} + +var ( + Success ErrorLanguage = NewLanguage("success", 200, "操作成功") + Error ErrorLanguage = NewLanguage("error", 400, "操作失败") + ErrWithMsg ErrorLanguage = NewLanguage("error_with_msg", 400, "操作失败: #msg#") +) diff --git a/error.go b/error.go index 4fb5878..f89aa73 100644 --- a/error.go +++ b/error.go @@ -4,6 +4,8 @@ import ( "context" ) +// Key生成错误信息 + type LangError interface { Error() string // 实现error接口&获取翻译后的错误信息 GetCode() int // 获取翻译后的Code @@ -12,6 +14,8 @@ type LangError interface { SetLang(lang string) // 设置语言 } + + type langError struct { ctx context.Context key string From 38a46b85e11f26baa9e5ab1ccac429dfcb5a0a91 Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 26 Oct 2025 15:41:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 26 +++++++++++++++++++++---- consts.go | 36 ++++++++++++++++++++-------------- error.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 94 insertions(+), 26 deletions(-) diff --git a/common.go b/common.go index 510a450..9b0f83b 100644 --- a/common.go +++ b/common.go @@ -9,9 +9,27 @@ func SetCtxLang(ctx context.Context, lang string) context.Context { } func GetCtxLang(ctx context.Context) string { - lang, ok := ctx.Value(ctxLangKey).(string) - if !ok { - return "" + // 指定的 + lang := ctx.Value(ctxLangKey) + + l := "" + + if lang != nil { + l = lang.(string) } - return lang + + // HTTP头部的 + if l == "" { + lang = ctx.Value("Accept-Language") + if lang != nil { + l = lang.(string) + } + } + + // 默认的 + if l == "" { + l = GetDefaultLang() + } + + return l } diff --git a/consts.go b/consts.go index 8274dad..ae54e60 100644 --- a/consts.go +++ b/consts.go @@ -4,43 +4,51 @@ import "context" // 定义错误常量 -type ErrorLanguage string +type ErrorLanguage struct { + LangError +} // 生成错误常量 func NewLanguage(uniKey string, code int, defaultValue string) ErrorLanguage { AppendCode(map[string]int{uniKey: code}) - AppendTrans("zh_hans", map[string]string{uniKey: defaultValue}) - return ErrorLanguage(uniKey) -} + AppendTrans(GetDefaultLang(), map[string]string{uniKey: defaultValue}) -// 获取原key -func (l ErrorLanguage) String() string { - return string(l) + l := NewErrorStruct(context.Background(), uniKey, nil) + + return ErrorLanguage{l} } // Key生成错误信息 func (l ErrorLanguage) Err() error { - return NewError(context.Background(), l.String()) + return l } // Key生成错误信息 func (l ErrorLanguage) Errf(format map[string]string) error { - return NewErrorf(context.Background(), l.String(), format) + newLang := l.Copy() + newLang.SetFormat(format) + return newLang } -// 获取翻译后的Code -func (l ErrorLanguage) Code() int { - return GetCode(l.String()) +func (l ErrorLanguage) ErrfKV(key, value string) error { + newLang := l.Copy() + newLang.SetFormatKV(key, value) + return newLang } // 获取翻译后的错误信息 func (l ErrorLanguage) Msg(ctx context.Context) string { - return GetFormatCtx(ctx, l.String(), nil) + newLang := l.Copy() + newLang.SetCtx(ctx) + return newLang.Error() } // 获取翻译后的错误信息 func (l ErrorLanguage) Msgf(ctx context.Context, format map[string]string) string { - return GetFormatCtx(ctx, l.String(), format) + newLang := l.Copy() + newLang.SetCtx(ctx) + newLang.SetFormat(format) + return newLang.Error() } var ( diff --git a/error.go b/error.go index f89aa73..aa32e5c 100644 --- a/error.go +++ b/error.go @@ -7,23 +7,57 @@ import ( // Key生成错误信息 type LangError interface { - Error() string // 实现error接口&获取翻译后的错误信息 - GetCode() int // 获取翻译后的Code - GetKey() string // 获取原Key值 - GetFormat() map[string]string // 获取附加数据 - SetLang(lang string) // 设置语言 + Copy() LangError // 复制一个新的错误信息 + Error() string // 实现error接口&获取翻译后的错误信息 + GetCode() int // 获取翻译后的Code + GetKey() string // 获取原Key值 + GetFormat() map[string]string // 获取附加数据 + SetFormat(format map[string]string) // 设置附加数据 + SetCtx(ctxHttp context.Context) // 设置上下文 + SetLang(lang string) // 设置语言 + SetFormatKV(key, value string) // 设置附加数据键值对 } - - type langError struct { ctx context.Context key string format map[string]string } +func (l *langError) Copy() LangError { + return &langError{ + ctx: l.ctx, + key: l.key, + format: l.format, + } +} + +func (e *langError) SetFormat(format map[string]string) { + e.format = format +} + +func (l *langError) SetFormatKV(key, value string) { + if l.format == nil { + l.format = make(map[string]string) + } + l.format[key] = value +} + +func (e *langError) SetCtx(ctxHttp context.Context) { + e.ctx = ctxHttp +} + func (l *langError) Error() string { - return GetFormat(GetCtxLang(l.ctx), l.key, l.format) + errLang := l.ctx.Value("Accept-Language") + lang := "" + if errLang != nil { + lang = errLang.(string) + } + if lang == "" { + lang = GetDefaultLang() + } + + return GetFormat(lang, l.key, l.format) } func (e *langError) GetCode() int { @@ -59,3 +93,11 @@ func NewError(ctx context.Context, key string) error { key: key, } } + +func NewErrorStruct(ctx context.Context, key string, format map[string]string) LangError { + return &langError{ + ctx: ctx, + key: key, + format: format, + } +}