Compare commits
6 Commits
046091cb35
...
4edb2d244e
| Author | SHA1 | Date | |
|---|---|---|---|
| 4edb2d244e | |||
| 914a74d05e | |||
| 8aaf4fe712 | |||
| 38a46b85e1 | |||
| 7ac8c24d2a | |||
| 175e36ebd7 |
@@ -9,9 +9,27 @@ func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||
}
|
||||
|
||||
func GetCtxLang(ctx context.Context) string {
|
||||
lang, ok := ctx.Value(ctxLangKey).(string)
|
||||
if !ok {
|
||||
return ""
|
||||
// 指定的
|
||||
lang := ctx.Value(ctxLangKey)
|
||||
|
||||
l := ""
|
||||
|
||||
if lang != nil {
|
||||
l = lang.(string)
|
||||
}
|
||||
return lang
|
||||
|
||||
// HTTP头部的
|
||||
if l == "" {
|
||||
lang = ctx.Value("Accept-Language")
|
||||
if lang != nil {
|
||||
l = lang.(string)
|
||||
}
|
||||
}
|
||||
|
||||
// 默认的
|
||||
if l == "" {
|
||||
l = GetDefaultLang()
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
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#")
|
||||
)
|
||||
@@ -1,61 +0,0 @@
|
||||
package langx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// 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
|
||||
key string
|
||||
format map[string]string
|
||||
}
|
||||
|
||||
func (l *langError) Error() string {
|
||||
return GetFormat(GetCtxLang(l.ctx), l.key, l.format)
|
||||
}
|
||||
|
||||
func (e *langError) GetCode() int {
|
||||
return GetCode(e.key)
|
||||
}
|
||||
|
||||
func (e *langError) GetKey() string {
|
||||
return e.key
|
||||
}
|
||||
|
||||
func (e *langError) GetFormat() map[string]string {
|
||||
if e.format == nil {
|
||||
e.format = make(map[string]string)
|
||||
}
|
||||
return e.format
|
||||
}
|
||||
|
||||
func (e *langError) SetLang(lang string) {
|
||||
e.ctx = SetCtxLang(e.ctx, lang)
|
||||
}
|
||||
|
||||
func NewErrorf(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,
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package langx_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/yuninks/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,
|
||||
"username": 201,
|
||||
})
|
||||
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.GetCode())
|
||||
}
|
||||
|
||||
err = langx.NewErrorf(ctx, "username", map[string]string{
|
||||
"name": "yuninks",
|
||||
})
|
||||
t.Log(err.Error())
|
||||
val, ok = err.(langx.LangError)
|
||||
if ok {
|
||||
t.Log(val.GetCode())
|
||||
|
||||
// 设置输出语言
|
||||
val.SetLang("en")
|
||||
|
||||
}
|
||||
|
||||
t.Log(val.Error())
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
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#")
|
||||
)
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"success":200,
|
||||
"error":400
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"success":"Success",
|
||||
"error":"Error #msg#"
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"success":"成功",
|
||||
"error":"失败 #msg#"
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
# 导入资源
|
||||
|
||||
# 通过embed导入
|
||||
|
||||
# 通过文件导入
|
||||
|
||||
# 追加导入
|
||||
@@ -16,25 +16,23 @@ type langx struct {
|
||||
ops *options
|
||||
codeMap map[string]int
|
||||
transMap map[string]map[string]string
|
||||
mut sync.Mutex
|
||||
mut sync.RWMutex
|
||||
}
|
||||
|
||||
var l *langx = &langx{}
|
||||
|
||||
func init() {
|
||||
|
||||
l = &langx{
|
||||
ops: defaultOptions(),
|
||||
codeMap: make(map[string]int),
|
||||
transMap: make(map[string]map[string]string),
|
||||
mut: sync.Mutex{},
|
||||
}
|
||||
var l *langx = &langx{
|
||||
ops: defaultOptions(),
|
||||
codeMap: make(map[string]int),
|
||||
transMap: make(map[string]map[string]string),
|
||||
mut: sync.RWMutex{},
|
||||
}
|
||||
|
||||
// 这是语言的Code
|
||||
func RegisterCode(datas map[string]int) {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
if datas == nil {
|
||||
datas = make(map[string]int)
|
||||
}
|
||||
l.codeMap = datas
|
||||
}
|
||||
|
||||
@@ -42,6 +40,14 @@ func RegisterCode(datas map[string]int) {
|
||||
func AppendCode(datas map[string]int) {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
|
||||
if datas == nil {
|
||||
datas = make(map[string]int)
|
||||
}
|
||||
if l.codeMap == nil {
|
||||
l.codeMap = map[string]int{}
|
||||
}
|
||||
|
||||
for k, v := range datas {
|
||||
l.codeMap[k] = v
|
||||
}
|
||||
@@ -211,8 +217,8 @@ func GetTransFormatCtx(ctx context.Context, key string, format map[string]string
|
||||
|
||||
// 根据Key获取code
|
||||
func GetCode(key string) int {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
l.mut.RLock()
|
||||
defer l.mut.RUnlock()
|
||||
code, ok := l.codeMap[key]
|
||||
if !ok {
|
||||
return l.ops.defaultCode
|
||||
@@ -222,8 +228,8 @@ func GetCode(key string) int {
|
||||
|
||||
// 获取翻译
|
||||
func GetMsg(lang string, key string) string {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
l.mut.RLock()
|
||||
defer l.mut.RUnlock()
|
||||
// 找指定语言
|
||||
str, ok := l.transMap[lang]
|
||||
if ok {
|
||||
|
||||
Reference in New Issue
Block a user