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
511 B
28 lines
511 B
3 years ago
|
package translation
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"golang.org/x/text/language"
|
||
|
)
|
||
|
|
||
|
type ctxkey uint8
|
||
|
|
||
|
const (
|
||
|
localeKey ctxkey = iota
|
||
|
)
|
||
|
|
||
|
// WithLanguage sets language to context.
|
||
|
func WithLanguage(ctx context.Context, lang language.Tag) context.Context {
|
||
|
return context.WithValue(ctx, localeKey, lang)
|
||
|
}
|
||
|
|
||
|
// FromContext get language from context or use default.
|
||
|
func FromContext(ctx context.Context, def language.Tag) language.Tag {
|
||
|
if t, ok := ctx.Value(localeKey).(language.Tag); ok {
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
return def
|
||
|
}
|