3 Star 5 Fork 3

三三物联网/ssiot-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
translate.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
三三物联网 提交于 2023-04-03 00:22 . 重构
/*
* @Author: lwnmengjing
* @Date: 2021/6/9 10:39 上午
* @Last Modified by: lwnmengjing
* @Last Modified time: 2021/6/9 10:39 上午
*/
package api
import (
"fmt"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
enTranslations "github.com/go-playground/validator/v10/translations/en"
chTranslations "github.com/go-playground/validator/v10/translations/zh"
)
// transInit local 通常取决于 http 请求头的 'Accept-Language'
func transInit(local string) (trans ut.Translator, err error) {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
zhT := zh.New() //chinese
enT := en.New() //english
uni := ut.New(enT, zhT, enT)
var o bool
//register translate
// 注册翻译器
switch local {
case "zh", "zh-CN":
trans, o = uni.GetTranslator("zh")
if !o {
return nil, fmt.Errorf("uni.GetTranslator(%s) failed", "zh")
}
err = chTranslations.RegisterDefaultTranslations(v, trans)
default:
trans, o = uni.GetTranslator("en")
if !o {
return nil, fmt.Errorf("uni.GetTranslator(%s) failed", "en")
}
err = enTranslations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sansaniot/ssiot-core.git
git@gitee.com:sansaniot/ssiot-core.git
sansaniot
ssiot-core
ssiot-core
v1.7.9

搜索帮助