var ( // ErrIncomplete indicates a compiled message does not define translations // for all possible argument values. If this message is returned, evaluating // a message may result in the ErrNoMatch error. ErrIncomplete = errors.New("catmsg: incomplete message; may not give result for all inputs") // ErrNoMatch indicates no translation message matched the given input // parameters when evaluating a message. ErrNoMatch = errors.New("catmsg: no translation for inputs") )
func Compile(tag language.Tag, macros Dictionary, m Message) (data string, err error)
Compile converts a Message to a data string that can be stored in a Catalog. The resulting string can subsequently be decoded by passing to the Execute method of a Decoder.
Affix is a message that adds a prefix and suffix to another message. This is mostly used add back whitespace to a translation that was stripped before sending it out.
type Affix struct { Message Message Prefix string Suffix string }
func (a Affix) Compile(e *Encoder) (err error)
Compile implements Message.
A Decoder deserializes and evaluates messages that are encoded by an encoder.
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder(tag language.Tag, r Renderer, macros Dictionary) *Decoder
NewDecoder returns a new Decoder.
Decoders are designed to be reused for multiple invocations of Execute. Only one goroutine may call Execute concurrently.
func (d *Decoder) Arg(i int) interface{}
Arg implements Renderer.
During evaluation of macros, the argument positions may be mapped to arguments that differ from the original call.
func (d *Decoder) DecodeString() string
DecodeString decodes a string that was encoded with EncodeString and advances the position.
func (d *Decoder) DecodeUint() uint64
DecodeUint decodes a number that was encoded with EncodeUint and advances the position.
func (d *Decoder) Done() bool
Done reports whether there are more bytes to process in this message.
func (d *Decoder) Execute(msg string) error
Execute decodes and evaluates msg.
Only one goroutine may call execute.
func (d *Decoder) ExecuteMessage() bool
ExecuteMessage decodes and executes the message at the current position.
func (d *Decoder) ExecuteSubstitution()
ExecuteSubstitution executes the message corresponding to the substitution as encoded by EncodeSubstitution.
func (d *Decoder) Language() language.Tag
Language returns the language in which the message is being rendered.
The destination language may be a child language of the language used for encoding. For instance, a decoding language of "pt-PT"" is consistent with an encoding language of "pt".
func (d *Decoder) Render(s string)
Render implements Renderer.
func (d *Decoder) SkipMessage()
SkipMessage skips the message at the current location and advances the position.
A Dictionary specifies a source of messages, including variables or macros.
type Dictionary interface { // Lookup returns the message for the given key. It returns false for ok if // such a message could not be found. Lookup(key string) (data string, ok bool) }
An Encoder serializes a Message to a string.
type Encoder struct {
// contains filtered or unexported fields
}
func (e *Encoder) EncodeMessage(m Message) error
EncodeMessage serializes the given message inline at the current position.
func (e *Encoder) EncodeMessageType(h Handle)
EncodeMessageType marks the current message to be of type h.
It must be the first call of a Message's Compile method.
func (e *Encoder) EncodeString(s string)
EncodeString encodes s.
func (e *Encoder) EncodeSubstitution(name string, arguments ...int)
EncodeSubstitution inserts a resolved reference to a variable or macro.
This call must be matched with a call to ExecuteSubstitution at decoding time.
func (e *Encoder) EncodeUint(x uint64)
EncodeUint encodes x.
func (e *Encoder) Language() language.Tag
Language reports the language for which the encoded message will be stored in the Catalog.
FirstOf is a message type that prints the first message in the sequence that resolves to a match for the given substitution arguments.
type FirstOf []Message
func (s FirstOf) Compile(e *Encoder) error
Compile implements Message.
A Handle refers to a registered message type.
type Handle int
func Register(name string, handler Handler) Handle
Register records the existence of a message type and returns a Handle that can be used in the Encoder's EncodeMessageType method to create such messages. The prefix of the name should be the package path followed by an optional disambiguating string. Register will panic if a handle for the same name was already registered.
A Handler decodes and evaluates data compiled by a Message and sends the result to the Decoder. The output may depend on the value of the substitution arguments, accessible by the Decoder's Arg method. The Handler returns false if there is no translation for the given substitution arguments.
type Handler func(d *Decoder) bool
A Message holds a collection of translations for the same phrase that may vary based on the values of substitution arguments.
type Message interface { // Compile encodes the format string(s) of the message as a string for later // evaluation. // // The first call Compile makes on the encoder must be EncodeMessageType. // The handle passed to this call may either be a handle returned by // Register to encode a single custom message, or HandleFirst followed by // a sequence of calls to EncodeMessage. // // Compile must return ErrIncomplete if it is possible for evaluation to // not match any translation for a given set of formatting parameters. // For example, selecting a translation based on plural form may not yield // a match if the form "Other" is not one of the selectors. // // Compile may return any other application-specific error. For backwards // compatibility with package like fmt, which often do not do sanity // checking of format strings ahead of time, Compile should still make an // effort to have some sensible fallback in case of an error. Compile(e *Encoder) error }
Raw is a message consisting of a single format string that is passed as is to the Renderer.
Note that a Renderer may still do its own variable substitution.
type Raw string
func (r Raw) Compile(e *Encoder) (err error)
Compile implements Message.
A Renderer renders a Message.
type Renderer interface { // Render renders the given string. The given string may be interpreted as a // format string, such as the one used by the fmt package or a template. Render(s string) // Arg returns the i-th argument passed to format a message. This method // should return nil if there is no such argument. Messages need access to // arguments to allow selecting a message based on linguistic features of // those arguments. Arg(i int) interface{} }
String is a message consisting of a single format string which contains placeholders that may be substituted with variables.
Variable substitutions are marked with placeholders and a variable name of the form ${name}. Any other substitutions such as Go templates or printf-style substitutions are left to be done by the Renderer.
When evaluation a string interpolation, a Renderer will receive separate calls for each placeholder and interstitial string. For example, for the message: "%[1]v ${invites} %[2]v to ${their} party." The sequence of calls is:
d.Render("%[1]v ") d.Arg(1) d.Render(resultOfInvites) d.Render(" %[2]v to ") d.Arg(2) d.Render(resultOfTheir) d.Render(" party.")
where the messages for "invites" and "their" both use a plural.Select referring to the first argument.
Strings may also invoke macros. Macros are essentially variables that can be reused. Macros may, for instance, be used to make selections between different conjugations of a verb. See the catalog package description for an overview of macros.
type String string
func (s String) Compile(e *Encoder) (err error)
Compile implements Message. It parses the placeholder formats and returns any error.
Var defines a message that can be substituted for a placeholder of the same name. If an expression does not result in a string after evaluation, Name is used as the substitution. For example:
Var{ Name: "minutes", Message: plural.Select(1, "one", "minute"), }
will resolve to minute for singular and minutes for plural forms.
type Var struct { Name string Message Message }
func (v *Var) Compile(e *Encoder) error
Compile implements Message.
Note that this method merely registers a variable; it does not create an encoded message.