Documentation
¶
Overview ¶
httptranslate is a helper library to increase code reusability in other projects It provides simple but effective way to obtain, store and use language information provided by the user. It is a wrapper around excellent golang.org/x/text/language This library forces you to provide every translation in all specified languages.
Index ¶
Constants ¶
const HttpGenericError string = "Internal server error - please try again in a few minutes."
Generic http error - useful when there is error with translations and we do not know user language yet.
Variables ¶
This section is empty.
Functions ¶
func GetLang ¶
Returns language tag from context value (provided by LangMiddleware()). If the value is missing returns non nil error.
func LangMiddleware ¶
Http middleware that provides language as a context value for later http.Handler. You can get this value by calling GetLang() later. It determines user language in the following order: - cookie named lang exists, its value is parsed and used (parsing uses "golang.org/x/text/language" Matcher) - http header Accept-Language, its value is parsed and used (as above). Additionaly, a cookie named lang is set. - uses fallback language (derived from SetLangTags())
func LoadTexts ¶
Accepts path for lanuage jsons and map that will contain translation structs for every language tag. Additionally you have to provide empty translation struct to use it as a base for other languages Function reads from path/langTag.json, where langTag is string representation of language tag. If the json is missing, malformed or does not match your translation struct one-to-one an non-nil error is returned.
func SetLangTags ¶
Accept languages used in your application as language tags. You need to use it before any other function from this package is used. First language provided will be used as fallback if package cannot determine user language.
Types ¶
This section is empty.