Documentation
¶
Overview ¶
Package tik provides the parser for the Textual Internationalization Key format, as well as the TIK to ICU message translator.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrTextEmpty = errors.New("empty text body") ErrUnexpClosure = errors.New("unexpected directive closure") ErrUknownPlaceholder = errors.New("unknown placeholder") ErrCardinalPluralEmpty = errors.New("empty cardinal pluralization") ErrDirectiveStartsCardinalPlural = errors.New( "directive starts a cardinal pluralization", ) ErrUnclosedPlaceholder = errors.New("unclosed placeholder") ErrNestedPluralization = errors.New("nested pluralization") ErrContextUnclosed = errors.New("unclosed context") ErrContextEmpty = errors.New("empty context") ErrContextInvalid = errors.New("invalid context") )
var DefaultConfig = Config{
OrdinalPluralOtherSuffix: "th",
}
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
OrdinalPluralOtherSuffix string
}
Config defines the TIK environment configuration.
type ICUTranslator ¶
type ICUTranslator struct {
// contains filtered or unexported fields
}
ICUTranslator is a reusable TIK to ICU message translator.
func NewICUTranslator ¶
func NewICUTranslator(conf Config) *ICUTranslator
func (*ICUTranslator) TIK2ICU ¶
func (i *ICUTranslator) TIK2ICU(tik TIK) (str string)
TIK2ICU translates a TIK into an incomplete ICU message that needs to be translated later. (See https://unicode-org.github.io/icu/userguide/format_parse/messages/) modifiers define positional modifiers such as gender and pluralization that weren't defined in the tik.
func (*ICUTranslator) TIK2ICUBuf ¶
func (i *ICUTranslator) TIK2ICUBuf( tik TIK, fn func(buf *bytes.Buffer), )
TIK2ICUBuf similar TIK2ICU but gives temporary access to the internal buffer to avoid string allocation if only a temporary byte slice is needed. This function can be used instead TIK2ICU to achieve efficiency when possible but must be used with caution!
WARNING: Never use or alias buf outside fn!
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a TIK parser instance.
Example ¶
package main
import (
"fmt"
tik "github.com/romshark/tik/tik-go"
)
func main() {
const input = `{name} had {# messages} on {date-medium} at {time-full}`
conf := tik.DefaultConfig
parser := tik.NewParser(conf)
tk, err := parser.Parse(input)
if err != nil {
fmt.Println("ERR:", err)
return
}
fmt.Println(" ")
fmt.Println("TOKENS:", len(tk.Tokens))
for _, x := range tk.Tokens {
fmt.Printf("%d-%d: %q (%s)\n", x.IndexStart, x.IndexEnd,
x.String(input), x.Type.String())
}
icu := tik.NewICUTranslator(conf)
fmt.Println("")
fmt.Println("ICU Message:")
fmt.Println(icu.TIK2ICU(tk))
}
Output: TOKENS: 9 0-6: "{name}" (text with gender) 6-11: " had " (literal) 11-14: "{# " (pluralization) 14-22: "messages" (literal) 22-23: "}" (pluralization block end) 23-27: " on " (literal) 27-40: "{date-medium}" (date medium) 40-44: " at " (literal) 44-55: "{time-full}" (time full) ICU Message: {var0} had {var1, plural, other {# messages}} on {var2, date, medium} at {var3, time, full}
type Token ¶
type Token struct {
// IndexStart defines the start index of this token in the original TIK.
IndexStart int
// IndexEnd defines the end index of this token in the original TIK.
IndexEnd int
Type TokenType
}
Token is a lexical TIK token.
type TokenType ¶
type TokenType uint8
TokenType defines the type of a TIK lexical token.
const ( TokenTypeContext TokenType TokenTypeLiteral // String. TokenTypeText // {text} TokenTypeTextWithGender // {name} // Numbers. TokenTypeInteger // {integer} TokenTypeNumber // {number} // Pluralization. TokenTypeCardinalPluralStart // `{# ` TokenTypeCardinalPluralEnd // `}` TokenTypeOrdinalPlural // {ordinal} // TokenTypeDateFull equals "EEEE, MMMM d, y" TokenTypeDateFull // {date-full} // TokenTypeDateLong equals "MMMM d, y" TokenTypeDateLong // {date-long} // TokenTypeDateMedium equals "MMM d, y" TokenTypeDateMedium // {date-medium} // TokenTypeDateShort equals "M/d/yy" TokenTypeDateShort // {date-short} // TokenTypeTimeFull equals "hour(h/H), minute(mm), second(ss), and zone(zzzz)." TokenTypeTimeFull // {time-full} // TokenTypeTimeLong equals "hour, minute, second, and zone(z)" TokenTypeTimeLong // {time-long} // TokenTypeTimeMedium equals "hour, minute, second." TokenTypeTimeMedium // {time-medium} // TokenTypeTimeShort equals "hour, minute." TokenTypeTimeShort // {time-short} // Currency. TokenTypeCurrency // {currency} )