更新一些设计

This commit is contained in:
yun
2025-10-26 15:41:30 +08:00
parent 046091cb35
commit 38a46b85e1
3 changed files with 94 additions and 26 deletions
+22 -4
View File
@@ -9,9 +9,27 @@ func SetCtxLang(ctx context.Context, lang string) context.Context {
} }
func GetCtxLang(ctx context.Context) string { func GetCtxLang(ctx context.Context) string {
lang, ok := ctx.Value(ctxLangKey).(string) // 指定的
if !ok { lang := ctx.Value(ctxLangKey)
return ""
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
} }
+22 -14
View File
@@ -4,43 +4,51 @@ import "context"
// 定义错误常量 // 定义错误常量
type ErrorLanguage string type ErrorLanguage struct {
LangError
}
// 生成错误常量 // 生成错误常量
func NewLanguage(uniKey string, code int, defaultValue string) ErrorLanguage { func NewLanguage(uniKey string, code int, defaultValue string) ErrorLanguage {
AppendCode(map[string]int{uniKey: code}) AppendCode(map[string]int{uniKey: code})
AppendTrans("zh_hans", map[string]string{uniKey: defaultValue}) AppendTrans(GetDefaultLang(), map[string]string{uniKey: defaultValue})
return ErrorLanguage(uniKey)
}
// 获取原key l := NewErrorStruct(context.Background(), uniKey, nil)
func (l ErrorLanguage) String() string {
return string(l) return ErrorLanguage{l}
} }
// Key生成错误信息 // Key生成错误信息
func (l ErrorLanguage) Err() error { func (l ErrorLanguage) Err() error {
return NewError(context.Background(), l.String()) return l
} }
// Key生成错误信息 // Key生成错误信息
func (l ErrorLanguage) Errf(format map[string]string) error { 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) ErrfKV(key, value string) error {
func (l ErrorLanguage) Code() int { newLang := l.Copy()
return GetCode(l.String()) newLang.SetFormatKV(key, value)
return newLang
} }
// 获取翻译后的错误信息 // 获取翻译后的错误信息
func (l ErrorLanguage) Msg(ctx context.Context) string { 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 { 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 ( var (
+45 -3
View File
@@ -7,23 +7,57 @@ import (
// Key生成错误信息 // Key生成错误信息
type LangError interface { type LangError interface {
Copy() LangError // 复制一个新的错误信息
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 // 获取附加数据
SetFormat(format map[string]string) // 设置附加数据
SetCtx(ctxHttp context.Context) // 设置上下文
SetLang(lang string) // 设置语言 SetLang(lang string) // 设置语言
SetFormatKV(key, value string) // 设置附加数据键值对
} }
type langError struct { type langError struct {
ctx context.Context ctx context.Context
key string key string
format map[string]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 { 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 { func (e *langError) GetCode() int {
@@ -59,3 +93,11 @@ func NewError(ctx context.Context, key string) error {
key: key, key: key,
} }
} }
func NewErrorStruct(ctx context.Context, key string, format map[string]string) LangError {
return &langError{
ctx: ctx,
key: key,
format: format,
}
}