Files
magpie/internal/gui/traywords.go
T
yetone f49f6ba686 tray: a usage window's name and unit read as its card says them (yi_time)
yi_time on Discord (Windows, v0.1.2777, magpie in Chinese): the tray
icon's tooltip said "WorkBuddy / Credits 0 / 500 credits · 已用 0% · 27 天
13 小时 后重置". The window's name and unit come from the WorkBuddy plugin
in English. The Usage page's card says both with t() (t(w.name),
t(w.unit)), so it read 积分 there, but trayCardText wrote them as given.

The fix is for every window, not WorkBuddy only:
- traywords.go reads the page's own table (embedded assets/i18n.js, once)
  and trayCardText says each window's name, and each " · " part of a
  pool's or family's name, and its count's unit, from it. The tray now
  reads as the card does in zh, zh-TW, ja and de; English is unchanged.
- i18n.js gains the window names and units plugins report that it lacked
  in every language, so the card had them in English too: Extra credits,
  Prepaid credits, Free model tokens (workbuddy); Add-on, Dedicated,
  Shared, Total credits (qoder); Monthly credits, Purchased credits,
  5-hour window (agnes); Promotional Credits (gcp-ai); Free models
  (devin); Free usage, Monthly, Included (cursor); Free trial (kiro);
  Dollar Usage (trae); Neurons/day (free allowance) (cloudflare-workers-ai);
  AI Q&A and the unit "questions" (orcaterm). Collected from
  magpie-community/plugins origin/main usage() results. No plugin change
  is needed: the plugins send English words, and magpie translates them.

Tests:
- TestTrayTooltipSaysWindowWordsAsTheCard: yi_time's card (Credits,
  0 / 500 credits, 27d 13h) gives the exact tooltip in all five languages,
  and a pool's window ("Gemini · 5 hours") is said in its parts.
- TestPluginWindowWordsSaidInEveryLanguage: each word above is in every
  language's table. TestParsePageWords: two pairs to a line, escapes, a
  comment and NOTICES left out.
- quota-window-words.test.cjs: the Usage card says WorkBuddy's and
  Qoder's window names and unit in en, zh, zh-TW, ja, de, chromium and
  webkit, 1100px and 420px.

Run at the pushed head 3886c2cc0's tree (on origin/main 84da86ebd), on mote:
- go test -run 'Tray|PageWords|PluginWindow|Translated|Budget'
  ./internal/gui: ok. GOOS=linux go vet -tags nogui ./internal/gui,
  GOOS=windows go vet ./internal/gui: ok. gofmt -l: clean.
- node --test quota-window-words, i18n-keys, quota-credits,
  quota-name-translated: 52 pass, 0 fail.
Before the rebase (on 05aba5ca4): go test ./internal/gui whole: all ok but
TestMainWindowCorners/macOS_26_SDK, which is #3949 (mote's macOS 27). The
GUI files that name any new key (credit-days, fold-show-grip,
plugin-namesake, plugin-option-words, quota-curve, usage-fold,
usage-merge, quota-pools, quota-pool-order, quota-warm, warm-reset-asleep,
warm-at-times, gateway-systemone): 250 pass, 6 fail, all in usage-fold,
en included; usage-fold fails 4 of 10 on an untouched 05aba5ca4 too
(#3415).

Broken by hand: with trayusage.go as it was, the tray test is red with
the reporter's line ("Credits 0 / 500 credits · 已用 0%"); with i18n.js as
it was, the words test and quota-window-words are red in zh, zh-TW, ja, de.

Not tried: the tooltip in a real tray on Windows; the whole GUI suite (no
layout change or API call). A window name a plugin adds later, and the
Chinese names some Chinese plugins send (积分, 限时积分), still read as sent
until i18n.js has them. A balance window's display text ("120 credits")
is the plugin's text and stays as sent, on the card too.
2026-11-03 11:38:09 +08:00

88 lines
2.7 KiB
Go

package gui
import (
"encoding/json"
"regexp"
"strings"
"sync"
)
// pageWords are the page's own translations (assets/i18n.js's I18N), by
// language, read once: the words a usage card's window is named and
// counted in ("Credits", "credits", "5 hours") come from the vendor or a
// plugin in English, and the Usage page says them with t(). The tray's
// tooltip says them with the same table (pageWord), so a window reads in
// the tray as on its card, in every language the page has (yi_time on
// Discord: "Credits 0 / 500 credits · 已用 0%").
var pageWords = sync.OnceValue(func() map[string]map[string]string {
b, err := assets.ReadFile("assets/i18n.js")
if err != nil {
return nil
}
return parsePageWords(string(b))
})
var (
pageLangHead = regexp.MustCompile(`^ "?([\w-]+)"?: \{`)
pagePair = regexp.MustCompile(`"((?:[^"\\]|\\.)*)"\s*:\s*"((?:[^"\\]|\\.)*)"`)
)
// parsePageWords reads I18N's language blocks from i18n.js's source: each
// ` zh: {` or ` "zh-TW": {` line starts one, and its entries are
// ` "English": "translation",` lines, one or more to a line. A string
// JSON can't read (a JS-only escape) is left out: that word reads in
// English, as the page's own fallback does.
func parsePageWords(src string) map[string]map[string]string {
if i := strings.Index(src, "\n};"); i >= 0 {
src = src[:i]
}
out := map[string]map[string]string{}
var cur map[string]string
for _, line := range strings.Split(src, "\n") {
if m := pageLangHead.FindStringSubmatch(line); m != nil {
cur = map[string]string{}
out[m[1]] = cur
continue
}
if cur == nil || !strings.HasPrefix(line, " \"") {
continue
}
for _, m := range pagePair.FindAllStringSubmatch(line, -1) {
var k, v string
if json.Unmarshal([]byte(`"`+m[1]+`"`), &k) != nil || json.Unmarshal([]byte(`"`+m[2]+`"`), &v) != nil {
continue
}
cur[k] = v
}
}
return out
}
// pageWord is s as the page says it in lang (its t()): s itself in
// English, or where the page has no word for it.
func pageWord(lang, s string) string {
if v, ok := pageWords()[lang][s]; ok && v != "" {
return v
}
return s
}
// trayWindowName is a window's name as its card says it in lang: the
// name whole when the page has it, else each " · " part the page has
// ("Gemini · 5 hours", a pool's window named with its pool by trayShared,
// a model's with its family by trayModels), as the card writes
// pool + " · " + t(name).
func trayWindowName(lang, name string) string {
if lang == "en" || name == "" {
return name
}
if w := pageWord(lang, name); w != name {
return w
}
parts := strings.Split(name, " · ")
for i, p := range parts {
parts[i] = pageWord(lang, p)
}
return strings.Join(parts, " · ")
}