Files
ai-agent/internal/pkg/i18nx/middleware.go
T

52 lines
904 B
Go
Raw Normal View History

2026-05-25 12:06:15 +08:00
package i18nx
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
const (
LocaleZhCN = "zh-CN"
LocaleEnUS = "en-US"
2026-05-25 12:06:15 +08:00
)
var supportedLocales = map[string]string{
"zh": LocaleZhCN,
"zh-cn": LocaleZhCN,
"zh_cn": LocaleZhCN,
"zh-hans": LocaleZhCN,
"en": LocaleEnUS,
"en-us": LocaleEnUS,
"en_us": LocaleEnUS,
}
var DefaultLocale = LocaleZhCN
func SetDefaultLocale(locale string) {
DefaultLocale = NormalizeLocale(locale)
}
2026-05-25 12:06:15 +08:00
func NormalizeLocale(value string) string {
key := strings.ToLower(strings.TrimSpace(value))
if key == "" {
return DefaultLocale
2026-05-25 12:06:15 +08:00
}
if locale, ok := supportedLocales[key]; ok {
return locale
}
return DefaultLocale
2026-05-25 12:06:15 +08:00
}
func ResolveRequestLocale(_ *http.Request) string {
return DefaultLocale
2026-05-25 12:06:15 +08:00
}
func Middleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
SetLocale(ctx, ResolveRequestLocale(ctx.Request))
ctx.Next()
}
}