Files
magpie/internal/tui/gateway.go
T
yetone 32c830fc16 tui: speaks the language picked in Settings, Simplified Chinese first
zaidu259434 on X asked why magpie's CLI/TUI pages don't support Chinese.
The GUI had en/zh/zh-TW/ja/de; the TUI had English only.

- internal/uilang: the language pick the tray already made (the
  setting, else the system's: zh-Hant regions to zh-TW, other zh to zh,
  ja, de) moved out of internal/gui so the TUI asks the same function.
  The tray calls it unchanged; its tests moved with it.
- internal/tui/i18n.go: tr/trf look a string up by its English text in
  a zh table (464 entries), falling back to the English. zh-TW reads
  Simplified for now; ja and de stay English. Terms come from the GUI's
  i18n.js where the same word is there (供应商, 路由, 用量, 会话, 资源库,
  分组, 方案 …).
- Every user-visible string in the TUI goes through tr/trf; widths that
  counted runes (Library's name column, sessions' columns) use
  lipgloss.Width, so 共享指令 is no longer cut to 共….
- Names stay names: agent and model ids, flags, commands, and the field
  labels magpie/ultracode/harness/opus/sonnet/haiku/fable.

TestEveryWordHasZh reads the TUI's source (every tr/trf literal, every
hints label, the tables shown through tr, agent field labels) and fails
on a word with no zh, on a zh format that takes the English one's
arguments badly, and on a zh entry no longer shown.

Not changed: the CLI's own output is still English (#6649).

Run:
- mica (macOS): go vet ./internal/uilang ./internal/gui ./internal/tui;
  go test -count=1 -timeout 45m ./... was still running when this
  was pushed (pushed on the coordinator's ask); its result goes on #6649.
- wsl-run (Linux): go vet + go test ./internal/tui ./internal/uilang ok;
  GOOS=windows go vet ./internal/uilang ./internal/gui ./internal/tui ok;
  GOOS=darwin CGO_ENABLED=0 go vet ./internal/uilang ./internal/tui ok.
- Real TUI (go build -tags nogui) in WSL magpie-desktop, sandbox HOME
  with settings.json {"lang":"zh"}, tmux 150x40: every page (Agents,
  Providers, Routing, Usage, Sessions, Library), the add-provider
  picker, sessions stats, the gateway line and footers read in Chinese.
  The Library row showed 共… before the lipgloss.Width fix, 共享指令 after.
- Broken by hand: an added tr("zzz untranslated") and a zh format with
  its %s dropped each turned TestEveryWordHasZh red.

Not tried: zh-TW, ja and de wording (they fall back as above); the TUI
on Windows' console.
2026-11-14 19:09:43 +08:00

97 lines
2.5 KiB
Go

package tui
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// The terminal serves the gateway while it is open, as the app does, when
// no magpie has it: in WSL with only magpie tui, the agents it connected to
// magpie asked an address nothing listened at, and Claude Code said
// ECONNREFUSED (gnayiab on X). Another magpie that has it (the app, magpie
// web, magpie serve) keeps it, and this one takes it up once that one is
// gone.
// gw is who serves the gateway, for the status line.
var gw struct {
here atomic.Bool // this process serves it
err atomic.Pointer[string] // why it couldn't, "" or nil when it could
}
// gatewayWatch is how often the terminal looks for a gateway gone.
var gatewayWatch = 15 * time.Second
// serveGateway serves the gateway here until ctx ends whenever no magpie
// answers at its address, and returns a wait for the one it serves to have
// stopped.
func serveGateway(ctx context.Context) (wait func()) {
var wg sync.WaitGroup
try := func() {
if gw.here.Load() || gateway.Running() {
return
}
s := gateway.New()
up := make(chan error, 1)
gw.here.Store(true)
wg.Add(1)
go func() {
defer wg.Done()
err := s.ListenAndServe(ctx)
gw.here.Store(false)
if err != nil && ctx.Err() == nil {
msg := err.Error()
gw.err.Store(&msg)
}
up <- err
}()
// listening, or refused the port, in a moment
for i := 0; i < 20 && !gateway.Running(); i++ {
select {
case <-up:
return
case <-time.After(50 * time.Millisecond):
}
}
if gateway.Running() {
gw.err.Store(nil)
}
}
try()
wg.Add(1)
go func() {
defer wg.Done()
t := time.NewTicker(gatewayWatch)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
try()
}
}
}()
return wg.Wait
}
// gatewayStatus is the status line's word on the gateway when it has
// nothing else to say: served here, or why agents can't reach it.
func gatewayStatus() string {
if gw.here.Load() {
return sMuted.Render("● " + trf("gateway %s · served while magpie tui is open", gateway.URL()))
}
if e := gw.err.Load(); e != nil && *e != "" {
return sBad.Render("✗ ") + sText.Render(trf("gateway not running: %s", *e))
}
return ""
}
// gatewayGoneNote is said as the terminal quits having served the gateway.
func gatewayGoneNote() string {
return tr("magpie: the gateway stopped with magpie tui; agents connected to magpie reach it while magpie tui, magpie web or magpie serve runs")
}