Files
agent-desk-ng/internal/pkg/i18nx/context.go
mono 2f7a491666 feat: harden support, localize knowledge and add operator availability [skip ci]
Source-only publication of reviewed commit 64e96a7f423673dc29ad56485be9fc79bce8af1c.

Passed in isolation: focused frontend/backend tests, race checks, TypeScript typecheck, lint, Go compilation, Flowgram build, and deterministic SDK build/tests.

Not accepted as a production release: Next.js export/native assembly remains blocked by environment memory limits; browser interaction and MySQL staging acceptance remain unverified. No production server, database/schema, runtime configuration, deployment, release tag, or workflow execution was performed.

Expected source tree: 0a4a7528e9.
2026-10-16 15:21:42 +00:00

57 lines
1.4 KiB
Go

// 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
}