完成部分翻译功能

This commit is contained in:
2024-04-12 17:17:10 +08:00
parent 6e8a6eb7d1
commit 353b3f0f4f
15 changed files with 1630 additions and 60 deletions

27
utils/basic/lang.go Normal file
View File

@@ -0,0 +1,27 @@
package basic
import "reflect"
func GetLangString(param any) string {
// 获取参数的反射值
value := reflect.ValueOf(param)
// 如果参数是指针,则获取指针指向的值
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
// 如果参数是结构体
if value.Kind() == reflect.Struct {
// 通过字段名获取字段值
langField := value.FieldByName("Lang")
// 如果字段存在并且是字符串类型
if langField.IsValid() && langField.Kind() == reflect.String {
return langField.String()
}
}
// 如果无法获取有效的字符串值则返回zh_cn
return "zh_cn"
}

View File

@@ -1,6 +1,9 @@
package basic
import "github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
import (
"github.com/iapologizewhenimwrong/Vestmore_GO/translator"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
)
// 全局返回的结构体
type Response struct {
@@ -10,6 +13,10 @@ type Response struct {
IsSuccess bool `json:"success"`
}
type ILocalize interface {
Localize(MessageID translator.TrCode) (string, error)
}
func (resp *Response) Error(errcode *ErrorCode, Data ...interface{}) *Response {
if resp == nil {
resp = &Response{}
@@ -46,6 +53,20 @@ func (resp *Response) ErrorMsg(Code int, Message string, Data ...interface{}) *R
return resp
}
// 实现翻译返回错误的代码
func (resp *Response) ErrorTrCode(il ILocalize, trcode translator.TrCode, Data ...interface{}) *Response {
if resp == nil {
resp = &Response{}
}
trStr, err := il.Localize(trcode)
if err != nil {
return resp.ErrorErr(1, err)
}
return resp.ErrorMsg(1, trStr)
}
func (resp *Response) Success(Data ...interface{}) *Response {
if resp == nil {
resp = &Response{}