Documentation
¶
Overview ¶
Package language contains a handler that provides support for handling requests in specific languages.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
var SessionKey string = "language"
SessionKey is the key under which the current session will be stored in the session.
Functions ¶
func I18N ¶
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 Logger ¶
Logger is used to print out any error messages. If none is provided, no error message will be printed.