Repository navigation
EN Localization
Русский: RU-Localization
The i18n package provides immutable ICU MessageFormat catalogs. Catalogs are validated and compiled during startup; a bad locale, duplicate catalog, empty key, or invalid message makes i18n.New return an error.
bundle, err := i18n.New("en",
i18n.Map("en", map[string]string{
"items": `{count, plural, one {# item} other {# items}}`,
"role": `{role, select, admin {Administrator} other {User}}`,
}),
i18n.Map("ru", map[string]string{
"items": `{count, plural, one {# предмет} few {# предмета} many {# предметов} other {# предмета}}`,
"role": `{role, select, admin {Администратор} other {Пользователь}}`,
}),
)
if err != nil {
panic(err)
}
app.Use(state.SessionMiddleware(store.Sessions(), 24*time.Hour))
app.Use(i18n.Middleware(bundle))Register session middleware first. Locale resolution then uses the saved session locale, the platform-provided Event.Locale, and finally the bundle default. A regional tag also falls back to its base language, so ru-RU can use a ru catalog. A missing key falls back through the same chain and eventually returns i18n.ErrMessageNotFound.
text, err := ctx.T("items", i18n.Args{"count": 5})
if err != nil {
return err
}
return ctx.SendText(text)ctx.T accepts zero or one argument map and returns formatting errors. ICU plural, select, parameters, and locale-aware number, date, and time values are supported:
"total": `Total: {amount, number}`
"created": `Created: {when, date, short} at {when, time, short}`i18n.JSON reads a flat JSON object from any io.Reader:
catalog, err := i18n.JSON("en", strings.NewReader(`{
"hello": "Hello, {name}!"
}`))
bundle, err := i18n.New("en", catalog)Keys must map directly to ICU pattern strings; nested JSON objects are not accepted.
if err := i18n.SetLocale(ctx, "ru"); err != nil {
return err
}SetLocale updates the current request immediately and stores the normalized language tag when a state session is attached. SessionMiddleware persists it after the handler succeeds. Without session middleware the change lasts only for the current request.
Pager renderers, FSM handlers, and keyboard.Builder labels need no special integration: translate their text with the same ctx.T call. See Pagination and FSM and Scenes.
- Telegram messages and callbacks use the sender's
LanguageCode. - Discord commands and component interactions use the interaction locale.
- VK events and ordinary Discord messages do not provide a locale; use a saved choice or the default catalog.