mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-12 19:30:25 +08:00
添加多语言最基础代码
This commit is contained in:
74
pkg/langs/utils.go
Normal file
74
pkg/langs/utils.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package langs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Message 读取消息
|
||||
// Read message
|
||||
func Message(langCode string, messageCode MessageCode, args ...any) string {
|
||||
return defaultManager.GetMessage(langCode, messageCode, args...)
|
||||
}
|
||||
|
||||
func ParseLangFromRequest(req *http.Request) (langCode string) {
|
||||
// parse language from cookie
|
||||
const cookieName = "edgelang"
|
||||
cookie, _ := req.Cookie(cookieName)
|
||||
if cookie != nil && len(cookie.Value) > 0 && defaultManager.HasLang(cookie.Value) {
|
||||
return cookie.Value
|
||||
}
|
||||
|
||||
// parse language from 'Accept-Language'
|
||||
var acceptLanguage = req.Header.Get("Accept-Language")
|
||||
if len(acceptLanguage) > 0 {
|
||||
var pieces = strings.Split(acceptLanguage, ",")
|
||||
for _, lang := range pieces {
|
||||
var index = strings.Index(lang, ";")
|
||||
if index >= 0 {
|
||||
lang = lang[:index]
|
||||
}
|
||||
|
||||
var match = defaultManager.MatchLang(lang)
|
||||
if len(match) > 0 {
|
||||
return match
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return defaultManager.DefaultLang()
|
||||
}
|
||||
|
||||
func ParseLangFromAction(action actions.ActionWrapper) (langCode string) {
|
||||
return ParseLangFromRequest(action.Object().Request)
|
||||
}
|
||||
|
||||
// Format 格式化变量
|
||||
// Format string that contains message variables, such as ${lang.MESSAGE_CODE}
|
||||
//
|
||||
// 暂时不支持变量中加参数
|
||||
func Format(langCode string, varString string) string {
|
||||
return configutils.ParseVariables(varString, func(varName string) (value string) {
|
||||
const prefix = "lang."
|
||||
if !strings.HasPrefix(varName, prefix) {
|
||||
return "${" + varName + "}" // keep origin variable
|
||||
}
|
||||
return Message(langCode, varName[len(prefix):])
|
||||
})
|
||||
}
|
||||
|
||||
// Load 加载消息定义
|
||||
// Load message definitions from map
|
||||
func Load(langCode string, messageMap map[string]string) {
|
||||
lang, ok := defaultManager.GetLang(langCode)
|
||||
if !ok {
|
||||
lang = defaultManager.AddLang(langCode)
|
||||
}
|
||||
for messageCode, messageText := range messageMap {
|
||||
lang.Set(messageCode, messageText)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user