internal

package
v12.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// VarsKey is the key for the message's variables, per locale(global) or per key (local).
	VarsKey = "Vars"
	// PluralCountKey is the key for the template's message pluralization.
	PluralCountKey = "PluralCount"
	// VarCountKeySuffix is the key suffix for the template's variable's pluralization,
	// e.g. HousesCount for ${Houses}.
	VarCountKeySuffix = "Count"
	// VarsKeySuffix is the key which the template message's variables
	// are stored with,
	// e.g. welcome.human.other_vars
	VarsKeySuffix = "_vars"
)

Variables

View Source
var DefaultPluralFormDecoder = func(_ context.Locale, key string) (PluralForm, bool) {
	if isDefaultPluralForm(key) {
		return pluralForm(key), true
	}

	return nil, false
}

DefaultPluralFormDecoder is the default `PluralFormDecoder`. Supprots "zero", "one", "two", "other", "=x", "<x", ">x".

Functions

This section is empty.

Types

type Catalog

type Catalog struct {
	Locales []*Locale
	// contains filtered or unexported fields
}

Catalog holds the locales and the variables message storage.

func NewCatalog

func NewCatalog(languages []language.Tag, opts Options) (*Catalog, error)

NewCatalog returns a new Catalog based on the registered languages and the loader options.

func (*Catalog) GetLocale

func (c *Catalog) GetLocale(index int) context.Locale

GetLocale returns a valid `Locale` based on the "index".

func (*Catalog) Set

func (c *Catalog) Set(tag language.Tag, key string, msgs ...catalog.Message) error

Set sets a simple translation message.

func (*Catalog) SetDefault

func (c *Catalog) SetDefault(index int) bool

SetDefault changes the default language based on the "index". See `I18n#SetDefault` method for more.

func (*Catalog) Store

func (c *Catalog) Store(langIndex int, kv Map) error

Store stores the a map of values to the locale derives from the given "langIndex".

type Locale

type Locale struct {

	// ID is the tag.String().
	ID string
	// Options given by the Catalog
	Options Options

	// Fields set by Catalog.
	FuncMap template.FuncMap
	Printer *message.Printer

	// Fields set by this Load method.
	Messages map[string]Renderer
	Vars     []Var // shared per-locale variables.
	// contains filtered or unexported fields
}

Locale is the default Locale. Created by Catalog. One Locale maps to one registered and loaded language. Stores the translation variables and most importantly, the Messages (keys and their renderers).

func (*Locale) GetMessage

func (loc *Locale) GetMessage(key string, args ...interface{}) string

GetMessage should return translated text based on the given "key".

func (*Locale) Index

func (loc *Locale) Index() int

Index returns the current locale index from the languages list.

func (*Locale) Language

func (loc *Locale) Language() string

Language should return the exact languagecode of this `Locale` that the user provided on `New` function.

Same as `Tag().String()` but it's static.

func (*Locale) Load

func (loc *Locale) Load(c *Catalog, keyValues Map) error

Load sets the translation messages based on the Catalog's key values.

func (*Locale) Tag

func (loc *Locale) Tag() *language.Tag

Tag returns the full language Tag attached to this Locale, it should be unique across different Locales.

type Map

type Map = map[string]interface{}

Map is just an alias of the map[string]interface{} type. Just like the iris.Map one.

type Message

type Message struct {
	Locale *Locale

	Key   string
	Value string

	Plural  bool
	Plurals []*PluralMessage // plural forms by order.

	Vars []Var
}

Message is the default Renderer for translation messages. Holds the variables and the plurals of this key. Each Locale has its own list of messages.

func (*Message) AddPlural

func (m *Message) AddPlural(form PluralForm, r Renderer)

AddPlural adds a plural message to the Plurals list.

func (*Message) Render

func (m *Message) Render(args ...interface{}) (string, error)

Render completes the Renderer interface. It accepts arguments, which can resolve the pluralization type of the message and its variables. If the Message is wrapped by a Template then the first argument should be a map. The map key resolves to the pluralization of the message is the "PluralCount". And for variables the user should set a message key which looks like: %VAR_NAME%Count, e.g. "DogsCount" to set plural count for the "Dogs" variable, case-sensitive.

type MessageFunc

type MessageFunc func(langInput, langMatched, key string, args ...interface{}) string

MessageFunc is the function type to modify the behavior when a key or language was not found. All language inputs fallback to the default locale if not matched. This is why this signature accepts both input and matched languages, so caller can provide better messages.

The first parameter is set to the client real input of the language, the second one is set to the matched language (default one if input wasn't matched) and the third and forth are the translation format/key and its optional arguments.

Note: we don't accept the Context here because Tr method and template func {{ tr }} have no direct access to it.

type Options

type Options struct {
	// Left delimiter for template messages.
	Left string
	// Right delimeter for template messages.
	Right string
	// Enable strict mode.
	Strict bool
	// Optional functions for template messages per locale.
	Funcs func(context.Locale) template.FuncMap
	// Optional function to be called when no message was found.
	DefaultMessageFunc MessageFunc
	// Customize the overall behavior of the plurazation feature.
	PluralFormDecoder PluralFormDecoder
}

The Options of the Catalog and its Locales.

type PluralCounter

type PluralCounter interface {
	// PluralCount returns the plural count of the message.
	// If returns -1 then this is not a valid plural message.
	PluralCount() int
	// VarCount should return the variable count, based on the variable name.
	VarCount(name string) int
}

PluralCounter if completes by an input argument of a message to render, then the plural renderer will resolve the plural count and any variables' counts. This is useful when the data is not a type of Map or integers.

type PluralForm

type PluralForm interface {
	String() string
	// the string is a verified plural case's raw string value.
	// Field for priority on which order to register the plural cases.
	Less(next PluralForm) bool
	MatchPlural(pluralCount int) bool
}

A PluralForm is responsible to decode locale keys to plural forms and match plural forms based on the given pluralCount.

See `pluralForm` package-level type for a default implementation.

type PluralFormDecoder

type PluralFormDecoder func(loc context.Locale, key string) (PluralForm, bool)

A PluralFormDecoder should report and return whether a specific "key" is a plural one. This function can be implemented and set on the `Options` to customize the plural forms and their behavior in general.

See the `DefaultPluralFormDecoder` package-level variable for the default implementation one.

type PluralMessage

type PluralMessage struct {
	Form     PluralForm
	Renderer Renderer
}

PluralMessage holds the registered Form and the corresponding Renderer. It is used on the `Message.AddPlural` method.

type Renderer

type Renderer interface {
	Render(args ...interface{}) (string, error)
}

Renderer is responsible to render a translation based on the given "args".

type Template

type Template struct {
	*Message
	// contains filtered or unexported fields
}

Template is a Renderer which renders template messages.

func NewTemplate

func NewTemplate(c *Catalog, m *Message) (*Template, error)

NewTemplate returns a new Template message based on the catalog and the base translation Message. See `Locale.Load` method.

func (*Template) Render

func (t *Template) Render(args ...interface{}) (string, error)

Render completes the Renderer interface. It renders a template message. Each key has its own Template, plurals too.

type Var

type Var struct {
	Name    string        // Variable name, e.g. Name
	Literal string        // Its literal is ${Name}
	Cases   []interface{} // one:...,few:...,...
	Format  string        // defaults to "%d".
	Argth   int           // 1, 2, 3...
}

Var represents a message variable. The variables, like the sub messages are sorted. First: plurals (which again, are sorted) and then any custom keys. In variables, the sorting depends on the exact order the associated message uses the variables. This is extremely handy. This package requires the golang.org/x/text/message capabilities only for the variables feature, the message itself's pluralization is managed by the package.

Jump to

Keyboard shortcuts

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