# README

acceptlang

acceptlang is an grpc interceptor which parses Accept-Language from metadata like HTTP header and set the AcceptLanguage to context.

Usage

import (
	"github.com/mercari/go-grpc-interceptor/acceptlang"
	"golang.org/x/net/context"
)

func main() {
	uIntOpt := grpc.UnaryInterceptor(acceptlang.UnaryServerInterceptor)
	sIntOpt := grpc.StreamInterceptor(acceptlang.StreamServerInterceptor)
	grpc.NewServer(uIntOpt, sIntOpt)
}

func foo(ctx context.Context) {
	acceptLangs := acceptlang.FromContext(ctx)
	fmt.printf("language :%s", acceptLangs[0].Language)
}

i18n with Accept-Language

Also support i18n integration with Accept-Language. github.com/nicksnyder/go-i18n is supported for now.

When you send accept language via metadata, i18n interceptor parses it and set i18n.TranslateFunc to context. Then use i18n.MustTFunc(ctx) for translactions.

import (
	"github.com/nicksnyder/go-i18n/v2/i18n"
	grpci18n "github.com/mercari/go-grpc-interceptor/acceptlang/i18n"
	"golang.org/x/net/context"
)

func main() {
	// load translation file
	bundle := i18n.NewBundle(language.English)
	bundle.MustLoadMessageFile("en.json")
	bundle.MustLoadMessageFile("ja.json")
	grpci18n.SetBundle(bundle)

	// set default language in case of no accept language specified
	// or no valid language found
	grpci18n.SetDefaultLanguage("en")

	// use i18 interceptor. Not explicitly required acceptlang interceptor
	uIntOpt := grpc.UnaryInterceptor(grpci18n.UnaryServerInterceptor)
	grpc.NewServer(uIntOpt)
}

func foo(ctx context.Context) {
	// get TranslateFunc from context
	localizer := grpci18n.MustLocalizer(ctx)
	fmt.printf("%s", localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "hello"}))
}

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

DefaultAcceptLangKey is metadata key name for accept language.

# Structs

No description provided by the author

# Type aliases

No description provided by the author