添加部分函数

This commit is contained in:
Yun
2024-07-10 18:28:53 +08:00
parent 9d844ed7d4
commit f84d5b5d28
3 changed files with 36 additions and 20 deletions
+3 -3
View File
@@ -9,10 +9,10 @@ import (
func main() {
langx.RegisterDir("./lang")
code, msg := langx.GetTrans("zh", "success", map[string]string{})
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
fmt.Println(code, msg)
code, msg = langx.GetTrans("en", "error", map[string]string{
"msg":"这是失败的原因",
code, msg = langx.GetTransFormat("en", "error", map[string]string{
"msg": "这是失败的原因",
})
fmt.Println(code, msg)
}
+28 -9
View File
@@ -44,10 +44,9 @@ func RegisterTrans(langName string, trans map[string]string) {
l.transMap[langName] = trans
}
// 直接读取文件夹获取配置
// 要求:
// 1.json格式文件
// 1.json格式文件
// 2.code.json为自定义响应码 格式map[string]int{}
// 3.其他的json文件为对应语音 格式map[string]string{}
// 4.如果json解析错误将会panic
@@ -94,12 +93,24 @@ func RegisterDir(dir string) error {
// 获取翻译
// 包含Code和Message
func GetTrans(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) {
code = GetCode(key)
str = GetFormat(lang, key, format)
return
}
func GetTrans(lang string, key string) (code int, str string) {
return GetTransFormat(lang, key, map[string]string{})
}
func GetTransCtx(ctx context.Context, key string) (code int, str string) {
return GetTrans(getLangFromCtx(ctx), key)
}
func GetTransFormatCtx(ctx context.Context, key string, format map[string]string) (code int, str string) {
return GetTransFormat(getLangFromCtx(ctx), key, format)
}
// 根据Key获取code
func GetCode(key string) int {
code, ok := l.codeMap[key]
@@ -133,12 +144,7 @@ func GetMsg(lang string, key string) string {
// 从ctx里面获取语言
func GetMsgCtx(ctx context.Context, key string) string {
ctxVal := ctx.Value(l.ops.ctxLangKey)
lang := l.ops.defaultLang
if ctxVal != nil {
lang = ctxVal.(string)
}
return GetMsg(lang, key)
return GetMsg(getLangFromCtx(ctx), key)
}
// 拼接回复
@@ -150,7 +156,20 @@ func GetFormat(lang string, key string, arr map[string]string) string {
return str
}
func GetFormatCtx(ctx context.Context, key string, arr map[string]string) string {
return GetFormat(getLangFromCtx(ctx), key, arr)
}
// 获取默认Code
func GetDefaultCode() int {
return l.ops.defaultCode
}
func getLangFromCtx(ctx context.Context) string {
ctxVal := ctx.Value(l.ops.ctxLangKey)
lang := l.ops.defaultLang
if ctxVal != nil {
lang = ctxVal.(string)
}
return lang
}
+5 -8
View File
@@ -6,23 +6,20 @@ import (
"github.com/yuninks/langx"
)
const(
const (
Lang string = "s"
)
var MapCode = map[string]int{
Lang:200,
Lang: 200,
}
func TestLangx(t *testing.T) {
langx.InitLangx(
langx.SetDefaultCode(0),
langx.SetDefaultLanguage("zh"),
)
langx.RegisterCode(map[string]int{
"login_success": 200,
"error": 400,
@@ -38,13 +35,13 @@ func TestLangx(t *testing.T) {
"username": "Hello #name#", // 有占位符
})
// 获取翻译码
code, msg := langx.GetTrans("zh", "login_success", nil)
code, msg := langx.GetTransFormat("zh", "login_success", nil)
t.Log(code, msg)
code, msg = langx.GetTrans("en", "error", nil)
code, msg = langx.GetTransFormat("en", "error", nil)
t.Log(code, msg)
// 获取翻译码,有占位符
code, msg = langx.GetTrans("zh", "username", map[string]string{
code, msg = langx.GetTransFormat("zh", "username", map[string]string{
"name": "张三",
})
t.Log(code, msg)