context.go

 1package i18n
 2
 3import "context"
 4
 5type contextKey int
 6
 7const (
 8	// localeContextKey is the key for storing locale in context.
 9	localeContextKey contextKey = iota
10)
11
12// WithLocale returns a new context with the given locale.
13func WithLocale(ctx context.Context, locale string) context.Context {
14	return context.WithValue(ctx, localeContextKey, locale)
15}
16
17// LocaleFromContext extracts the locale from context.
18// Returns empty string if no locale is set.
19func LocaleFromContext(ctx context.Context) string {
20	if locale, ok := ctx.Value(localeContextKey).(string); ok {
21		return locale
22	}
23	return ""
24}