45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package i18n
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
i18n2 "github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"golang.org/x/text/language"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
func getLocalizer(ctx context.Context) (*i18n2.Localizer, bool) {
|
|
v := ctx.Value(I18nKey)
|
|
if l, b := v.(*i18n2.Localizer); b {
|
|
return l, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func withRequest(r *http.Request, currentLang language.Tag, bundle *i18n2.Bundle) *http.Request {
|
|
|
|
accept := r.Header.Get(defaultLangHeaderKey)
|
|
localizer := i18n2.NewLocalizer(bundle, accept)
|
|
ctx := setLocalizer(r.Context(), localizer)
|
|
ctx = setCurrentLang(ctx, currentLang)
|
|
ctx = metadata.AppendToOutgoingContext(ctx, defaultLangHeaderKey, accept)
|
|
return r.WithContext(ctx)
|
|
}
|
|
|
|
func setCurrentLang(ctx context.Context, currentLang language.Tag) context.Context {
|
|
return context.WithValue(ctx, I18nCurrentLangKey, currentLang)
|
|
}
|
|
|
|
func setLocalizer(ctx context.Context, l *i18n2.Localizer) context.Context {
|
|
return context.WithValue(ctx, I18nKey, l)
|
|
}
|
|
|
|
func IsHasI18n(ctx context.Context) bool {
|
|
v := ctx.Value(I18nKey)
|
|
if v != nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|