You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
511B

  1. package translation
  2. import (
  3. "context"
  4. "golang.org/x/text/language"
  5. )
  6. type ctxkey uint8
  7. const (
  8. localeKey ctxkey = iota
  9. )
  10. // WithLanguage sets language to context.
  11. func WithLanguage(ctx context.Context, lang language.Tag) context.Context {
  12. return context.WithValue(ctx, localeKey, lang)
  13. }
  14. // FromContext get language from context or use default.
  15. func FromContext(ctx context.Context, def language.Tag) language.Tag {
  16. if t, ok := ctx.Value(localeKey).(language.Tag); ok {
  17. return t
  18. }
  19. return def
  20. }