Documentation
¶
Index ¶
- Constants
- Variables
- func Cardinal(lang language.Tag, number interface{}) (out string, err error)
- func FormatNamed(tag language.Tag, pattern string, args map[string]interface{}) (out string, err error)
- func FormatPositional(tag language.Tag, pattern string, args ...interface{}) (out string, err error)
- func FormatTemplateParseTree(tag language.Tag, pattern string) (tree *templateparse.Tree, err error)
- func IVWFT(number interface{}) (i, v, w, f, t int, err error)
- func IsEmptyParseTree(tree *templateparse.Tree) bool
- func Ordinal(lang language.Tag, number interface{}) (out string, err error)
- func TemplateRuntimeFunc(typ string, args ...interface{}) interface{}
- type Argument
- type DateArgNode
- type DatetimeArgNode
- type Node
- type NoneArgNode
- type PluralArgNode
- type PluralClause
- type PoundNode
- type SelectArgNode
- type SelectClause
- type TextNode
- type TimeArgNode
- type Token
- type TokenType
Examples ¶
Constants ¶
const TemplateRuntimeFuncName = "__messageformat__"
TemplateRuntimeFuncName is the name of the runtime helper function used in the output template.
Variables ¶
var ErrLeadingZeroNumber = errors.New("number must not have leading zero")
var ErrUnterminatedQuotedString = errors.New("unterminated quoted string")
Functions ¶
func FormatNamed ¶
func FormatNamed(tag language.Tag, pattern string, args map[string]interface{}) (out string, err error)
FormatNamed parses pattern and format to string with a map of args.
func FormatPositional ¶
func FormatPositional(tag language.Tag, pattern string, args ...interface{}) (out string, err error)
FormatPositional parses pattern and format to string with a slice of args.
Example ¶
numFiles := 1 out, err := FormatPositional( language.English, `{0, plural, =0 {There are no files on disk.} =1 {There is only 1 file on disk.} other {There are # files on disk.} }`, numFiles, ) if err != nil { panic(err) } fmt.Printf("%s\n", out)
Output: There is only 1 file on disk.
func FormatTemplateParseTree ¶
func FormatTemplateParseTree(tag language.Tag, pattern string) (tree *templateparse.Tree, err error)
FormatTemplateParseTree turns pattern into a text/template/parse.Tree. This is the recommended way to use messageformat with html/template where you can include HTML in your translation.
Example ¶
tree, err := FormatTemplateParseTree(language.English, `Hello there! Check <a href="{LINK}">this</a> out!`) if err != nil { panic(err) } template := htmltemplate.New("main") template.Funcs(htmltemplate.FuncMap{ TemplateRuntimeFuncName: TemplateRuntimeFunc, "makemap": func(pairs ...interface{}) map[string]interface{} { out := make(map[string]interface{}) for i := 0; i < len(pairs); i += 2 { key := pairs[i].(string) value := pairs[i+1] out[key] = value } return out }, }) _, err = template.Parse(`<!DOCTYPE html><html><head><title>Hi</title></head><body><p>{{ template "greeting" (makemap "LINK" .Link) }}</p></body></html>`) if err != nil { panic(err) } _, err = template.AddParseTree("greeting", tree) if err != nil { panic(err) } var buf strings.Builder err = template.Execute(&buf, map[string]interface{}{ "Link": "https://www.example.com", }) if err != nil { panic(err) } fmt.Printf("%s\n", buf.String())
Output: <!DOCTYPE html><html><head><title>Hi</title></head><body><p>Hello there! Check <a href="https://www.example.com">this</a> out!</p></body></html>
func IVWFT ¶
IVWFT derives i, v, w, f, t from number according to https://unicode.org/reports/tr35/tr35-numbers.html#Operands
func IsEmptyParseTree ¶
func IsEmptyParseTree(tree *templateparse.Tree) bool
func TemplateRuntimeFunc ¶
func TemplateRuntimeFunc(typ string, args ...interface{}) interface{}
TemplateRuntimeFunc is the runtime helper function used in the output template.
Types ¶
type DateArgNode ¶
DateArgNode is `{Argument, date, short | medium | long | full}`.
type DatetimeArgNode ¶
DatetimeArgNode is `{Argument, datetime, short | medium | long | full}`.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is an semantic item.
type PluralArgNode ¶
type PluralArgNode struct { Arg Argument // plural or selectordinal Kind string Offset int Clauses []PluralClause }
PluralArgNode is `{Argument, plural | selectordinal, [offset:number] PluralClause+}`.
type PluralClause ¶
type PluralClause struct { // If Keyword is empty, use ExplicitValue Keyword string ExplicitValue int Nodes []Node }
PluralClause is `(keyword | =ExplicitValue) {message}`.
type SelectArgNode ¶
type SelectArgNode struct { Arg Argument Clauses []SelectClause }
SelectArgNode is `{Argument, select, SelectClause+}`.
type SelectClause ¶
SelectClause is `Keyword {message}`.
type TimeArgNode ¶
TimeArgNode is `{Argument, time, short | medium | long | full}`.