Package pipeline provides tools for creating translation pipelines.




This section is empty.


This section is empty.


func Generate

func Generate(w io.Writer, pkg string, extracted *Locale, trans ...*Locale) (n int, err error)

Generate writes a Go file with the given package name to w, which defines a Catalog with translated messages.

func Rewrite

func Rewrite(w io.Writer, goPackage string) error

Rewrite rewrites the Go files in a single package to use the localization machinery and rewrites strings to adopt best practices when possible. If w is not nil the generated files are written to it, each files with a "--- <filename>" header. Otherwise the files are overwritten.


type Config

type Config struct {
	SourceLanguage language.Tag

	// Supported indicates the languages for which data should be generated.
	// If unspecified, it will attempt to derive the set of supported languages
	// from the context.
	Supported []language.Tag

	Packages []string

Config contains configuration for the translation pipeline.

type Feature

type Feature struct {
	Type string `json:"type"` // Right now this is only gender and plural.


Feature holds information about a feature that can be implemented by an Argument.

type IDList

type IDList []string

IDList is a set identifiers that each may refer to possibly different versions of the same message. When looking up a messages, the first identifier in the list takes precedence.

func (*IDList) MarshalJSON

func (id *IDList) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*IDList) UnmarshalJSON

func (id *IDList) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Locale

type Locale struct {
	Language language.Tag    `json:"language"`
	Messages []Message       `json:"messages"`
	Macros   map[string]Text `json:"macros,omitempty"`

A Locale is used to store all information for a single locale. This type is used both for extraction and injection.

func Extract

func Extract(c *Config) (*Locale, error)

Extract extracts all strings form the package defined in Config.

type Message

type Message struct {
	// ID contains a list of identifiers for the message.
	ID IDList `json:"id"`
	// Key is the string that is used to look up the message at runtime.
	Key         string `json:"key"`
	Meaning     string `json:"meaning,omitempty"`
	Message     Text   `json:"message"`
	Translation Text   `json:"translation"`

	Comment           string `json:"comment,omitempty"`
	TranslatorComment string `json:"translatorComment,omitempty"`

	Placeholders []Placeholder `json:"placeholders,omitempty"`

	// Extraction information.
	Position string `json:"position,omitempty"` // filePosition:line

A Message describes a message to be translated.

func (*Message) Placeholder

func (m *Message) Placeholder(id string) *Placeholder

Placeholder reports the placeholder for the given ID if it is defined or nil otherwise.

func (*Message) Substitute

func (m *Message) Substitute(msg string) (sub string, err error)

Substitute replaces placeholders in msg with their original value.

type Placeholder

type Placeholder struct {
	// ID is the placeholder identifier without the curly braces.
	ID string `json:"id"`

	// String is the string with which to replace the placeholder. This may be a
	// formatting string (for instance "%d" or "{{.Count}}") or a literal string
	// (<div>).
	String string `json:"string"`

	Type           string `json:"type"`
	UnderlyingType string `json:"underlyingType"`
	// ArgNum and Expr are set if the placeholder is a substitution of an
	// argument.
	ArgNum int    `json:"argNum,omitempty"`
	Expr   string `json:"expr,omitempty"`

	Comment string `json:"comment,omitempty"`
	Example string `json:"example,omitempty"`

	// Features contains the features that are available for the implementation
	// of this argument.
	Features []Feature `json:"features,omitempty"`

A Placeholder is a part of the message that should not be changed by a translator. It can be used to hide or prettify format strings (e.g. %d or {{.Count}}), hide HTML, or mark common names that should not be translated.

type Select

type Select struct {
	Feature string          `json:"feature"` // Name of Feature type (e.g plural)
	Arg     string          `json:"arg"`     // The placeholder ID
	Cases   map[string]Text `json:"cases"`

Select selects a Text based on the feature value associated with a feature of a certain argument.

type Text

type Text struct {
	// Msg and Select contains the message to be displayed. Msg may be used as
	// a fallback value if none of the select cases match.
	Msg    string  `json:"msg,omitempty"`
	Select *Select `json:"select,omitempty"`

	// Var defines a map of variables that may be substituted in the selected
	// message.
	Var map[string]Text `json:"var,omitempty"`

	// Example contains an example message formatted with default values.
	Example string `json:"example,omitempty"`

Text defines a message to be displayed.

func (*Text) IsEmpty

func (t *Text) IsEmpty() bool

IsEmpty reports whether this Text can generate anything.

func (*Text) MarshalJSON

func (t *Text) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Text) UnmarshalJSON

func (t *Text) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.