Phase 7: languages
- Complete Spanish (neutral Latin American, Paraguay friendly, no inverted marks) and Dutch catalogs, including the Mini App and owner texts - Command menus per language in three scopes (private, group members, group admins); per-chat menus follow a user's /language and a group's /grouplang, from chat and from the Mini App - Bot name, description and short description per language, sent only when the catalog text changes (TELEGRAM_SET_PROFILE) - Dates from the catalog, numbers, sizes and durations per locale - Pseudo-locale en-XA generated from English (I18N_PSEUDO), owners only - Catalog checks: completeness, placeholders, tags, commands, plurals, Telegram length limits, untranslated text, unused and missing IDs - CI workflow; make i18n-check; docs/TRANSLATING.md - Test fix: the queued-download helper could match another chat's status Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4f11f939a0
commit
f1f9c6156c
+29
-2
@@ -3,6 +3,7 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"io/fs"
|
||||
@@ -34,6 +35,7 @@ type Bundle struct {
|
||||
names []string
|
||||
matcher language.Matcher
|
||||
log *slog.Logger
|
||||
opts []Option
|
||||
// raw keeps each catalog as parsed TOML, for exporting the Mini App's
|
||||
// messages to the browser.
|
||||
raw map[string]map[string]any
|
||||
@@ -43,13 +45,27 @@ type Bundle struct {
|
||||
printers map[string]*message.Printer
|
||||
}
|
||||
|
||||
// Option changes how catalogs are loaded.
|
||||
type Option func(*options)
|
||||
|
||||
type options struct{ pseudo bool }
|
||||
|
||||
// WithPseudo adds the pseudo-locale (Pseudo), made from the English
|
||||
// catalog, for testing translations.
|
||||
func WithPseudo() Option { return func(o *options) { o.pseudo = true } }
|
||||
|
||||
// Load reads every *.toml catalog in fsys. The file name is the language
|
||||
// tag, for example en.toml or es.toml.
|
||||
func Load(fsys fs.FS, log *slog.Logger) (*Bundle, error) {
|
||||
func Load(fsys fs.FS, log *slog.Logger, opts ...Option) (*Bundle, error) {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
var o options
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
b := &Bundle{
|
||||
opts: opts,
|
||||
bundle: goi18n.NewBundle(language.English),
|
||||
log: log,
|
||||
localizers: map[string]*goi18n.Localizer{},
|
||||
@@ -83,6 +99,17 @@ func Load(fsys fs.FS, log *slog.Logger) (*Bundle, error) {
|
||||
if !hasDefault {
|
||||
return nil, fmt.Errorf("i18n: the %s catalog is missing", Default)
|
||||
}
|
||||
if o.pseudo {
|
||||
doc := pseudoDoc(b.raw[Default], "")
|
||||
var buf bytes.Buffer
|
||||
if err := toml.NewEncoder(&buf).Encode(doc); err != nil {
|
||||
return nil, fmt.Errorf("i18n: pseudo-locale: %w", err)
|
||||
}
|
||||
if _, err := b.bundle.ParseMessageFileBytes(buf.Bytes(), Pseudo+".toml"); err != nil {
|
||||
return nil, fmt.Errorf("i18n: pseudo-locale: %w", err)
|
||||
}
|
||||
b.raw[Pseudo] = doc
|
||||
}
|
||||
// English first, so it is the matcher's default.
|
||||
b.tags = []language.Tag{language.English}
|
||||
for _, t := range b.bundle.LanguageTags() {
|
||||
@@ -109,7 +136,7 @@ func (b *Bundle) Hidden(lang string) bool { return strings.EqualFold(lang, Pseud
|
||||
// Reload replaces the catalogs with the ones in fsys. When they do not
|
||||
// load, the old ones stay in use and the error says why.
|
||||
func (b *Bundle) Reload(fsys fs.FS) error {
|
||||
fresh, err := Load(fsys, b.log)
|
||||
fresh, err := Load(fsys, b.log, b.opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user