Files
ai-agent/internal/pkg/i18nx/middleware.go
T
mlogclub f827a7471c refactor: remove LocaleSwitcher component and update locale handling
- Removed LocaleSwitcher from NavUser, SiteHeader, and WorkbenchHeader components.
- Updated tests to reflect the removal of LocaleSwitcher.
- Changed default locale from "en-US" to "zh-CN" in i18n configuration.
- Refactored locale resolution logic to read configured locale without relying on browser language detection.
- Updated AgentDesk SDK to support language configuration from public API.
- Cleaned up unused auth options fetching in the auth API.
2026-06-26 14:46:01 +08:00

52 lines
904 B
Go

package i18nx
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
const (
LocaleZhCN = "zh-CN"
LocaleEnUS = "en-US"
)
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)
}
func NormalizeLocale(value string) string {
key := strings.ToLower(strings.TrimSpace(value))
if key == "" {
return DefaultLocale
}
if locale, ok := supportedLocales[key]; ok {
return locale
}
return DefaultLocale
}
func ResolveRequestLocale(_ *http.Request) string {
return DefaultLocale
}
func Middleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
SetLocale(ctx, ResolveRequestLocale(ctx.Request))
ctx.Next()
}
}