Files
Solis/internal/i18n/i18n.go
T
lucioandClaude Opus 4.6 ff1d1fc490 Add i18n framework with English and Chinese support
Introduce an embedded-locale i18n package exposing a T() template
function. UI strings across all templates and catalog category/item
names are now translatable via locale JSON files, selectable through
the new server.language config option. Falls back to English when a
locale is missing or fails to load.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
2026-05-21 21:30:56 -07:00

62 lines
1.0 KiB
Go

package i18n
import (
"embed"
"encoding/json"
"fmt"
"strings"
"sync"
)
//go:embed locales
var localesFS embed.FS
type Translator struct {
lang string
messages map[string]string
mu sync.RWMutex
}
func New(lang string) (*Translator, error) {
if lang == "" {
lang = "en"
}
t := &Translator{
lang: lang,
messages: make(map[string]string),
}
data, err := localesFS.ReadFile(fmt.Sprintf("locales/%s.json", lang))
if err != nil {
return nil, fmt.Errorf("loading locale %s: %w", lang, err)
}
if err := json.Unmarshal(data, &t.messages); err != nil {
return nil, fmt.Errorf("parsing locale %s: %w", lang, err)
}
return t, nil
}
func (t *Translator) T(key string) string {
t.mu.RLock()
defer t.mu.RUnlock()
if msg, ok := t.messages[key]; ok {
return msg
}
return key
}
func (t *Translator) TF(key string, args ...string) string {
msg := t.T(key)
for _, arg := range args {
msg = strings.Replace(msg, "%s", arg, 1)
}
return msg
}
func (t *Translator) Lang() string {
return t.lang
}