i18n

package module
v1.0.2 Latest Latest
Warning

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

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

README

Internationalization Build Status Coverage Status Go Report Card GoDoc Release

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

Usage

Please take a look of the following example:

$ cd example
$ go run main.go
## fallback language(default to English)
$ curl "http://localhost:1234"
Home

## retrieve prefered language from URL query
$ curl "http://localhost:1234?lang=zh"
主页

$ curl "http://localhost:1234?lang=zh-TW"
主頁

$ curl "http://localhost:1234?lang=zh-HK"
主頁

## retrieve prefered language Cookie
$ curl -b "lang=zh-Hant" "http://localhost:1234"
主頁

## retrieve prefered language from header
$ curl -H "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,zh-TW;q=0.6,pt;q=0.5" "http://localhost:1234/hello?name=foo"
你好,foo

Integrate with other frameworks

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

Types

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 {
	*message.Printer
}

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 {
	*catalog.Builder
	// 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.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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