Compare commits

...

10 Commits

Author SHA1 Message Date
yun 046091cb35 更新 2024-11-10 22:55:35 +08:00
yun 7eaf21634c 优化逻辑 2024-11-10 22:28:02 +08:00
yun f66e76883f 允许获取当前默认的语言 2024-08-11 23:35:18 +08:00
yun 3a4c3e58e6 更新用法 2024-08-11 22:19:28 +08:00
yun 8d3bd7cade 修改错误接口的方法 2024-07-31 16:47:23 +08:00
yun ea8d17943d 优化Error的封装 2024-07-24 19:47:00 +08:00
yun d9fdeabf85 修复在append的时候会有panic问题 2024-07-24 19:36:32 +08:00
yun d710d6b30c 添加锁 避免panic 2024-07-21 11:10:41 +08:00
yun 5205a6cad1 更新 2024-07-11 21:12:01 +08:00
yun 2bed4e7fca 支持embed的文件 2024-07-11 13:47:21 +08:00
14 changed files with 344 additions and 64 deletions
+17
View File
@@ -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
}
+50
View File
@@ -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#")
)
+25 -22
View File
@@ -4,42 +4,49 @@ import (
"context" "context"
) )
type LangError struct { // Key生成错误信息
type LangError interface {
Error() string // 实现error接口&获取翻译后的错误信息
GetCode() int // 获取翻译后的Code
GetKey() string // 获取原Key值
GetFormat() map[string]string // 获取附加数据
SetLang(lang string) // 设置语言
}
type langError struct {
ctx context.Context ctx context.Context
key string key string
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)
const errorLang errorConst = "errorLang"
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 {
return GetCode(e.key) return GetCode(e.key)
} }
func (e *LangError) GetMsg() string { func (e *langError) GetKey() string {
return e.key return e.key
} }
func (e *LangError) GetFormat() map[string]string { func (e *langError) GetFormat() map[string]string {
if e.format == nil { if e.format == nil {
e.format = make(map[string]string) e.format = make(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) {
return &LangError{ e.ctx = SetCtxLang(e.ctx, lang)
}
func NewErrorf(ctx context.Context, key string, format map[string]string) error {
return &langError{
ctx: ctx, ctx: ctx,
key: key, key: key,
format: format, format: format,
@@ -47,12 +54,8 @@ func NewErrorFormat(ctx context.Context, key string, format map[string]string) e
} }
func NewError(ctx context.Context, key string) error { func NewError(ctx context.Context, key string) error {
return &LangError{ return &langError{
ctx: ctx, ctx: ctx,
key: key, key: key,
} }
} }
func SetCtxLang(ctx context.Context, lang string) context.Context {
return context.WithValue(ctx, errorLang, lang)
}
+9 -3
View File
@@ -35,18 +35,24 @@ func TestError(t *testing.T) {
err = langx.NewError(ctx, "error") err = langx.NewError(ctx, "error")
// fmt.Printf("err: %v\n", err) // fmt.Printf("err: %v\n", err)
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())
} }
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())
} }
+44
View File
@@ -0,0 +1,44 @@
package main
import (
"context"
"github.com/yuninks/langx"
)
func main() {
err := ErrorWithMsg.Error()
// 输出:错误
println(err.Error())
}
type Language string
// 添加key+默认语言
func newLanguage(uniKey string, code int, defaultValue string) Language {
langx.AppendCode(map[string]int{uniKey: code})
langx.AppendTrans("zh_hans", map[string]string{uniKey: defaultValue})
return Language(uniKey)
}
func (l Language) String() string {
return string(l)
}
func (l Language) Error() error {
return langx.NewError(context.Background(), l.String())
}
func (l Language) Errorf(format map[string]string) error {
return langx.NewErrorf(context.Background(), l.String(), format)
}
var (
Success Language = newLanguage("success", 200, "成功")
Error Language = newLanguage("error", 400, "错误")
ErrorWithMsg Language = newLanguage("error_with_msg", 400, "错误 #msg#")
)
+54
View File
@@ -0,0 +1,54 @@
package main
import (
"embed"
"fmt"
"github.com/yuninks/langx"
)
//go:embed lang
var assetsFs embed.FS
func main() {
regByAppend()
}
// 导入语言包 基于Append
func regByAppend() {
langx.AppendCode(map[string]int{
"success": 200,
})
langx.AppendTrans("zh-CN", map[string]string{
"success": "成功",
})
code, msg := langx.GetTransFormat("zh-CN", "success", map[string]string{})
fmt.Println(code, msg)
}
// 导入语言包 基于Embed
func regByEmbed() {
err := langx.RegisterEmbed(assetsFs)
fmt.Println(err)
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
fmt.Println(code, msg)
code, msg = langx.GetTransFormat("en", "error", map[string]string{
"msg": "这是失败的原因",
})
fmt.Println(code, msg)
}
// 导入语言包 基于文件
func regByDir() {
langx.RegisterDir("./lang")
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
fmt.Println(code, msg)
code, msg = langx.GetTransFormat("en", "error", map[string]string{
"msg": "这是失败的原因",
})
fmt.Println(code, msg)
}
+7
View File
@@ -0,0 +1,7 @@
# 导入资源
# 通过embed导入
# 通过文件导入
# 追加导入
-18
View File
@@ -1,18 +0,0 @@
package main
import (
"fmt"
"github.com/yuninks/langx"
)
func main() {
langx.RegisterDir("./lang")
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
fmt.Println(code, msg)
code, msg = langx.GetTransFormat("en", "error", map[string]string{
"msg": "这是失败的原因",
})
fmt.Println(code, msg)
}
+119 -13
View File
@@ -2,18 +2,21 @@ package langx
import ( import (
"context" "context"
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
) )
type langx struct { type langx struct {
ops *options ops *options
codeMap map[string]int codeMap map[string]int
transMap map[string]map[string]string transMap map[string]map[string]string
mut sync.Mutex
} }
var l *langx = &langx{} var l *langx = &langx{}
@@ -24,26 +27,45 @@ func init() {
ops: defaultOptions(), ops: defaultOptions(),
codeMap: make(map[string]int), codeMap: make(map[string]int),
transMap: make(map[string]map[string]string), transMap: make(map[string]map[string]string),
} mut: sync.Mutex{},
}
// 设置
func InitLangx(ops ...Option) {
for _, opt := range ops {
opt(l.ops)
} }
} }
// 这是语言的Code // 这是语言的Code
func RegisterCode(datas map[string]int) { func RegisterCode(datas map[string]int) {
l.mut.Lock()
defer l.mut.Unlock()
l.codeMap = datas l.codeMap = datas
} }
// 追加覆盖Code
func AppendCode(datas map[string]int) {
l.mut.Lock()
defer l.mut.Unlock()
for k, v := range datas {
l.codeMap[k] = v
}
}
// 注册语言翻译 // 注册语言翻译
func RegisterTrans(langName string, trans map[string]string) { func RegisterTrans(langName string, trans map[string]string) {
l.mut.Lock()
defer l.mut.Unlock()
l.transMap[langName] = trans l.transMap[langName] = trans
} }
// 追加覆盖翻译
func AppendTrans(langName string, trans map[string]string) {
l.mut.Lock()
defer l.mut.Unlock()
for k, v := range trans {
if l.transMap[langName] == nil {
l.transMap[langName] = map[string]string{}
}
l.transMap[langName][k] = v
}
}
// 直接读取文件夹获取配置 // 直接读取文件夹获取配置
// 要求: // 要求:
// 1.json格式文件 // 1.json格式文件
@@ -83,14 +105,90 @@ func RegisterDir(dir string) error {
return nil return nil
}) })
// code文件为状态码,其他为对应的语言文件,文件名为语言名
return err
}
func RegisterEmbed(asset embed.FS) error {
paths, err := readEmbedAllPath(asset, "")
if err != nil { if err != nil {
panic(err) return err
}
for _, ff := range paths {
// 获取文件的后缀
fileName := strings.Replace(ff.path, ".json", "", 1)
if fileName == "code" {
data := map[string]int{}
err = json.Unmarshal(ff.datas, &data)
if err != nil {
return err
}
RegisterCode(data)
} else {
data := map[string]string{}
err = json.Unmarshal(ff.datas, &data)
if err != nil {
return err
}
RegisterTrans(fileName, data)
}
} }
// code文件为状态码,其他为对应的语言文件,文件名为语言名
return nil return nil
} }
type fileData struct {
path string
datas []byte
}
func readEmbedAllPath(asset embed.FS, path string) ([]fileData, error) {
newRoot := ""
if path == "" {
newRoot = "."
} else {
newRoot = path
}
fs, err := asset.ReadDir(newRoot)
if err != nil {
return nil, err
}
var files []fileData
for _, f := range fs {
if f.IsDir() {
chaild := ""
if newRoot == "." {
chaild = f.Name()
} else {
chaild = newRoot + "/" + f.Name()
}
df, err := readEmbedAllPath(asset, chaild)
if err != nil {
return nil, err
}
files = append(files, df...)
continue
}
by, err := asset.ReadFile(newRoot + "/" + f.Name())
if err != nil {
return nil, err
}
ff := fileData{
path: f.Name(),
datas: by,
}
files = append(files, ff)
}
return files, nil
}
// 获取翻译 // 获取翻译
// 包含Code和Message // 包含Code和Message
func GetTransFormat(lang string, key string, format map[string]string) (code int, str string) { func GetTransFormat(lang string, key string, format map[string]string) (code int, str string) {
@@ -113,6 +211,8 @@ func GetTransFormatCtx(ctx context.Context, key string, format map[string]string
// 根据Key获取code // 根据Key获取code
func GetCode(key string) int { func GetCode(key string) int {
l.mut.Lock()
defer l.mut.Unlock()
code, ok := l.codeMap[key] code, ok := l.codeMap[key]
if !ok { if !ok {
return l.ops.defaultCode return l.ops.defaultCode
@@ -122,6 +222,8 @@ func GetCode(key string) int {
// 获取翻译 // 获取翻译
func GetMsg(lang string, key string) string { func GetMsg(lang string, key string) string {
l.mut.Lock()
defer l.mut.Unlock()
// 找指定语言 // 找指定语言
str, ok := l.transMap[lang] str, ok := l.transMap[lang]
if ok { if ok {
@@ -165,11 +267,15 @@ func GetDefaultCode() int {
return l.ops.defaultCode return l.ops.defaultCode
} }
// 获取默认语言
func GetDefaultLang() string {
return l.ops.defaultLang
}
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
} }
+14 -7
View File
@@ -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,14 @@ func defaultOptions() *options {
defaultCode: 200, defaultCode: 200,
defaultLang: "zh", defaultLang: "zh",
replaceKey: "#%s#", replaceKey: "#%s#",
ctxLangKey: "language", // ctxLangKey: "language",
}
}
// 设置
func InitLangx(ops ...Option) {
for _, opt := range ops {
opt(l.ops)
} }
} }
@@ -26,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 {
+5 -1
View File
@@ -1,5 +1,9 @@
# 简介 # 简介
这是简单的一个多语言响应工具 这是简单的一个多语言响应工具,能够通过多种方式加载资源文件,然后进行匹配。同时支持占位符替换的形式在多语言响应的时候替换特定字符,让响应更友好。
# 使用方法
看example