[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

@@ -1,11 +1,9 @@
package utils
import (
"fmt"
"github.com/spf13/cast"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
)
@@ -182,43 +180,4 @@ func (this *ParseClass) Domain(value any) (domain string) {
return ""
}
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
}