httptranslate

package module
v0.0.0-...-7c92ad5 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

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

View Source
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

func GetLang(r *http.Request) (language.Tag, error)

Returns language tag from context value (provided by LangMiddleware()). If the value is missing returns non nil error.

func LangMiddleware

func LangMiddleware(next http.Handler) http.Handler

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

func LoadTexts[T any](path string, l map[language.Tag]*T, t T) error

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

func SetLangTags(l ...language.Tag)

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.

Jump to

Keyboard shortcuts

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