// Modified in the independent AgentDesk fork: localized knowledge workflows and safe display errors. package i18nx import ( "context" "github.com/gin-gonic/gin" ) const contextLocaleKey = "i18nx.locale" func Locale(ctx *gin.Context) string { if ctx == nil { return DefaultLocale } value, ok := ctx.Get(contextLocaleKey) if !ok { return DefaultLocale } locale, ok := value.(string) if !ok { return DefaultLocale } return NormalizeLocale(locale) } func SetLocale(ctx *gin.Context, locale string) { if ctx == nil { return } ctx.Set(contextLocaleKey, NormalizeLocale(locale)) } // localeContextKey is deliberately distinct from Gin's string key. type localeContextKey struct{} // WithLocale carries an explicitly selected application locale without losing // the parent's cancellation, deadline, or values. It does not detect a locale // from HTTP headers or change the configured default locale. func WithLocale(ctx context.Context, locale string) context.Context { if ctx == nil { ctx = context.Background() } return context.WithValue(ctx, localeContextKey{}, NormalizeLocale(locale)) } // LocaleFromContext preserves the configured-default behavior of callers that // do not provide an explicit locale (including background jobs). func LocaleFromContext(ctx context.Context) string { if ctx != nil { if locale, ok := ctx.Value(localeContextKey{}).(string); ok { return NormalizeLocale(locale) } } return DefaultLocale }