i18n

package
v0.0.0-...-41f935c Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FallbackLocale = "en-GB"
	ErrorLocale    = FallbackLocale
)
View Source
var (
	DefaultHTMLRuntime     HTMLRuntime
	DefaultJSRuntime       JSRuntime
	DefaultMarkdownRuntime MarkdownRuntime
)
View Source
var ErrNotFound = errors.New("message not found")

Functions

func ClosestLocale

func ClosestLocale(candidates []string) (string, bool)

func LoadJSON

func LoadJSON(locale string, r io.Reader) error

func LoadJSONFiles

func LoadJSONFiles(locales fs.FS) error

func SprintNode

func SprintNode(node Node) string

Types

type AfterPostProcessFunc

type AfterPostProcessFunc func(res string) string

type BinaryNode

type BinaryNode struct {
	Op  Token
	LHS Node
	RHS Node
	// contains filtered or unexported fields
}

type Bool

type Bool struct {
	Value bool
}

func NewBool

func NewBool(b bool) Bool

func (Bool) Add

func (b Bool) Add(rhs Value) Value

func (Bool) AsBool

func (b Bool) AsBool() Bool

func (Bool) AsFloat

func (b Bool) AsFloat() Float

func (Bool) AsInt

func (b Bool) AsInt() Int

func (Bool) AsSlice

func (b Bool) AsSlice() Slice

func (Bool) AsString

func (b Bool) AsString() String

func (Bool) Div

func (b Bool) Div(rhs Value) Value

func (Bool) Equal

func (b Bool) Equal(rhs Value) Bool

func (Bool) Less

func (b Bool) Less(rhs Value) Bool

func (Bool) Mod

func (b Bool) Mod(rhs Value) Value

func (Bool) Mul

func (b Bool) Mul(rhs Value) Value

func (Bool) Sub

func (b Bool) Sub(rhs Value) Value

func (Bool) Type

func (b Bool) Type() Type

type CallNode

type CallNode struct {
	LHS  Node
	Args []Node
	// contains filtered or unexported fields
}

type Duration

type Duration struct {
	Value time.Duration
}

func NewDuration

func NewDuration(d time.Duration) Duration

func (Duration) Add

func (d Duration) Add(rhs Value) Value

func (Duration) AsBool

func (d Duration) AsBool() Bool

func (Duration) AsFloat

func (d Duration) AsFloat() Float

func (Duration) AsInt

func (d Duration) AsInt() Int

func (Duration) AsSlice

func (d Duration) AsSlice() Slice

func (Duration) AsString

func (d Duration) AsString() String

func (Duration) Div

func (d Duration) Div(rhs Value) Value

func (Duration) Equal

func (d Duration) Equal(rhs Value) Bool

func (Duration) Less

func (d Duration) Less(rhs Value) Bool

func (Duration) Mod

func (d Duration) Mod(rhs Value) Value

func (Duration) Mul

func (d Duration) Mul(rhs Value) Value

func (Duration) Sub

func (d Duration) Sub(rhs Value) Value

func (Duration) Type

func (d Duration) Type() Type

type Error

type Error struct {
	Token   Token
	Message string
}

type Errors

type Errors []Error

func (Errors) Error

func (e Errors) Error() string

type Float

type Float struct {
	Value float64
}

func NewFloat

func NewFloat(f float64) Float

func (Float) Add

func (f Float) Add(rhs Value) Value

func (Float) AsBool

func (f Float) AsBool() Bool

func (Float) AsFloat

func (f Float) AsFloat() Float

func (Float) AsInt

func (f Float) AsInt() Int

func (Float) AsSlice

func (f Float) AsSlice() Slice

func (Float) AsString

func (f Float) AsString() String

func (Float) Div

func (f Float) Div(rhs Value) Value

func (Float) Equal

func (f Float) Equal(rhs Value) Bool

func (Float) Less

func (f Float) Less(rhs Value) Bool

func (Float) Mod

func (f Float) Mod(rhs Value) Value

func (Float) Mul

func (f Float) Mul(rhs Value) Value

func (Float) Sub

func (f Float) Sub(rhs Value) Value

func (Float) Type

func (f Float) Type() Type

type HTMLRuntime

type HTMLRuntime struct {
	MarkdownRuntime
}

func (HTMLRuntime) Bold

func (r HTMLRuntime) Bold(value Value) RawString

func (HTMLRuntime) Italic

func (r HTMLRuntime) Italic(value Value) RawString

func (HTMLRuntime) Kind

func (r HTMLRuntime) Kind() string
func (r HTMLRuntime) Link(label, href, target Value) RawString

func (HTMLRuntime) PostProcess

func (r HTMLRuntime) PostProcess(value Value, after AfterPostProcessFunc) any

type IdentNode

type IdentNode struct {
	Name Token
	// contains filtered or unexported fields
}

type IndexNode

type IndexNode struct {
	LHS  Node
	Expr Node
	// contains filtered or unexported fields
}

type Int

type Int struct {
	Value int64
}

func NewInt

func NewInt(i int64) Int

func (Int) Add

func (i Int) Add(rhs Value) Value

func (Int) AsBool

func (i Int) AsBool() Bool

func (Int) AsFloat

func (i Int) AsFloat() Float

func (Int) AsInt

func (i Int) AsInt() Int

func (Int) AsSlice

func (i Int) AsSlice() Slice

func (Int) AsString

func (i Int) AsString() String

func (Int) Div

func (i Int) Div(rhs Value) Value

func (Int) Equal

func (i Int) Equal(rhs Value) Bool

func (Int) Less

func (i Int) Less(rhs Value) Bool

func (Int) Mod

func (i Int) Mod(rhs Value) Value

func (Int) Mul

func (i Int) Mul(rhs Value) Value

func (Int) Sub

func (i Int) Sub(rhs Value) Value

func (Int) Type

func (i Int) Type() Type

type InvalidNode

type InvalidNode struct {
	Start Token
	End   Token
	// contains filtered or unexported fields
}

type JSRuntime

type JSRuntime struct {
	MarkdownRuntime
}

func (JSRuntime) Bold

func (r JSRuntime) Bold(value Value) RawString

func (JSRuntime) Italic

func (r JSRuntime) Italic(value Value) RawString

func (JSRuntime) Kind

func (r JSRuntime) Kind() string
func (r JSRuntime) Link(label, href, target Value) RawString

func (JSRuntime) PostProcess

func (r JSRuntime) PostProcess(value Value, after AfterPostProcessFunc) any

type Lexer

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

func NewLexer

func NewLexer() *Lexer

func (*Lexer) Consume

func (l *Lexer) Consume() (Token, error)

func (*Lexer) Load

func (l *Lexer) Load(scanner io.ByteScanner)

type LiteralNode

type LiteralNode struct {
	Token
	// contains filtered or unexported fields
}

type MarkdownRuntime

type MarkdownRuntime struct{}

func (MarkdownRuntime) Bold

func (r MarkdownRuntime) Bold(value Value) RawString

func (MarkdownRuntime) Fraction

func (r MarkdownRuntime) Fraction(value, roundingUnit Value) Float

func (MarkdownRuntime) Integer

func (r MarkdownRuntime) Integer(value Value) Int

func (MarkdownRuntime) Italic

func (r MarkdownRuntime) Italic(value Value) RawString

func (MarkdownRuntime) Join

func (r MarkdownRuntime) Join(s, sep Value) String

func (MarkdownRuntime) Kind

func (r MarkdownRuntime) Kind() string
func (r MarkdownRuntime) Link(label, href, target Value) RawString

func (MarkdownRuntime) PadLeft

func (r MarkdownRuntime) PadLeft(value, length, padding Value) String

func (MarkdownRuntime) PadRight

func (r MarkdownRuntime) PadRight(value, length, padding Value) String

func (MarkdownRuntime) PostProcess

func (r MarkdownRuntime) PostProcess(value Value, after AfterPostProcessFunc) any

func (MarkdownRuntime) Split

func (r MarkdownRuntime) Split(s, sep Value) Slice

func (MarkdownRuntime) T

func (r MarkdownRuntime) T(key Value, locale string, value, opt Value) String

func (MarkdownRuntime) TrimLeft

func (r MarkdownRuntime) TrimLeft(value, trim Value) String

func (MarkdownRuntime) TrimRight

func (r MarkdownRuntime) TrimRight(value, trim Value) String

type Message

type Message struct {
	Key  string
	Vars Vars
}

func M

func M(id string, pairs ...any) Message

func (Message) Error

func (m Message) Error() string

func (Message) Is

func (m Message) Is(target error) bool

func (Message) String

func (m Message) String() string

func (Message) WithContext

func (m Message) WithContext(context string) Message

type Node

type Node interface {
	// contains filtered or unexported methods
}

type OptNode

type OptNode struct {
	Match  Node
	Result Node
	// contains filtered or unexported fields
}

type Parser

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

func NewParser

func NewParser() *Parser

func (*Parser) Parse

func (p *Parser) Parse(src io.ByteScanner) (*RootNode, error)

type RawString

type RawString struct {
	Value string
}

func NewRawString

func NewRawString(s string) RawString

func (RawString) Add

func (s RawString) Add(rhs Value) Value

func (RawString) AsBool

func (s RawString) AsBool() Bool

func (RawString) AsFloat

func (s RawString) AsFloat() Float

func (RawString) AsInt

func (s RawString) AsInt() Int

func (RawString) AsSlice

func (s RawString) AsSlice() Slice

func (RawString) AsString

func (s RawString) AsString() String

func (RawString) Div

func (s RawString) Div(rhs Value) Value

func (RawString) Equal

func (s RawString) Equal(rhs Value) Bool

func (RawString) Less

func (s RawString) Less(rhs Value) Bool

func (RawString) Mod

func (s RawString) Mod(rhs Value) Value

func (RawString) Mul

func (s RawString) Mul(rhs Value) Value

func (RawString) Sub

func (s RawString) Sub(rhs Value) Value

func (RawString) Type

func (s RawString) Type() Type

type Resource

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

func NewResource

func NewResource() *Resource

func (*Resource) LoadJSON

func (rsc *Resource) LoadJSON(r io.Reader) error

func (*Resource) T

func (rsc *Resource) T(rt Runtime, locale string, message Message) (Value, error)

type RootNode

type RootNode struct {
	Fragments []Node
	// contains filtered or unexported fields
}

type Rule

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

type Runtime

type Runtime interface {
	Kind() string
	Join(s, sep Value) String
	Split(s, sep Value) Slice
	Bold(value Value) RawString
	Italic(value Value) RawString
	Link(label, href, target Value) RawString
	PadLeft(value, length, padding Value) String
	PadRight(value, length, padding Value) String
	TrimLeft(value, trim Value) String
	TrimRight(value, trim Value) String
	Integer(value Value) Int
	Fraction(value, roundingUnit Value) Float
	T(key Value, locale string, value, opt Value) String
	PostProcess(value Value, after AfterPostProcessFunc) any
}

type SelectNode

type SelectNode struct {
	Value Node
	Opts  []*OptNode
	// contains filtered or unexported fields
}

type Slice

type Slice []Value

func NewSlice

func NewSlice(values []Value) Slice

func NewValues

func NewValues[T any](s []T) (Slice, error)

func (Slice) Add

func (s Slice) Add(rhs Value) Value

func (Slice) AsBool

func (s Slice) AsBool() Bool

func (Slice) AsFloat

func (s Slice) AsFloat() Float

func (Slice) AsInt

func (s Slice) AsInt() Int

func (Slice) AsSlice

func (s Slice) AsSlice() Slice

func (Slice) AsString

func (s Slice) AsString() String

func (Slice) Div

func (s Slice) Div(rhs Value) Value

func (Slice) Equal

func (s Slice) Equal(rhs Value) Bool

func (Slice) Less

func (s Slice) Less(rhs Value) Bool

func (Slice) Mod

func (s Slice) Mod(rhs Value) Value

func (Slice) Mul

func (s Slice) Mul(rhs Value) Value

func (Slice) Sub

func (s Slice) Sub(rhs Value) Value

func (Slice) Type

func (s Slice) Type() Type

type SliceNode

type SliceNode struct {
	LHS   Node
	Start Node
	End   Node
	// contains filtered or unexported fields
}

type String

type String struct {
	Value string
}

func NewString

func NewString(s string) String

func (String) Add

func (s String) Add(rhs Value) Value

func (String) AsBool

func (s String) AsBool() Bool

func (String) AsFloat

func (s String) AsFloat() Float

func (String) AsInt

func (s String) AsInt() Int

func (String) AsSlice

func (s String) AsSlice() Slice

func (String) AsString

func (s String) AsString() String

func (String) Div

func (s String) Div(rhs Value) Value

func (String) Equal

func (s String) Equal(rhs Value) Bool

func (String) Less

func (s String) Less(rhs Value) Bool

func (String) Mod

func (s String) Mod(rhs Value) Value

func (String) Mul

func (s String) Mul(rhs Value) Value

func (String) Sub

func (s String) Sub(rhs Value) Value

func (String) Type

func (s String) Type() Type

type Time

type Time struct {
	Value time.Time
}

func NewTime

func NewTime(t time.Time) Time

func (Time) Add

func (t Time) Add(rhs Value) Value

func (Time) AsBool

func (t Time) AsBool() Bool

func (Time) AsFloat

func (t Time) AsFloat() Float

func (Time) AsInt

func (t Time) AsInt() Int

func (Time) AsSlice

func (t Time) AsSlice() Slice

func (Time) AsString

func (t Time) AsString() String

func (Time) Div

func (t Time) Div(rhs Value) Value

func (Time) Equal

func (t Time) Equal(rhs Value) Bool

func (Time) Less

func (t Time) Less(rhs Value) Bool

func (Time) Mod

func (t Time) Mod(rhs Value) Value

func (Time) Mul

func (t Time) Mul(rhs Value) Value

func (Time) Sub

func (t Time) Sub(rhs Value) Value

func (Time) Type

func (t Time) Type() Type

type Token

type Token struct {
	Kind   TokenKind
	Lexeme string
}

type TokenKind

type TokenKind int
const (
	KindUnknown TokenKind = iota
	KindUnexpected
	KindEOF
	KindEnterExpr
	KindLeaveExpr
	KindInt
	KindFloat
	KindString
	KindText
	KindIdent
	KindOr
	KindAnd
	KindBang
	KindComma
	KindColon
	KindLParen
	KindRParen
	KindLBrack
	KindRBrack
	KindPlus
	KindMinus
	KindAsterisk
	KindSlash
	KindPercent
	KindEqual
	KindEqualEqual
	KindBangEqual
	KindGreater
	KindGreaterEqual
	KindLess
	KindLessEqual
	KindEqualGreater
)

func (TokenKind) String

func (k TokenKind) String() string

type Translation

type Translation struct {
	Condition Node
	Value     Node
	Group     []Translation
}

type Translations

type Translations map[string]Translation

type Type

type Type int
const (
	TypeUnknown Type = iota
	TypeBool
	TypeInt
	TypeFloat
	TypeString
	TypeRawString
	TypeTime
	TypeDuration
	TypeSlice
)

func (Type) String

func (t Type) String() string

type UnaryNode

type UnaryNode struct {
	Op  Token
	RHS Node
	// contains filtered or unexported fields
}

type Value

type Value interface {
	Type() Type
	AsBool() Bool
	AsInt() Int
	AsFloat() Float
	AsString() String
	AsSlice() Slice
	Equal(rhs Value) Bool
	Less(rhs Value) Bool
	Add(rhs Value) Value
	Sub(rhs Value) Value
	Mul(rhs Value) Value
	Div(rhs Value) Value
	Mod(rhs Value) Value
}

func Eval

func Eval(node Node, rt Runtime, locale string, vars Vars) (Value, error)

func NewValue

func NewValue(v any) (Value, error)

func T

func T(rt Runtime, locale string, message Message) (Value, error)

type Vars

type Vars map[string]Value

func NewVars

func NewVars(pairs []any) (Vars, error)

Jump to

Keyboard shortcuts

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