Files
magpie/internal/gui/traymenu_test.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

116 lines
4.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The tray menu is in the page's language: the setting, or the system's
// when it follows the system (#301).
func TestTrayMenuLabels(t *testing.T) {
zhSys := func() string { return "zh-Hans-CN" }
enSys := func() string { return "en-US" }
for _, c := range []struct {
pref string
sys func() string
want string
}{
{"zh", enSys, "zh"},
{"en", zhSys, "en"},
{"system", zhSys, "zh"},
{"system", enSys, "en"},
{"", func() string { return "zh_CN.UTF-8" }, "zh"},
{"system", func() string { return "" }, "en"},
{"ja", enSys, "ja"},
{"system", func() string { return "ja-JP" }, "ja"},
{"", func() string { return "ja_JP.UTF-8" }, "ja"},
{"de", enSys, "de"},
{"system", func() string { return "de-DE" }, "de"},
{"", func() string { return "de_AT.UTF-8" }, "de"},
{"zh-TW", enSys, "zh-TW"},
{"system", func() string { return "zh-Hant-TW" }, "zh-TW"},
{"system", func() string { return "zh-TW" }, "zh-TW"},
{"system", func() string { return "zh-HK" }, "zh-TW"},
{"system", func() string { return "zh-Hant" }, "zh-TW"},
{"", func() string { return "zh_TW.UTF-8" }, "zh-TW"},
{"", func() string { return "zh_HK" }, "zh-TW"},
{"system", func() string { return "zh-Hans-HK" }, "zh"},
{"system", func() string { return "zh-SG" }, "zh"},
} {
if got := trayLang(c.pref, c.sys); got != c.want {
t.Errorf("trayLang(%q, %s) = %s, want %s", c.pref, c.sys(), got, c.want)
}
}
de := trayMenuLabels("de", "0.1.500", "0.1.501", false)
if de != (trayLabels{"magpie öffnen", "Zugangspunkt pausieren", "Version 0.1.500", "Zum Aktualisieren auf 0.1.501 neu starten", "magpie beenden"}) {
t.Errorf("de: %+v", de)
}
for lang, want := range map[string]string{"de": "en", "en": "en", "zh": "zh", "zh-TW": "zh"} {
if got := notesLang(lang); got != want {
t.Errorf("notesLang(%q) = %q, want %q", lang, got, want)
}
}
zh := trayMenuLabels("zh", "0.1.500", "", false)
if zh != (trayLabels{"打开 magpie", "暂停网关", "版本 0.1.500", "重启以更新", "退出 magpie"}) {
t.Errorf("zh: %+v", zh)
}
if tw := trayMenuLabels("zh-TW", "0.1.500", "0.1.501", false); tw != (trayLabels{"開啟 magpie", "暫停閘道", "版本 0.1.500", "重新啟動以更新到 0.1.501", "結束 magpie"}) {
t.Errorf("zh-TW: %+v", tw)
}
if ja := trayMenuLabels("ja", "0.1.500", "", false); ja != (trayLabels{"magpie を開く", "ゲートウェイを一時停止", "バージョン 0.1.500", "再起動してアップデート", "magpie を終了"}) {
t.Errorf("ja: %+v", ja)
}
if l := trayMenuLabels("zh", "0.1.500", "0.1.501", false); l.restart != "重启以更新到 0.1.501" {
t.Errorf("zh restart: %q", l.restart)
}
en := trayMenuLabels("en", "0.1.500", "0.1.501", false)
if en != (trayLabels{"Open magpie", "Pause Gateway", "Version 0.1.500", "Restart to Update to 0.1.501", "Quit magpie"}) {
t.Errorf("en: %+v", en)
}
// paused, the menu's line resumes the gateway (me2seeks on X)
for lang, want := range map[string]string{"en": "Resume Gateway", "zh": "恢复网关", "zh-TW": "恢復閘道", "ja": "ゲートウェイを再開", "de": "Zugangspunkt fortsetzen"} {
if l := trayMenuLabels(lang, "0.1.500", "", true); l.pause != want {
t.Errorf("%s paused: %q, want %q", lang, l.pause, want)
}
}
}
// A language changed on the Settings page relabels the tray menu; a save
// that leaves it as it was doesn't.
func TestSettingsLangRelabelsTray(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
if err := settings.Save(settings.Settings{Lang: "en"}); err != nil {
t.Fatal(err)
}
calls := 0
onLang = func() { calls++ }
t.Cleanup(func() { onLang = nil })
post := func(body string) {
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings", strings.NewReader(body)))
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
}
post(`{"theme":"dark","lang":"en"}`)
if calls != 0 {
t.Fatalf("relabelled %d times with the language unchanged", calls)
}
post(`{"theme":"dark","lang":"zh"}`)
if calls != 1 {
t.Fatalf("relabelled %d times after en → zh, want 1", calls)
}
if l := trayMenuLabels(trayLang(settings.Load().Lang, systemLang), "1", "", false); l.quit != "退出 magpie" {
t.Fatalf("after the change the menu reads %+v", l)
}
}