messageformat

package module
v0.0.0-...-8bed129 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: BSD-2-Clause Imports: 8 Imported by: 0

README

messageformat

The messageformat package is a Go implementation of ICU messages formatting.

Dependencies

The messageformat package depends on the makeplural package to compute named keys based on ICU rules.

Todo

  • More unit tests
  • More doc
  • FormatArray(data []interface{}) (string, error) ?

Documentation

Overview

Package messageformat implements ICU messages formatting for Go. see http://userguide.icu-project.org/formatparse/messages inspired by https://github.com/SlexAxton/messageformat.js

Index

Constants

View Source
const (
	EscapeChar = '\\'
	OpenChar   = '{'
	CloseChar  = '}'
	PartChar   = ','
	PoundChar  = '#'
	EqualChar  = '='
)

Variables

View Source
var Funcs = template.FuncMap{
	"mfPluralNode":         ParsePlural,
	"mfPluralNodeWithData": ParsePluralWithData,
}

Functions

func AddChild

func AddChild(parent, child Node)

Types

type Choice

type Choice struct {
	Key  string
	Node Node
}

type Complexity

type Complexity int
const (
	NoComplexity Complexity = iota
	SingleLiteral
	SingleRoad
	Complex
)

func (Complexity) IsComplex

func (c Complexity) IsComplex() bool

func (Complexity) IsSingleLiteral

func (c Complexity) IsSingleLiteral() bool

func (Complexity) IsSingleRoad

func (c Complexity) IsSingleRoad() bool

func (Complexity) String

func (i Complexity) String() string

type Container

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

func (*Container) Children

func (nd *Container) Children() []Node

func (*Container) Complexity

func (nd *Container) Complexity() Complexity

func (*Container) Culture

func (nd *Container) Culture() language.Tag

func (*Container) Data

func (nd *Container) Data() interface{}

func (*Container) Format

func (nd *Container) Format(mf *MessageFormat, output *bytes.Buffer, data Data, pound string) error

func (*Container) Parent

func (nd *Container) Parent() Node

func (*Container) Root

func (nd *Container) Root() Node

func (*Container) ToString

func (nd *Container) ToString(output *bytes.Buffer) (err error)

func (*Container) Type

func (nd *Container) Type() string

type Data

type Data map[string]interface{}

func (Data) ValueStr

func (data Data) ValueStr(key string) (string, error)

ValueStr retrieves a value from the given map and tries to return a string representation.

It will returns an error if the value's type is not <nil/string/int/float64>.

type Equal

type Equal struct {
	Key  float64
	Node Node
}

type Equals

type Equals []Equal

func (Equals) Len

func (s Equals) Len() int

func (Equals) Less

func (s Equals) Less(i, j int) bool

func (Equals) Swap

func (s Equals) Swap(i, j int)

type Literal

type Literal struct {
	Container
	Varname string
	Content []string
}

func (*Literal) Format

func (nd *Literal) Format(_ *MessageFormat, output *bytes.Buffer, data Data, pound string) (err error)

func (*Literal) ToString

func (nd *Literal) ToString(output *bytes.Buffer) (err error)

func (*Literal) Type

func (nd *Literal) Type() string

type MessageFormat

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

func (*MessageFormat) Format

func (x *MessageFormat) Format() (string, error)

func (*MessageFormat) FormatData

func (x *MessageFormat) FormatData(data Data) (string, error)

func (*MessageFormat) HasSelect

func (x *MessageFormat) HasSelect() bool

func (*MessageFormat) Root

func (x *MessageFormat) Root() Node

func (*MessageFormat) ToString

func (x *MessageFormat) ToString() (string, error)

type Node

type Node interface {
	Type() string
	ToString(output *bytes.Buffer) error
	Culture() language.Tag
	Complexity() Complexity
	Data() interface{}
	Root() Node
	Parent() Node
	Children() []Node
	Format(mf *MessageFormat, buf *bytes.Buffer, data Data, pound string) error
	// contains filtered or unexported methods
}

func ParsePlural

func ParsePlural(culture language.Tag, input string) (Node, error)

func ParsePluralWithData

func ParsePluralWithData(culture language.Tag, input string, data interface{}) (Node, error)

type NothingSkipper

type NothingSkipper struct{}

func (NothingSkipper) Skip

func (s NothingSkipper) Skip(k string) (skip bool, err error)

type Ordinal

type Ordinal struct {
	Select
}

func (*Ordinal) Format

func (nd *Ordinal) Format(mf *MessageFormat, output *bytes.Buffer, data Data, _ string) error

It will returns an error if : - the associated value can't be convert to string or to an int (i.e. bool, ...) - the pluralFunc is not defined (MessageFormat.getNamedKey)

It will falls back to the "other" choice if : - its key can't be found in the given map - the computed named key (MessageFormat.getNamedKey) is not a key of the given map

func (*Ordinal) Less

func (s *Ordinal) Less(i, j int) bool

sort choices

func (*Ordinal) ToString

func (nd *Ordinal) ToString(output *bytes.Buffer) error

func (*Ordinal) Type

func (nd *Ordinal) Type() string

type Parser

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

func New

func New() (*Parser, error)

func NewWithCulture

func NewWithCulture(culture language.Tag) (*Parser, error)

func (*Parser) Parse

func (x *Parser) Parse(input string, data interface{}) (*MessageFormat, error)

func (*Parser) SkipForPlural

func (x *Parser) SkipForPlural(skip bool)

SkipForPlural must be called before Parse.

type Plural

type Plural struct {
	Select
	EqualsMap map[float64]Node
	// contains filtered or unexported fields
}

func (*Plural) Equals

func (nd *Plural) Equals() Equals

func (*Plural) Format

func (nd *Plural) Format(mf *MessageFormat, output *bytes.Buffer, data Data, _ string) error

It will returns an error if : - the associated value can't be convert to string or to an int (i.e. bool, ...) - the pluralFunc is not defined (MessageFormat.getNamedKey)

It will falls back to the "other" choice if : - its key can't be found in the given map - the computed named key (MessageFormat.getNamedKey) is not a key of the given map

func (*Plural) Less

func (s *Plural) Less(i, j int) bool

sort choices

func (*Plural) Offset

func (nd *Plural) Offset() int

func (*Plural) ToString

func (nd *Plural) ToString(output *bytes.Buffer) error

func (*Plural) Type

func (nd *Plural) Type() string

type PluralSkipper

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

func NewPluralSkipper

func NewPluralSkipper(culture language.Tag, ordinal bool) (*PluralSkipper, error)

func (*PluralSkipper) Skip

func (s *PluralSkipper) Skip(k string) (skip bool, err error)

type Root

type Root struct {
	Container
	// contains filtered or unexported fields
}

func (*Root) Complexity

func (nd *Root) Complexity() Complexity

func (*Root) Culture

func (nd *Root) Culture() language.Tag

func (*Root) Data

func (nd *Root) Data() interface{}

func (*Root) Root

func (nd *Root) Root() Node

func (*Root) Type

func (nd *Root) Type() string

type Select

type Select struct {
	Container

	ChoicesMap map[string]Node
	// contains filtered or unexported fields
}

func (*Select) Choices

func (nd *Select) Choices() []Choice

func (*Select) ChoicesUnsorted

func (nd *Select) ChoicesUnsorted() []Choice

func (*Select) Format

func (nd *Select) Format(mf *MessageFormat, output *bytes.Buffer, data Data, _ string) error

It will falls back to the "other" choice if : - its key can't be found in the given map - its string representation is not a key of the given map

It will returns an error if : - the associated value can't be convert to string (i.e. bool, ...)

func (*Select) Len

func (s *Select) Len() int

sort choices

func (*Select) Less

func (s *Select) Less(i, j int) bool

sort choices

func (*Select) Other

func (nd *Select) Other() Node

func (*Select) Swap

func (s *Select) Swap(i, j int)

sort choices

func (*Select) ToString

func (nd *Select) ToString(output *bytes.Buffer) error

func (*Select) Type

func (nd *Select) Type() string

func (*Select) Varname

func (nd *Select) Varname() string

type SelectNode

type SelectNode interface {
	Node
	sort.Interface
	Varname() string
	Choices() []Choice
	ChoicesUnsorted() []Choice
	Other() Node
}

type SelectSkipper

type SelectSkipper interface {
	Skip(key string) (skip bool, err error)
}

type Var

type Var struct {
	Container
	Varname string
}

func (*Var) Format

func (nd *Var) Format(_ *MessageFormat, output *bytes.Buffer, data Data, value string) (err error)

func (*Var) ToString

func (nd *Var) ToString(output *bytes.Buffer) (err error)

func (*Var) Type

func (nd *Var) Type() string

Jump to

Keyboard shortcuts

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