tik

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 8 Imported by: 1

README

GoDoc GoReportCard Coverage Status

tik-go

This is an implementation of the Textual Internationalization Key (TIK) specification in the Go programming language.

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

View Source
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")
)
View Source
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 ErrParser

type ErrParser struct {
	Index int
	Err   error
}

func (ErrParser) Error

func (e ErrParser) Error() string

func (ErrParser) Unwrap

func (e ErrParser) Unwrap() error

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}

func NewParser

func NewParser(conf Config) *Parser

NewParser creates a new TIK parser instance.

func (*Parser) Parse

func (p *Parser) Parse(input string) (tik TIK, err error)

Parse parses input and returns a validated TIK, otherwise returns an error. The tokens slice in the returned TIK is a copy of the buffer and doesn't alias the internal parser buffer. If you want to avoid the token buffer copy use ParseFn with caution instead.

func (*Parser) ParseFn

func (p *Parser) ParseFn(input string, fn func(tik TIK)) ErrParser

ParseFn is similar to Parse but avoid copying the token buffer and instead uses the original buffer of the parser in the tik provided to fn.

WARNING: Do not alias and use the token slice once fn returns!

type TIK

type TIK struct {
	Raw    string
	Tokens Tokens
}

TIK is a parsed and validated textual internationalization token.

func (TIK) Placeholders

func (t TIK) Placeholders() iter.Seq2[int, Token]

Placeholders returns an iterators that iterates over placeholder tokens.

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.

func (Token) String

func (t Token) String(source string) string

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}
)

func (TokenType) String

func (t TokenType) String() string

type Tokenizer

type Tokenizer struct{}

func (*Tokenizer) Tokenize

func (t *Tokenizer) Tokenize(buffer Tokens, s string, c Config) (Tokens, ErrParser)

Tokenize appends all tokens from input to buffer and returns the buffer. If c == nil the default configuration applies.

type Tokens

type Tokens []Token

Tokens is a slice of the lexical tokens of a textual internationalization key.

Jump to

Keyboard shortcuts

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