2026-05-25 12:06:15 +08:00
|
|
|
package i18nx
|
|
|
|
|
|
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
const contextLocaleKey = "i18nx.locale"
|
|
|
|
|
|
|
|
|
|
func Locale(ctx *gin.Context) string {
|
|
|
|
|
if ctx == nil {
|
2026-05-31 20:06:44 +08:00
|
|
|
return DefaultLocale
|
2026-05-25 12:06:15 +08:00
|
|
|
}
|
|
|
|
|
value, ok := ctx.Get(contextLocaleKey)
|
|
|
|
|
if !ok {
|
2026-05-31 20:06:44 +08:00
|
|
|
return DefaultLocale
|
2026-05-25 12:06:15 +08:00
|
|
|
}
|
|
|
|
|
locale, ok := value.(string)
|
|
|
|
|
if !ok {
|
2026-05-31 20:06:44 +08:00
|
|
|
return DefaultLocale
|
2026-05-25 12:06:15 +08:00
|
|
|
}
|
|
|
|
|
return NormalizeLocale(locale)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetLocale(ctx *gin.Context, locale string) {
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Set(contextLocaleKey, NormalizeLocale(locale))
|
|
|
|
|
}
|