Files
go-utils/utils/lang.go
兔子 c699bc7d0c v1.0.7
v1.0.7
2023-04-12 17:07:16 +08:00

68 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"fmt"
"github.com/spf13/cast"
)
type LangModel struct {
Directory string // 语言包目录
Lang string // 当前语言
Mode string // 文件类型
}
// Lang 实例化
func Lang(model ...LangModel) *LangModel {
item := new(LangModel)
// 合并参数
if len(model) > 0 {
item = &model[0]
}
// 设置默认值
item.Lang = Ternary[string](!IsEmpty(item.Lang), item.Lang, "zh-cn")
item.Mode = Ternary[string](!IsEmpty(item.Mode), item.Mode, "json")
item.Directory = Ternary[string](!IsEmpty(item.Directory), item.Directory, "config/i18n/")
return item
}
func (this *LangModel) Value(key any, args ...any) (result any) {
// 读取语言包
bytes := File().Byte(this.Directory + this.Lang + "." + this.Mode)
if bytes.Error != nil {
return
}
text := cast.ToString(key)
// 解析语言包
lang := cast.ToStringMap(JsonDecode(bytes.Text))
// 获取语言
result = lang[text]
// 如果没有找到语言通过javascript风格获取
if IsEmpty(result) {
item, err := JsonGet(bytes.Text, text)
if err == nil {
result = item
}
}
// 如果没有找到语言,则返回原文
if IsEmpty(result) {
return text
}
// 如果有参数,则格式化
if len(args) > 0 {
return fmt.Sprintf(cast.ToString(result), args...)
}
return
}