i18nmw

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

Internationalisation Middleware

The package core/middleware/i18nmw is used to capture internationalisation parameters from HTTP headers or GRPC metadata.

gRPC server middleware
server = grpc.NewServer(
    i18nmw.NewStreamServerInterceptor(),
    i18nmw.NewUnaryServerInterceptor(),
)
HTTP server middleware

Using gorilla mux.

r := mux.NewRouter()
r.Use(i18nmw.ParseLocaleMiddleware)

Using standard net/http library.

handler = i18nmw.ParseLocaleHandler(func(w http.ResponseWriter, r *http.Request) {
    locale := i18n.LocaleFromContext(r.Context())
    fmt.Fprintln(w, locale)
})

Documentation

Overview

Package i18nmw provides transport middlewares for dealing with internationalisation.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ParseLocaleMiddleware = MiddlewareFunc(ParseLocaleHandler)

ParseLocaleMiddleware wraps the parse locale handler in a gorilla mux middleware.

Functions

func AppendLocaleToOutgoingContext

func AppendLocaleToOutgoingContext(ctx context.Context, locale string) context.Context

AppendLocaleToOutgoingContext will inject locale metadata to the outgoing context for use with clients.

func InterceptLocale

func InterceptLocale(ctx context.Context) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL