localizer

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LangSpanish = "es"
	LangEnglish = "en"
)
View Source
const ContextKey string = "phx_localization"

context.Context key to localize localization token inside a request

Variables

This section is empty.

Functions

func CreateCookie

func CreateCookie(w http.ResponseWriter, localization string, cy core.Cypher) error

func ReadCookie

func ReadCookie(req *http.Request, cy core.Cypher) (string, error)

Types

type Localizer

type Localizer map[string]string

Localizer is a map of key and translations.

func (Localizer) Get

func (loc Localizer) Get(key string) string

Safely gets a localizer key. If the key is not defined in the localizer returns the key instead of returning the translation.

func (Localizer) GetFormatted added in v1.4.1

func (loc Localizer) GetFormatted(key string, args ...any) string

Safely gets a localizer key. If the key is not defined in the localizer returns the key instead of returning the translation. Then, it will format the string replacing a format specifier (using fmt.Sprintf)

type Store

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

A localizer store is a system to get a Localizer depending of a file name.

func NewLocalizerStore

func NewLocalizerStore(files embed.FS) Store

func (Store) Get

func (ls Store) Get(key, language string) Localizer

Gets a Localizer reading a json file identified by 'key' and then reads the language defined in that file.

func (Store) LoadIntoField

func (ls Store) LoadIntoField(field **Localizer, key string, language string)

type WebStore added in v1.1.3

type WebStore struct {
	Store
	// contains filtered or unexported fields
}

WebStore is a Localizer store with a defined SharedKey, ErrorKey and a core.Cypher.

  • SharedKey is the filename of shared translations. For example 'shared.json'. This file will be used as a fallback.
  • ErrorKey is the file of error translations. For example 'error.json'. This file will be used to tranlate errors when you call the function "GetLocalizerError"
  • core.Cypher is used to create and read localization HTTP cookie.

func NewWebLocalizerStore added in v1.1.3

func NewWebLocalizerStore(files embed.FS, sharedKey, errorKey string, cypher core.Cypher) WebStore

Creates a web localizer store. See WebStore documentation.

func (WebStore) CreateCookie added in v1.1.3

func (ws WebStore) CreateCookie(w http.ResponseWriter, localization string) error

Creates a localizer cookie

func (WebStore) Get added in v1.1.3

func (ws WebStore) Get(key, language string) Localizer

Gets a Localizer reading a json file identified by 'key' and then reads the language defined in that file. The localizer loaded will be merged with shared localizer.

func (WebStore) GetLocalizedError added in v1.1.3

func (ws WebStore) GetLocalizedError(err core.DomainError, req *http.Request) string

Localize a DomainError using the ErrorKey

func (WebStore) GetUsingRequest added in v1.1.3

func (ws WebStore) GetUsingRequest(key string, req *http.Request) Localizer

Get a Localizer using the language defined in HTTP cookie

func (WebStore) GetUsingRequestWithoutShared added in v1.1.3

func (ws WebStore) GetUsingRequestWithoutShared(key string, req *http.Request) Localizer

Get a Localizer using the language defined in HTTP cookie without SharedKey

func (WebStore) LoadIntoFieldUsingRequest added in v1.1.3

func (ws WebStore) LoadIntoFieldUsingRequest(field **Localizer, key string, req *http.Request)

func (WebStore) ReadCookie added in v1.1.3

func (ws WebStore) ReadCookie(req *http.Request) string

Reads a localizer cookie

Jump to

Keyboard shortcuts

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