42 lines
922 B
Go
42 lines
922 B
Go
package requestctx
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.toowon.com/jimmy/go-common/tools"
|
|
)
|
|
|
|
type languageKey struct{}
|
|
type timezoneKey struct{}
|
|
|
|
const (
|
|
DefaultLanguage = "zh-CN"
|
|
DefaultTimezone = tools.AsiaShanghai
|
|
)
|
|
|
|
// WithLanguage 写入语言到 context
|
|
func WithLanguage(ctx context.Context, lang string) context.Context {
|
|
return context.WithValue(ctx, languageKey{}, lang)
|
|
}
|
|
|
|
// Language 从 context 读取语言
|
|
func Language(ctx context.Context) string {
|
|
if lang, ok := ctx.Value(languageKey{}).(string); ok && lang != "" {
|
|
return lang
|
|
}
|
|
return DefaultLanguage
|
|
}
|
|
|
|
// WithTimezone 写入时区到 context
|
|
func WithTimezone(ctx context.Context, tz string) context.Context {
|
|
return context.WithValue(ctx, timezoneKey{}, tz)
|
|
}
|
|
|
|
// Timezone 从 context 读取时区
|
|
func Timezone(ctx context.Context) string {
|
|
if tz, ok := ctx.Value(timezoneKey{}).(string); ok && tz != "" {
|
|
return tz
|
|
}
|
|
return DefaultTimezone
|
|
}
|