mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-05 08:16:50 +08:00
[v1.6.0] 重新封装 Lang
This commit is contained in:
@@ -3,16 +3,54 @@ package utils
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Lang *LangClass
|
||||
|
||||
type LangClass struct {
|
||||
Directory string // 语言包目录
|
||||
Lang string // 当前语言
|
||||
Mode string // 文件类型
|
||||
}
|
||||
|
||||
// Lang 实例化
|
||||
func Lang(model ...LangClass) *LangClass {
|
||||
func (this *LangClass) 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(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)
|
||||
|
||||
@@ -29,40 +67,41 @@ func Lang(model ...LangClass) *LangClass {
|
||||
return item
|
||||
}
|
||||
|
||||
func (this *LangClass) Value(key any, args ...any) (result any) {
|
||||
|
||||
// 读取语言包
|
||||
bytes := File().Byte(this.Directory + this.Lang + "." + this.Mode)
|
||||
|
||||
if bytes.Error != nil {
|
||||
return
|
||||
type Language struct {
|
||||
Language string
|
||||
Quality float64
|
||||
}
|
||||
|
||||
text := cast.ToString(key)
|
||||
// AcceptLanguage - 解析请求头的 Accept-Language
|
||||
func (this *LangClass) AcceptLanguage(value any) (string, []Language, error) {
|
||||
|
||||
// 解析语言包
|
||||
lang := cast.ToStringMap(Json.Decode(bytes.Text))
|
||||
var hot Language
|
||||
var items []Language
|
||||
|
||||
// 获取语言
|
||||
result = lang[text]
|
||||
for _, val := range strings.Split(cast.ToString(value), ",") {
|
||||
|
||||
// 如果没有找到语言,通过javascript风格获取
|
||||
if Is.Empty(result) {
|
||||
item, err := Json.Get(bytes.Text, text)
|
||||
if err == nil {
|
||||
result = item
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到语言,则返回原文
|
||||
if Is.Empty(result) {
|
||||
return fmt.Sprintf(text, args...)
|
||||
}
|
||||
|
||||
// 如果有参数,则格式化
|
||||
if len(args) > 0 {
|
||||
return fmt.Sprintf(cast.ToString(result), args...)
|
||||
}
|
||||
|
||||
return
|
||||
return hot.Language, items, nil
|
||||
}
|
@@ -1,11 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@@ -183,42 +181,3 @@ func (this *ParseClass) Domain(value any) (domain string) {
|
||||
}
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user