lang

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2018 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package language contains a handler that provides support for handling requests in specific languages.

Index

Constants

This section is empty.

Variables

View Source
var ContextKey contextKey = "i18n-data"

ContextKey is the key under which the the language list and current language will be be stored in the request context.

View Source
var SessionKey string = "language"

SessionKey is the key under which the current session will be stored in the session.

Functions

func I18N

func I18N(h http.Handler, opts ...Option) http.Handler

I18N returns a handler that deals with detecting and setting a language for use in handler h. If there is only one supported language, the handler only sets the ContextValue in the request before calling the handler h. The current language is set in the url, redirecting to it first if it is not there yet. It also strips the language from the request path so that other handlers in the chain won't see a url they won't expect.

The handler first checks whether the request url already contains any of the supported languages. If one matches, but ends with the language code and without a terminating slash, a redirect is sent. Example:

'/en' -> '/en/'. Or if a UrlPRefix is set: '/prefix/en' -> '/prefix/en/'

If the path contains the language and additional path data, that language is stored as the current language in the request context. It is also stored in the session, if such an interface is provided.

If the url contains no language code, several methods are attempted to decide what the language should be. If a session interface is provided, it is checked first for a stored language. If none is found, the Accept-Language header is checked for a suitable choice. It then tries the LANG and LC_MESSAGES environment variables. If no language has been selected, or the selected one isn't supported, the first language in the supported slice is used. With a valid language, a redirect is created with the language code added to the url.

By default, error messages will not be printed out.

func URL

func URL(url, prefix string, data ContextValue) string

URL prefixes a url string with the request language.

Types

type ContextValue

type ContextValue struct {
	// Languages contains all supported languages.
	Languages []xlang.Tag
	// Current is the currently required language.
	Current xlang.Tag
}

ContextValue is stored in the request context

func Data

func Data(r *http.Request) ContextValue

Data returns the language data stored in the request.

type Option

type Option struct {
	// contains filtered or unexported fields
}

An Option is used to change the default behaviour of the language handlers.

func Languages

func Languages(tags []xlang.Tag) Option

Languages provides the handler with supported languages.

func Logger

func Logger(l handler.Logger) Option

Logger is used to print out any error messages. If none is provided, no error message will be printed.

func Session

func Session(s handler.Session) Option

Session will be used to set the current language.

func URLPrefix

func URLPrefix(p string) Option

URLPrefix is the prefix that may be used in the url, before the language code itself. For example, if the prefix is '/web', the final url will be '/web/en'.

Jump to

Keyboard shortcuts

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