package module
Version: v0.1.0 Latest Latest

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

Go to latest
Published: Jun 17, 2020 License: BSD-3-Clause Imports: 10 Imported by: 2



Build Status Coverage Status Go.Dev reference Go Report Card Release

This package is built on top of text/language and text/message.


Checkout example for details.

Integrate with other frameworks

It is easy to integrate with other frameworks by Handler or Middleware, and then retrieves translator in handler by GetTranslator.




This section is empty.


This section is empty.


func Handler

func Handler(ts *Translators, next http.Handler, parsers ...LanguageParser) http.Handler

Handler is a HTTP handler that store the matched translator in request context.

func Middleware

func Middleware(ts *Translators, parsers ...LanguageParser) func(http.Handler) http.Handler

Middleware is a HTTP middleware that store the matched translator in request context.


type CookieLanguageParser

type CookieLanguageParser struct {
	// contains filtered or unexported fields

CookieLanguageParser returns the language value from cookie.

func NewCookieLanguageParser

func NewCookieLanguageParser(name string) CookieLanguageParser

NewCookieLanguageParser returns a CookieLanguageParser.

func (CookieLanguageParser) Parse

Parse implements LanguageParser.Parse.

type FileDecoder

type FileDecoder interface {
	Decode([]byte, *Translation) error

FileDecoder is a function that decode file content to tranlation.

type FileStore

type FileStore struct {
	Directory string
	Decoder   FileDecoder

FileStore is a file store.

func NewFileStore

func NewFileStore(directory string, decoder FileDecoder) *FileStore

NewFileStore returns a file store with the given directory and decoder.

func (*FileStore) Get

func (s *FileStore) Get() (Translations, error)

Get implements Store.Get.

type HeaderLanguageParser

type HeaderLanguageParser struct {

HeaderLanguageParser returns the language value from request header.

func (HeaderLanguageParser) Parse

Parse implements LanguageParser.Parse.

type JSONFileDecoder

type JSONFileDecoder struct {

JSONFileDecoder is a JSON file decoder.

func (JSONFileDecoder) Decode

func (d JSONFileDecoder) Decode(data []byte, translation *Translation) error

Decode implements FileDecoder.Decode

type LanguageParser

type LanguageParser interface {
	Parse(*http.Request) string

LanguageParser is an interface that defined how to retrieve language from a request.

type Option

type Option func(ts *Translators)

Option for appling on translatiors.

func Fallback

func Fallback(fallback string) Option

Fallback is an option to change fallback language of translators.

type Store

type Store interface {
	Get() (Translations, error)

Store is where translations located.

type Translation

type Translation map[string]string

Translation is a key-value pair.

type Translations

type Translations map[string]Translation

Translations is a map that mapping from language to translations.

type Translator

type Translator struct {

Translator is a wrapper of message.Printer.

func GetTranslator

func GetTranslator(r *http.Request) *Translator

GetTranslator returns a translator from request context.

func NewTranslator

func NewTranslator(printer *message.Printer) *Translator

NewTranslator returns a new Translator.

type Translators

type Translators struct {
	// contains filtered or unexported fields

Translators is a collections of translator.

func New

func New(opts ...Option) *Translators

New returns a translators.

func (*Translators) Import

func (ts *Translators) Import(store Store) error

Import imports translations from the given store.

func (*Translators) MatchTranslator

func (ts *Translators) MatchTranslator(langs ...string) *Translator

MatchTranslator returns the matched translator of the given language.

type URLLanguageParser

type URLLanguageParser struct {
	// contains filtered or unexported fields

URLLanguageParser returns the language value from request URL.

func NewURLLanguageParser

func NewURLLanguageParser(name string) URLLanguageParser

NewURLLanguageParser returns a URLLanguageParser.

func (URLLanguageParser) Parse

func (p URLLanguageParser) Parse(r *http.Request) string

Parse implements LanguageParser.Parse.

Jump to

Keyboard shortcuts

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