Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 046091cb35 | |||
| 7eaf21634c | |||
| f66e76883f | |||
| 3a4c3e58e6 | |||
| 8d3bd7cade | |||
| ea8d17943d | |||
| d9fdeabf85 | |||
| d710d6b30c | |||
| 5205a6cad1 | |||
| 2bed4e7fca |
@@ -0,0 +1,17 @@
|
||||
package langx
|
||||
|
||||
import "context"
|
||||
|
||||
const ctxLangKey string = "ctxLang"
|
||||
|
||||
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, ctxLangKey, lang)
|
||||
}
|
||||
|
||||
func GetCtxLang(ctx context.Context) string {
|
||||
lang, ok := ctx.Value(ctxLangKey).(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return lang
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
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#")
|
||||
)
|
||||
@@ -4,42 +4,49 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type LangError struct {
|
||||
// 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
|
||||
}
|
||||
|
||||
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 (l *langError) Error() string {
|
||||
return GetFormat(GetCtxLang(l.ctx), l.key, l.format)
|
||||
}
|
||||
|
||||
func (e *LangError) GetCode() int {
|
||||
func (e *langError) GetCode() int {
|
||||
return GetCode(e.key)
|
||||
}
|
||||
|
||||
func (e *LangError) GetMsg() string {
|
||||
func (e *langError) GetKey() string {
|
||||
return e.key
|
||||
}
|
||||
|
||||
func (e *LangError) GetFormat() map[string]string {
|
||||
func (e *langError) GetFormat() map[string]string {
|
||||
if e.format == nil {
|
||||
e.format = make(map[string]string)
|
||||
}
|
||||
return e.format
|
||||
}
|
||||
|
||||
func NewErrorFormat(ctx context.Context, key string, format map[string]string) error {
|
||||
return &LangError{
|
||||
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,
|
||||
@@ -47,12 +54,8 @@ func NewErrorFormat(ctx context.Context, key string, format map[string]string) e
|
||||
}
|
||||
|
||||
func NewError(ctx context.Context, key string) error {
|
||||
return &LangError{
|
||||
return &langError{
|
||||
ctx: ctx,
|
||||
key: key,
|
||||
}
|
||||
}
|
||||
|
||||
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, errorLang, lang)
|
||||
}
|
||||
|
||||
+9
-3
@@ -35,18 +35,24 @@ func TestError(t *testing.T) {
|
||||
err = langx.NewError(ctx, "error")
|
||||
// fmt.Printf("err: %v\n", err)
|
||||
t.Log(err.Error())
|
||||
val, ok := err.(*langx.LangError)
|
||||
val, ok := err.(langx.LangError)
|
||||
if ok {
|
||||
t.Log(val.GetCode())
|
||||
}
|
||||
|
||||
err = langx.NewErrorFormat(ctx, "username", map[string]string{
|
||||
err = langx.NewErrorf(ctx, "username", map[string]string{
|
||||
"name": "yuninks",
|
||||
})
|
||||
t.Log(err.Error())
|
||||
val, ok = err.(*langx.LangError)
|
||||
val, ok = err.(langx.LangError)
|
||||
if ok {
|
||||
t.Log(val.GetCode())
|
||||
|
||||
// 设置输出语言
|
||||
val.SetLang("en")
|
||||
|
||||
}
|
||||
|
||||
t.Log(val.Error())
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
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#")
|
||||
)
|
||||
@@ -0,0 +1,54 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# 导入资源
|
||||
|
||||
# 通过embed导入
|
||||
|
||||
# 通过文件导入
|
||||
|
||||
# 追加导入
|
||||
@@ -1,18 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yuninks/langx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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)
|
||||
}
|
||||
@@ -2,18 +2,21 @@ package langx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type langx struct {
|
||||
ops *options
|
||||
codeMap map[string]int
|
||||
transMap map[string]map[string]string
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
var l *langx = &langx{}
|
||||
@@ -24,26 +27,45 @@ func init() {
|
||||
ops: defaultOptions(),
|
||||
codeMap: make(map[string]int),
|
||||
transMap: make(map[string]map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// 设置
|
||||
func InitLangx(ops ...Option) {
|
||||
for _, opt := range ops {
|
||||
opt(l.ops)
|
||||
mut: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// 这是语言的Code
|
||||
func RegisterCode(datas map[string]int) {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
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) {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
l.transMap[langName] = trans
|
||||
}
|
||||
|
||||
// 追加覆盖翻译
|
||||
func AppendTrans(langName string, trans map[string]string) {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
for k, v := range trans {
|
||||
if l.transMap[langName] == nil {
|
||||
l.transMap[langName] = map[string]string{}
|
||||
}
|
||||
l.transMap[langName][k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// 直接读取文件夹获取配置
|
||||
// 要求:
|
||||
// 1.json格式文件
|
||||
@@ -83,14 +105,90 @@ func RegisterDir(dir string) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
// code文件为状态码,其他为对应的语言文件,文件名为语言名
|
||||
return err
|
||||
}
|
||||
|
||||
func RegisterEmbed(asset embed.FS) error {
|
||||
paths, err := readEmbedAllPath(asset, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ff := range paths {
|
||||
// 获取文件的后缀
|
||||
|
||||
fileName := strings.Replace(ff.path, ".json", "", 1)
|
||||
if fileName == "code" {
|
||||
data := map[string]int{}
|
||||
err = json.Unmarshal(ff.datas, &data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
RegisterCode(data)
|
||||
} else {
|
||||
data := map[string]string{}
|
||||
err = json.Unmarshal(ff.datas, &data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
RegisterTrans(fileName, data)
|
||||
}
|
||||
}
|
||||
|
||||
// code文件为状态码,其他为对应的语言文件,文件名为语言名
|
||||
return nil
|
||||
}
|
||||
|
||||
type fileData struct {
|
||||
path string
|
||||
datas []byte
|
||||
}
|
||||
|
||||
func readEmbedAllPath(asset embed.FS, path string) ([]fileData, error) {
|
||||
newRoot := ""
|
||||
if path == "" {
|
||||
newRoot = "."
|
||||
} else {
|
||||
newRoot = path
|
||||
}
|
||||
|
||||
fs, err := asset.ReadDir(newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var files []fileData
|
||||
for _, f := range fs {
|
||||
if f.IsDir() {
|
||||
chaild := ""
|
||||
if newRoot == "." {
|
||||
chaild = f.Name()
|
||||
} else {
|
||||
chaild = newRoot + "/" + f.Name()
|
||||
}
|
||||
|
||||
df, err := readEmbedAllPath(asset, chaild)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, df...)
|
||||
continue
|
||||
}
|
||||
|
||||
by, err := asset.ReadFile(newRoot + "/" + f.Name())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ff := fileData{
|
||||
path: f.Name(),
|
||||
datas: by,
|
||||
}
|
||||
|
||||
files = append(files, ff)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// 获取翻译
|
||||
// 包含Code和Message
|
||||
func GetTransFormat(lang string, key string, format map[string]string) (code int, str string) {
|
||||
@@ -113,6 +211,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()
|
||||
code, ok := l.codeMap[key]
|
||||
if !ok {
|
||||
return l.ops.defaultCode
|
||||
@@ -122,6 +222,8 @@ func GetCode(key string) int {
|
||||
|
||||
// 获取翻译
|
||||
func GetMsg(lang string, key string) string {
|
||||
l.mut.Lock()
|
||||
defer l.mut.Unlock()
|
||||
// 找指定语言
|
||||
str, ok := l.transMap[lang]
|
||||
if ok {
|
||||
@@ -165,11 +267,15 @@ func GetDefaultCode() int {
|
||||
return l.ops.defaultCode
|
||||
}
|
||||
|
||||
// 获取默认语言
|
||||
func GetDefaultLang() string {
|
||||
return l.ops.defaultLang
|
||||
}
|
||||
|
||||
func getLangFromCtx(ctx context.Context) string {
|
||||
ctxVal := ctx.Value(l.ops.ctxLangKey)
|
||||
lang := l.ops.defaultLang
|
||||
if ctxVal != nil {
|
||||
lang = ctxVal.(string)
|
||||
lang := GetCtxLang(ctx)
|
||||
if lang == "" {
|
||||
lang = l.ops.defaultLang
|
||||
}
|
||||
return lang
|
||||
}
|
||||
|
||||
+14
-7
@@ -4,7 +4,7 @@ type options struct {
|
||||
defaultCode int
|
||||
defaultLang string
|
||||
replaceKey string
|
||||
ctxLangKey string
|
||||
// ctxLangKey string
|
||||
}
|
||||
|
||||
func defaultOptions() *options {
|
||||
@@ -12,7 +12,14 @@ func defaultOptions() *options {
|
||||
defaultCode: 200,
|
||||
defaultLang: "zh",
|
||||
replaceKey: "#%s#",
|
||||
ctxLangKey: "language",
|
||||
// ctxLangKey: "language",
|
||||
}
|
||||
}
|
||||
|
||||
// 设置
|
||||
func InitLangx(ops ...Option) {
|
||||
for _, opt := range ops {
|
||||
opt(l.ops)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +33,11 @@ func SetDefaultCode(code int) Option {
|
||||
}
|
||||
|
||||
// 从ctx里面获取语言的key
|
||||
func SetCtxLangKey(key string) Option {
|
||||
return func(o *options) {
|
||||
o.ctxLangKey = key
|
||||
}
|
||||
}
|
||||
// func SetCtxLangKey(key string) Option {
|
||||
// return func(o *options) {
|
||||
// o.ctxLangKey = key
|
||||
// }
|
||||
// }
|
||||
|
||||
// 默认语言
|
||||
func SetDefaultLanguage(lang string) Option {
|
||||
|
||||
Reference in New Issue
Block a user