添加部分函数
This commit is contained in:
+3
-3
@@ -9,10 +9,10 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
langx.RegisterDir("./lang")
|
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)
|
fmt.Println(code, msg)
|
||||||
code, msg = langx.GetTrans("en", "error", map[string]string{
|
code, msg = langx.GetTransFormat("en", "error", map[string]string{
|
||||||
"msg":"这是失败的原因",
|
"msg": "这是失败的原因",
|
||||||
})
|
})
|
||||||
fmt.Println(code, msg)
|
fmt.Println(code, msg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ func RegisterTrans(langName string, trans map[string]string) {
|
|||||||
l.transMap[langName] = trans
|
l.transMap[langName] = trans
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 直接读取文件夹获取配置
|
// 直接读取文件夹获取配置
|
||||||
// 要求:
|
// 要求:
|
||||||
// 1.json格式文件
|
// 1.json格式文件
|
||||||
@@ -94,12 +93,24 @@ func RegisterDir(dir string) error {
|
|||||||
|
|
||||||
// 获取翻译
|
// 获取翻译
|
||||||
// 包含Code和Message
|
// 包含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)
|
code = GetCode(key)
|
||||||
str = GetFormat(lang, key, format)
|
str = GetFormat(lang, key, format)
|
||||||
return
|
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
|
// 根据Key获取code
|
||||||
func GetCode(key string) int {
|
func GetCode(key string) int {
|
||||||
code, ok := l.codeMap[key]
|
code, ok := l.codeMap[key]
|
||||||
@@ -133,12 +144,7 @@ func GetMsg(lang string, key string) string {
|
|||||||
|
|
||||||
// 从ctx里面获取语言
|
// 从ctx里面获取语言
|
||||||
func GetMsgCtx(ctx context.Context, key string) string {
|
func GetMsgCtx(ctx context.Context, key string) string {
|
||||||
ctxVal := ctx.Value(l.ops.ctxLangKey)
|
return GetMsg(getLangFromCtx(ctx), key)
|
||||||
lang := l.ops.defaultLang
|
|
||||||
if ctxVal != nil {
|
|
||||||
lang = ctxVal.(string)
|
|
||||||
}
|
|
||||||
return GetMsg(lang, key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拼接回复
|
// 拼接回复
|
||||||
@@ -150,7 +156,20 @@ func GetFormat(lang string, key string, arr map[string]string) string {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetFormatCtx(ctx context.Context, key string, arr map[string]string) string {
|
||||||
|
return GetFormat(getLangFromCtx(ctx), key, arr)
|
||||||
|
}
|
||||||
|
|
||||||
// 获取默认Code
|
// 获取默认Code
|
||||||
func GetDefaultCode() int {
|
func GetDefaultCode() int {
|
||||||
return l.ops.defaultCode
|
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
@@ -6,23 +6,20 @@ import (
|
|||||||
"github.com/yuninks/langx"
|
"github.com/yuninks/langx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const(
|
const (
|
||||||
Lang string = "s"
|
Lang string = "s"
|
||||||
)
|
)
|
||||||
|
|
||||||
var MapCode = map[string]int{
|
var MapCode = map[string]int{
|
||||||
Lang:200,
|
Lang: 200,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestLangx(t *testing.T) {
|
func TestLangx(t *testing.T) {
|
||||||
langx.InitLangx(
|
langx.InitLangx(
|
||||||
langx.SetDefaultCode(0),
|
langx.SetDefaultCode(0),
|
||||||
langx.SetDefaultLanguage("zh"),
|
langx.SetDefaultLanguage("zh"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
langx.RegisterCode(map[string]int{
|
langx.RegisterCode(map[string]int{
|
||||||
"login_success": 200,
|
"login_success": 200,
|
||||||
"error": 400,
|
"error": 400,
|
||||||
@@ -38,13 +35,13 @@ func TestLangx(t *testing.T) {
|
|||||||
"username": "Hello #name#", // 有占位符
|
"username": "Hello #name#", // 有占位符
|
||||||
})
|
})
|
||||||
// 获取翻译码
|
// 获取翻译码
|
||||||
code, msg := langx.GetTrans("zh", "login_success", nil)
|
code, msg := langx.GetTransFormat("zh", "login_success", nil)
|
||||||
t.Log(code, msg)
|
t.Log(code, msg)
|
||||||
code, msg = langx.GetTrans("en", "error", nil)
|
code, msg = langx.GetTransFormat("en", "error", nil)
|
||||||
t.Log(code, msg)
|
t.Log(code, msg)
|
||||||
|
|
||||||
// 获取翻译码,有占位符
|
// 获取翻译码,有占位符
|
||||||
code, msg = langx.GetTrans("zh", "username", map[string]string{
|
code, msg = langx.GetTransFormat("zh", "username", map[string]string{
|
||||||
"name": "张三",
|
"name": "张三",
|
||||||
})
|
})
|
||||||
t.Log(code, msg)
|
t.Log(code, msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user