ErrNotFound indicates there was no message for the given key.
var ErrNotFound = errors.New("catalog: message not found")
A Builder allows building a Catalog programmatically.
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder(opts ...Option) *Builder
NewBuilder returns an empty mutable Catalog.
func (b *Builder) Context(tag language.Tag, r catmsg.Renderer) *Context
Context returns a Context for formatting messages. Only one Message may be formatted per context at any given time.
func (b *Builder) Languages() []language.Tag
Languages returns all languages for which the Catalog contains variants.
func (c *Builder) Matcher() language.Matcher
func (c *Builder) Set(tag language.Tag, key string, msg ...Message) error
Set sets the translation for the given language and key.
When evaluation this message, the first Message in the sequence to msgs to evaluate to a string will be the message returned.
func (c *Builder) SetMacro(tag language.Tag, name string, msg ...Message) error
SetMacro defines a Message that may be substituted in another message. The arguments to a macro Message are passed as arguments in the placeholder the form "${foo(arg1, arg2)}".
func (c *Builder) SetString(tag language.Tag, key string, msg string) error
SetString is shorthand for Set(tag, key, String(msg)).
A Catalog allows lookup of translated messages.
type Catalog interface { // Languages returns all languages for which the Catalog contains variants. Languages() []language.Tag // Matcher returns a Matcher for languages from this Catalog. Matcher() language.Matcher // A Context is used for evaluating Messages. Context(tag language.Tag, r catmsg.Renderer) *Context // contains filtered or unexported methods }
func NewFromMap(dictionaries map[string]Dictionary, opts ...Option) (Catalog, error)
NewFromMap creates a Catalog from the given map. If a Dictionary is underspecified the entry is retrieved from a parent language.
A Context is used for evaluating Messages. Only one Message may be formatted per context at any given time.
type Context struct {
// contains filtered or unexported fields
}
func (c *Context) Execute(key string) error
Execute looks up and executes the message with the given key. It returns ErrNotFound if no message could be found in the index.
A Dictionary is a source of translations for a single language.
type Dictionary interface { // Lookup returns a message compiled with catmsg.Compile for the given key. // It returns false for ok if such a message could not be found. Lookup(key string) (data string, ok bool) }
A Message holds a collection of translations for the same phrase that may vary based on the values of substitution arguments.
type Message = catmsg.Message
func String(name string) Message
String specifies a plain message string. It can be used as fallback if no other strings match or as a simple standalone message.
It is an error to pass more than one String in a message sequence.
func Var(name string, msg ...Message) Message
Var sets a variable that may be substituted in formatting patterns using named substitution of the form "${name}". The name argument is used as a fallback if the statements do not produce a match. The statement sequence may not contain any Var calls.
The name passed to a Var must be unique within message sequence.
An Option configures Catalog behavior.
type Option func(*options)
func Fallback(tag language.Tag) Option
Fallback specifies the default fallback language. The default is Und.