i18n

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package i18n provides native multi-language support for PaperValet. Every user-facing string should go through the catalog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithLang

func WithLang(ctx context.Context, l Lang) context.Context

WithLang returns a context carrying the active language.

Types

type Catalog

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

Catalog is a translation table: lang -> key -> template. Templates use {0}, {1} style placeholders.

func CoreCatalog

func CoreCatalog() *Catalog

func New

func New(defaultLang Lang) *Catalog

New creates an empty catalog with the given default language.

func SetupCatalog added in v0.1.1

func SetupCatalog() *Catalog

SetupCatalog holds the strings of the terminal `initialize` flow. It is separate from CoreCatalog because it is only needed before the bot runs.

func (*Catalog) Add

func (c *Catalog) Add(l Lang, key, template string)

Add registers a translation for a language.

func (*Catalog) AddMany

func (c *Catalog) AddMany(l Lang, entries map[string]string)

AddMany registers multiple translations for a language.

func (*Catalog) Default

func (c *Catalog) Default() Lang

Default returns the default language.

func (*Catalog) Has

func (c *Catalog) Has(l Lang, key string) bool

Has reports whether a key exists in the given language (or default).

func (*Catalog) Keys

func (c *Catalog) Keys(l Lang) []string

Keys returns all keys registered for a language, sorted.

func (*Catalog) Languages

func (c *Catalog) Languages() []Lang

Languages returns all registered languages, sorted.

func (*Catalog) Merge

func (c *Catalog) Merge(other *Catalog)

Merge merges another catalog's entries into this one.

func (*Catalog) SetDefault

func (c *Catalog) SetDefault(l Lang)

SetDefault changes the default language.

func (*Catalog) T

func (c *Catalog) T(l Lang, key string, args ...any) string

T translates a key into the given language, substituting {N} placeholders. Fallback chain: requested lang → default lang → key itself.

type Lang

type Lang string

Lang is a language code like "zh-CN" or "en-US".

const (
	ZhCN Lang = "zh-CN"
	EnUS Lang = "en-US"
)

func LangFrom

func LangFrom(ctx context.Context, fallback Lang) Lang

LangFrom returns the language stored in ctx, or the fallback.

type Manager

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

Manager holds the catalog plus per-user language preferences.

func NewManager

func NewManager(cat *Catalog) *Manager

NewManager creates a language manager over a catalog.

func (*Manager) Catalog

func (m *Manager) Catalog() *Catalog

Catalog returns the underlying catalog.

func (*Manager) ClearUserLangs added in v0.1.1

func (m *Manager) ClearUserLangs()

ClearUserLangs drops per-user overrides so the catalog default applies.

func (*Manager) SetUserLang

func (m *Manager) SetUserLang(userID int64, l Lang)

SetUserLang records a user's preferred language.

func (*Manager) T

func (m *Manager) T(userID int64, key string, args ...any) string

T translates for a user, falling back to the catalog default.

func (*Manager) TContext

func (m *Manager) TContext(ctx context.Context, userID int64, key string, args ...any) string

TContext translates using the language carried in ctx (if any), else user pref.

func (*Manager) UserLang

func (m *Manager) UserLang(userID int64) Lang

UserLang returns a user's preferred language, or the catalog default.

Jump to

Keyboard shortcuts

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