添加锁 避免panic

This commit is contained in:
Yun
2024-07-21 11:10:41 +08:00
parent 5205a6cad1
commit d710d6b30c
+29
View File
@@ -9,12 +9,14 @@ import (
"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{}
@@ -25,6 +27,7 @@ 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{},
} }
} }
@@ -37,14 +40,36 @@ func InitLangx(ops ...Option) {
// 这是语言的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 {
l.transMap[langName][k] = v
}
}
// 直接读取文件夹获取配置 // 直接读取文件夹获取配置
// 要求: // 要求:
// 1.json格式文件 // 1.json格式文件
@@ -190,6 +215,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
@@ -199,6 +226,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 {