Documentation
¶
Overview ¶
Package i18nmw provides transport middlewares for dealing with internationalisation.
Index ¶
- Variables
- func AppendLocaleToOutgoingContext(ctx context.Context, locale string) context.Context
- func InterceptLocale(ctx context.Context) (string, error)
- func NewStreamServerInterceptor() grpc.ServerOption
- func NewUnaryServerInterceptor() grpc.ServerOption
- func ParseLocaleHandler(next http.HandlerFunc) http.HandlerFunc
- func StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, ...) error
- func UnaryServerInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, ...) (interface{}, error)
- type MiddlewareFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ParseLocaleMiddleware = MiddlewareFunc(ParseLocaleHandler)
ParseLocaleMiddleware wraps the parse locale handler in a gorilla mux middleware.
Functions ¶
func AppendLocaleToOutgoingContext ¶
AppendLocaleToOutgoingContext will inject locale metadata to the outgoing context for use with clients.
func InterceptLocale ¶
InterceptLocale returns the locale from the context.
func NewStreamServerInterceptor ¶
func NewStreamServerInterceptor() grpc.ServerOption
NewStreamServerInterceptor will spawn a server option for internationalisation in streaming operations.
Example ¶
package main import ( "github.com/LUSHDigital/core/middleware/i18nmw" "google.golang.org/grpc" ) var server *grpc.Server func main() { server = grpc.NewServer( i18nmw.NewStreamServerInterceptor(), i18nmw.NewUnaryServerInterceptor(), ) }
Output:
func NewUnaryServerInterceptor ¶
func NewUnaryServerInterceptor() grpc.ServerOption
NewUnaryServerInterceptor will spawn a server option for internationalisation in unary operations.
func ParseLocaleHandler ¶
func ParseLocaleHandler(next http.HandlerFunc) http.HandlerFunc
ParseLocaleHandler will take a language from an http header and attach it to the context.
Example ¶
package main import ( "fmt" "net/http" "github.com/LUSHDigital/core/middleware/i18nmw" "github.com/LUSHDigital/core/i18n" ) var handler http.Handler func main() { handler = i18nmw.ParseLocaleHandler(func(w http.ResponseWriter, r *http.Request) { locale := i18n.LocaleFromContext(r.Context()) fmt.Fprintln(w, locale) }) }
Output:
func StreamServerInterceptor ¶
func StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error
StreamServerInterceptor is a gRPC server-side interceptor that places the locale into context for streaming procedures.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
UnaryServerInterceptor is a gRPC server-side interceptor that places the locale into context for unary procedures.
Types ¶
type MiddlewareFunc ¶
type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
MiddlewareFunc represents a middleware func for use with gorilla mux.
func (MiddlewareFunc) Middleware ¶
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler
Middleware allows MiddlewareFunc to implement the middleware interface.