This commit is contained in:
Yun
2024-06-30 11:52:34 +08:00
commit cf9ac56b88
6 changed files with 255 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
package langx
import (
"context"
)
type LangError struct {
ctx context.Context
key string
format map[string]string
}
type errorConst string
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) Code() int {
return GetCode(e.key)
}
func NewErrorFormat(ctx context.Context, key string, format map[string]string) error {
return &LangError{
ctx: ctx,
key: key,
format: format,
}
}
func NewError(ctx context.Context, key string) error {
return &LangError{
ctx: ctx,
key: key,
}
}
func SetCtxLang(ctx context.Context, lang string) context.Context {
return context.WithValue(ctx, errorLang, lang)
}
+42
View File
@@ -0,0 +1,42 @@
package langx_test
import (
"context"
"testing"
"code.yun.ink/pkg/langx"
)
func TestError(t *testing.T) {
var err error
ctx := context.Background()
langx.InitLangx(
langx.SetDefaultCode(0),
langx.SetDefaultLanguage("zh"),
)
langx.RegisterCode(map[string]int{
"login_success": 200,
"error": 400,
})
langx.RegisterTrans("zh", map[string]string{
"login_success": "成功",
"error": "错误",
"username": "你好 #name#", // 有占位符
})
langx.RegisterTrans("en", map[string]string{
"login_success": "success",
"error": "error",
"username": "Hello #name#", // 有占位符
})
err = langx.NewError(ctx, "error")
// fmt.Printf("err: %v\n", err)
t.Log(err.Error())
val, ok := err.(*langx.LangError)
if ok {
t.Log(val.Code())
}
}
+3
View File
@@ -0,0 +1,3 @@
module code.yun.ink/pkg/langx
go 1.19
+88
View File
@@ -0,0 +1,88 @@
package langx
import (
"fmt"
"strings"
)
type langx struct {
ops *options
codeMap map[string]int
transMap map[string]map[string]string
}
var l *langx = &langx{}
func init() {
o := defaultOptions()
l = &langx{
ops: o,
codeMap: make(map[string]int),
transMap: make(map[string]map[string]string),
}
}
func InitLangx(ops ...Option) {
for _, opt := range ops {
opt(l.ops)
}
}
// 这是语言的Code
func RegisterCode(datas map[string]int) {
l.codeMap = datas
}
// 注册语言翻译
func RegisterTrans(langName string, trans map[string]string) {
l.transMap[langName] = trans
}
// 获取翻译
// 包含Code和Message
func GetTrans(lang string, key string, format map[string]string) (code int, str string) {
code = GetCode(key)
str = GetFormat(lang, key, format)
return
}
// 根据Key获取code
func GetCode(key string) int {
code, ok := l.codeMap[key]
if !ok {
return l.ops.defaultCode
}
return code
}
func GetMsg(lang string, key string) string {
// 找指定语言
str, ok := l.transMap[lang]
if ok {
val, ok := str[key]
if ok {
return val
}
}
// 找默认语言
str, ok = l.transMap[l.ops.defaultLang]
if ok {
val, ok := str[key]
if ok {
return val
}
}
return key
}
// 拼接回复
func GetFormat(lang string, key string, arr map[string]string) string {
str := GetMsg(lang, key)
for k, v := range arr {
str = strings.ReplaceAll(str, fmt.Sprintf(l.ops.replaceKey, k), v)
}
return str
}
+40
View File
@@ -0,0 +1,40 @@
package langx_test
import (
"testing"
"code.yun.ink/pkg/langx"
)
func TestLangx(t *testing.T) {
langx.InitLangx(
langx.SetDefaultCode(0),
langx.SetDefaultLanguage("zh"),
)
langx.RegisterCode(map[string]int{
"login_success": 200,
"error": 400,
})
langx.RegisterTrans("zh", map[string]string{
"login_success": "成功",
"error": "错误",
"username": "你好 #name#", // 有占位符
})
langx.RegisterTrans("en", map[string]string{
"login_success": "success",
"error": "error",
"username": "Hello #name#", // 有占位符
})
// 获取翻译码
code, msg := langx.GetTrans("zh", "login_success", nil)
t.Log(code, msg)
code, msg = langx.GetTrans("en", "error", nil)
t.Log(code, msg)
// 获取翻译码,有占位符
code, msg = langx.GetTrans("zh", "username", map[string]string{
"name": "张三",
})
t.Log(code, msg)
}
+35
View File
@@ -0,0 +1,35 @@
package langx
type options struct {
defaultCode int
defaultLang string
replaceKey string
}
func defaultOptions() *options {
return &options{
defaultCode: 200,
defaultLang: "zh",
replaceKey: "#%s#",
}
}
type Option func(*options)
func SetDefaultCode(code int) Option {
return func(o *options) {
o.defaultCode = code
}
}
func SetDefaultLanguage(lang string) Option {
return func(o *options) {
o.defaultLang = lang
}
}
func SetReplaceKey(key string) Option {
return func(o *options) {
o.replaceKey = key
}
}