Merge branch 'master' into dev
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
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 := ctx.Value(ctxLangKey)
|
||||
|
||||
l := ""
|
||||
|
||||
if lang != nil {
|
||||
l = lang.(string)
|
||||
}
|
||||
|
||||
// HTTP头部的
|
||||
if l == "" {
|
||||
lang = ctx.Value("Accept-Language")
|
||||
if lang != nil {
|
||||
l = lang.(string)
|
||||
}
|
||||
}
|
||||
|
||||
// 默认的
|
||||
if l == "" {
|
||||
l = GetDefaultLang()
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package langx
|
||||
|
||||
import "context"
|
||||
|
||||
// 定义错误常量
|
||||
|
||||
type ErrorLanguage struct {
|
||||
LangError
|
||||
}
|
||||
|
||||
// 生成错误常量
|
||||
func NewLanguage(uniKey string, code int, defaultValue string) ErrorLanguage {
|
||||
AppendCode(map[string]int{uniKey: code})
|
||||
AppendTrans(GetDefaultLang(), map[string]string{uniKey: defaultValue})
|
||||
|
||||
l := NewStruct(context.Background(), uniKey, nil)
|
||||
|
||||
return ErrorLanguage{l}
|
||||
}
|
||||
|
||||
// Key生成错误信息
|
||||
func (l ErrorLanguage) Err() error {
|
||||
return l
|
||||
}
|
||||
|
||||
// Key生成错误信息
|
||||
func (l ErrorLanguage) Errf(format map[string]string) error {
|
||||
newLang := l.Copy()
|
||||
newLang.SetFormat(format)
|
||||
return newLang
|
||||
}
|
||||
|
||||
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 {
|
||||
newLang := l.Copy()
|
||||
newLang.SetCtx(ctx)
|
||||
return newLang.Error()
|
||||
}
|
||||
|
||||
// 获取翻译后的错误信息
|
||||
func (l ErrorLanguage) Msgf(ctx context.Context, format map[string]string) string {
|
||||
newLang := l.Copy()
|
||||
newLang.SetCtx(ctx)
|
||||
newLang.SetFormat(format)
|
||||
return newLang.Error()
|
||||
}
|
||||
|
||||
var (
|
||||
Success ErrorLanguage = NewLanguage("success", 200, "操作成功")
|
||||
Error ErrorLanguage = NewLanguage("error", 400, "操作失败")
|
||||
ErrWithMsg ErrorLanguage = NewLanguage("error_with_msg", 400, "操作失败: #msg#")
|
||||
)
|
||||
@@ -4,14 +4,18 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// Key生成错误信息
|
||||
|
||||
type LangError interface {
|
||||
Copy() LangError // 复制错误信息
|
||||
Copy() LangError // 复制一个新的错误信息
|
||||
Error() string // 实现error接口&获取翻译后的错误信息
|
||||
GetCode() int // 获取翻译后的Code
|
||||
GetKey() string // 获取原Key值
|
||||
GetFormat() map[string]string // 获取附加数据
|
||||
SetFormat(format map[string]string) // 设置附加数据
|
||||
SetCtx(ctxhttp context.Context) // 设置上下文
|
||||
SetCtx(ctxHttp context.Context) // 设置上下文
|
||||
SetLang(lang string) // 设置语言
|
||||
SetFormatKV(key, value string) // 设置附加数据键值对
|
||||
}
|
||||
|
||||
type langError struct {
|
||||
@@ -28,13 +32,24 @@ func (l *langError) Copy() LangError {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *langError) Error() string {
|
||||
errLang := e.ctx.Value("Accept-Language")
|
||||
l := ""
|
||||
if errLang != nil {
|
||||
l = string(errLang.(string))
|
||||
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)
|
||||
}
|
||||
return GetFormat(l, e.key, e.format)
|
||||
l.format[key] = value
|
||||
}
|
||||
|
||||
func (e *langError) SetCtx(ctxHttp context.Context) {
|
||||
e.ctx = ctxHttp
|
||||
}
|
||||
|
||||
func (l *langError) Error() string {
|
||||
errLang := GetCtxLang(l.ctx)
|
||||
return GetFormat(errLang, l.key, l.format)
|
||||
}
|
||||
|
||||
func (e *langError) GetCode() int {
|
||||
@@ -52,15 +67,11 @@ func (e *langError) GetFormat() map[string]string {
|
||||
return e.format
|
||||
}
|
||||
|
||||
func (e *langError) SetFormat(format map[string]string) {
|
||||
e.format = format
|
||||
func (e *langError) SetLang(lang string) {
|
||||
e.ctx = SetCtxLang(e.ctx, lang)
|
||||
}
|
||||
|
||||
func (e *langError) SetCtx(ctx context.Context) {
|
||||
e.ctx = ctx
|
||||
}
|
||||
|
||||
func NewErrorFormat(ctx context.Context, key string, format map[string]string) error {
|
||||
func NewErrorf(ctx context.Context, key string, format map[string]string) error {
|
||||
return &langError{
|
||||
ctx: ctx,
|
||||
key: key,
|
||||
@@ -75,6 +86,10 @@ func NewError(ctx context.Context, key string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, "Accept-Language", lang)
|
||||
func NewStruct(ctx context.Context, key string, format map[string]string) LangError {
|
||||
return &langError{
|
||||
ctx: ctx,
|
||||
key: key,
|
||||
format: format,
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -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())
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -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 (
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# 导入资源
|
||||
|
||||
# 通过embed导入
|
||||
|
||||
# 通过文件导入
|
||||
|
||||
# 追加导入
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+7
-7
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user