[v1.6.0] 重新封装 Lang

This commit is contained in:
兔子
2025-01-16 16:51:08 +08:00
parent b141f3177c
commit a0106918f1
2 changed files with 69 additions and 71 deletions

View File

@@ -3,16 +3,54 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/spf13/cast" "github.com/spf13/cast"
"strconv"
"strings"
) )
var Lang *LangClass
type LangClass struct { type LangClass struct {
Directory string // 语言包目录 Directory string // 语言包目录
Lang string // 当前语言 Lang string // 当前语言
Mode string // 文件类型 Mode string // 文件类型
} }
// Lang 实例化 func (this *LangClass) Value(key any, args ...any) (result any) {
func Lang(model ...LangClass) *LangClass {
// 读取语言包
bytes := File().Byte(this.Directory + this.Lang + "." + this.Mode)
if bytes.Error != nil { return }
text := cast.ToString(key)
// 解析语言包
lang := cast.ToStringMap(Json.Decode(bytes.Text))
// 获取语言
result = lang[text]
// 如果没有找到语言通过javascript风格获取
if Is.Empty(result) {
item, err := Json.Get(bytes.Text, text)
if err == nil { result = item }
}
// 如果没有找到语言,则返回原文
if Is.Empty(result) {
return fmt.Sprintf(text, args...)
}
// 如果有参数,则格式化
if len(args) > 0 {
return fmt.Sprintf(cast.ToString(result), args...)
}
return
}
// New 实例化
func (this *LangClass) New(model ...LangClass) *LangClass {
item := new(LangClass) item := new(LangClass)
@@ -29,40 +67,41 @@ func Lang(model ...LangClass) *LangClass {
return item return item
} }
func (this *LangClass) Value(key any, args ...any) (result any) { type Language struct {
Language string
// 读取语言包 Quality float64
bytes := File().Byte(this.Directory + this.Lang + "." + this.Mode) }
if bytes.Error != nil { // AcceptLanguage - 解析请求头的 Accept-Language
return func (this *LangClass) AcceptLanguage(value any) (string, []Language, error) {
}
var hot Language
text := cast.ToString(key) var items []Language
// 解析语言包 for _, val := range strings.Split(cast.ToString(value), ",") {
lang := cast.ToStringMap(Json.Decode(bytes.Text))
params := strings.Split(val, ";")
// 获取语言 lang := params[0]
result = lang[text] quality := 1.0
// 如果没有找到语言通过javascript风格获取 if len(params) > 1 {
if Is.Empty(result) { q, err := strconv.ParseFloat(strings.TrimPrefix(params[1], "q="), 64)
item, err := Json.Get(bytes.Text, text) if err != nil {
if err == nil { return "", nil, fmt.Errorf("invalid quality value: %v", err)
result = item }
} quality = q
} }
// 如果没有找到语言,则返回原文 item := Language{
if Is.Empty(result) { Language: lang,
return fmt.Sprintf(text, args...) Quality : quality,
} }
items = append(items, item)
// 如果有参数,则格式化
if len(args) > 0 { if len(hot.Language) == 0 || item.Quality > hot.Quality {
return fmt.Sprintf(cast.ToString(result), args...) hot = item
} }
}
return
return hot.Language, items, nil
} }

View File

@@ -1,11 +1,9 @@
package utils package utils
import ( import (
"fmt"
"github.com/spf13/cast" "github.com/spf13/cast"
"net/url" "net/url"
"regexp" "regexp"
"strconv"
"strings" "strings"
"sync" "sync"
) )
@@ -183,42 +181,3 @@ func (this *ParseClass) Domain(value any) (domain string) {
} }
return URL.Hostname() return URL.Hostname()
} }
type Language struct {
Language string
Quality float64
}
// AcceptLanguage - 解析请求头的 Accept-Language
func (this *ParseClass) AcceptLanguage(value string) (string, []Language, error) {
var hot Language
var items []Language
for _, val := range strings.Split(value, ",") {
params := strings.Split(val, ";")
lang := params[0]
quality := 1.0
if len(params) > 1 {
q, err := strconv.ParseFloat(strings.TrimPrefix(params[1], "q="), 64)
if err != nil {
return "", nil, fmt.Errorf("invalid quality value: %v", err)
}
quality = q
}
item := Language{
Language: lang,
Quality : quality,
}
items = append(items, item)
if len(hot.Language) == 0 || item.Quality > hot.Quality {
hot = item
}
}
return hot.Language, items, nil
}