Documentation ¶
Overview ¶
Package i18n provides internationalization utilities.
Index ¶
- Constants
- func CleanAcceptLanguage(s string) (string, error)
- func ContextWithTranslator(c context.Context, t *Translator) context.Context
- func FmtNumber(l language.Tag, n interface{}) (s string)
- func Handle(h http.Handler, l Locales, fallback language.Tag, parsers ...Parser) http.Handler
- func HandleFunc(f http.HandlerFunc, l Locales, fallback language.Tag, parsers ...Parser) http.Handler
- func ParseAcceptLanguage(r *http.Request) []language.Tag
- func ParseCookie(r *http.Request) []language.Tag
- func ParseFormValue(r *http.Request) []language.Tag
- func RequestWithTranslator(r *http.Request, t *Translator) *http.Request
- func SetCookie(w http.ResponseWriter, r *http.Request)
- type Locales
- type Parser
- type TransFileSize
- type TransFloat64
- type TransInt
- type Translatable
- type Translations
- type Translator
- func (t *Translator) Locale() language.Tag
- func (t *Translator) T(key string, args ...interface{}) string
- func (t *Translator) THTML(key string, args ...interface{}) template.HTML
- func (t *Translator) Tn(key string, n int, args ...interface{}) (s string)
- func (t *Translator) TnHTML(key string, n int, args ...interface{}) template.HTML
Constants ¶
const (
ContextKeyTranslator contextKey = iota
)
Context keys
const LocaleFieldName = "locale"
LocaleFieldName defines the name used for the locale cookie or any parsable field name.
const TnPlaceholder = "{{.n}}"
TnPlaceholder is the placeholder replaced by n in a translation, when using the Tn function.
Variables ¶
This section is empty.
Functions ¶
func CleanAcceptLanguage ¶
CleanAcceptLanguage parses, cleans and returns the contents of a Accept-Language header. If an error is encountered, the returned string is the same as received.
func ContextWithTranslator ¶
func ContextWithTranslator(c context.Context, t *Translator) context.Context
ContextWithTranslator returns the context with the translator t set.
func Handle ¶
Handle returns a handler that will parse the language from the request thanks to the parsers, in the same order they are provided.
func HandleFunc ¶
func HandleFunc(f http.HandlerFunc, l Locales, fallback language.Tag, parsers ...Parser) http.Handler
HandleFunc returns a handler wrapping an http.HandlerFunc and works the same as the Handle function.
func ParseAcceptLanguage ¶
ParseAcceptLanguage parses the Accept-Language header.
func ParseCookie ¶
ParseCookie parses the LocaleFieldName cookie.
func ParseFormValue ¶
ParseFormValue parses the LocaleFieldName form value.
func RequestWithTranslator ¶
func RequestWithTranslator(r *http.Request, t *Translator) *http.Request
RequestWithTranslator returns the HTTP request with the translator t set.
func SetCookie ¶
func SetCookie(w http.ResponseWriter, r *http.Request)
SetCookie writes the response cookie when the parsed request locale differs from the one set inside the LocaleFieldName cookie. If the parsing has not been done yet (request translator is nil), this function can't help you so no cookie will be set.
Types ¶
type Locales ¶
type Locales map[language.Tag]Translations
Locales is a map of locales and their translations.
type Parser ¶
A Parser is a funcion that returns a list of accepted languages according to a request variable (header, URL...), most preferred first.
type TransFileSize ¶
type TransFileSize uint64
TransFileSize is a translatable argument of representing a file size.
type TransFloat64 ¶
type TransFloat64 float64
TransFloat64 is a translatable argument of type float64.
type Translatable ¶
Translatable defines a translatable content that can vary from a language to another.
type Translations ¶
Translations is a map of translations associated to keys.
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
A Translator contains the current locale with its translations and provides functions to get them.
func ContextTranslator ¶
func ContextTranslator(c context.Context) *Translator
ContextTranslator returns the translator used for the context.
func RequestTranslator ¶
func RequestTranslator(r *http.Request) *Translator
RequestTranslator returns the translator used for the HTTP request.
func (*Translator) Locale ¶
func (t *Translator) Locale() language.Tag
Locale returns the locale used be the translator.
func (*Translator) T ¶
func (t *Translator) T(key string, args ...interface{}) string
T returns the translation associated to key, for the client locale.
func (*Translator) THTML ¶
func (t *Translator) THTML(key string, args ...interface{}) template.HTML
THTML works like T but returns an HTML unescaped translation. An "nl2br" function is applied to the result.
func (*Translator) Tn ¶
func (t *Translator) Tn(key string, n int, args ...interface{}) (s string)
Tn returns the translation associated to key, for the client locale. If the translation defines plural forms (keys with a "Zero", "One" or "Other" suffix), it uses the most appropriate. All TnPlaceholder in the translation are replaced with number n. When translation is not found, an empty string is returned.