Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/165-english.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
管理器和命令行有了英文界面:按系统语言自动选,设置里可以改 (ModAVD/ModAVD#165)
2 changes: 2 additions & 0 deletions cmd/modavd-gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"modavd/internal/emu"
"modavd/internal/errs"
"modavd/internal/host"
"modavd/internal/i18n"
"modavd/internal/logx"
"modavd/internal/root"
"modavd/internal/ui"
Expand Down Expand Up @@ -112,6 +113,7 @@ func printVersion() int {
}

func run(args []string) (code int) {
i18n.Init()
var log *logx.Logger
defer func() {
// A GUI process has no console: a panic would otherwise end it without a trace.
Expand Down
20 changes: 20 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import (
"modavd/internal/emu"
"modavd/internal/errs"
"modavd/internal/host"
"modavd/internal/i18n"
"modavd/internal/logx"
"modavd/internal/mcp"
"modavd/internal/root"
Expand Down Expand Up @@ -217,6 +218,11 @@ func readerIsTerminal(r io.Reader) bool {
return ok && f != nil && fileIsTerminal(f)
}

func writerIsTerminal(w io.Writer) bool {
f, ok := w.(*os.File)
return ok && f != nil && fileIsTerminal(f)
}

// Main runs one command and returns the process exit code (errs.ExitCode, see the package comment).
func Main(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
if len(args) > 0 {
Expand All @@ -237,6 +243,7 @@ func Main(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
return emu.RunDiagnosticArchive(args[1:], stderr)
}
}
i18n.Init()
// StartOrphan re-runs this executable as the spawn helper.
if exe, err := os.Executable(); err == nil {
host.SetOrphanHelper(exe)
Expand Down Expand Up @@ -298,6 +305,19 @@ func run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.
d.stdinTerminal = readerIsTerminal
}
inv, perr := parseArgs(args)
// Human output in the language of the user (ModAVD/ModAVD#165), on a console only: what goes to a file or a pipe
// (the manager's update log, scripts) stays as before unless MODAVD_LANG asks for a language. --json, adb and
// mcp stay byte for byte, and so does what shell prints (the guest's own output).
if lang := i18n.Current(); !inv.json {
raw := inv.cmd != nil && inv.cmd.raw
forced := os.Getenv(i18n.EnvVar) != ""
if !raw && (inv.cmd == nil || inv.cmd.name != "shell") && (forced || writerIsTerminal(stdout)) {
stdout = i18n.Writer(stdout, lang)
}
if !raw && (forced || writerIsTerminal(stderr)) {
stderr = i18n.Writer(stderr, lang)
}
}
log := newLogger(stderr, inv.globals)
out := &output{stdout: stdout, stderr: stderr, json: inv.json, raw: inv.cmd != nil && inv.cmd.raw, log: log}
if perr != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"modavd/internal/avd"
"modavd/internal/components"
"modavd/internal/emu"
"modavd/internal/i18n"
)

// Human-readable summaries (stdout without --json). Wording follows AUTO-MAS where it has a message for the same
Expand Down Expand Up @@ -134,6 +135,13 @@ func displayWidth(s string) int {

// writeTable aligns columns by display width (text/tabwriter counts runes, which misaligns Chinese).
func writeTable(w io.Writer, rows [][]string) {
if lang := i18n.LangOf(w); lang != i18n.Chinese {
for _, row := range rows {
for k, cell := range row {
row[k] = i18n.Translate(lang, cell)
}
}
}
var widths []int
for _, row := range rows {
for k, cell := range row {
Expand Down
4 changes: 3 additions & 1 deletion internal/cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"

"modavd/internal/errs"
"modavd/internal/i18n"
"modavd/internal/logx"
)

Expand Down Expand Up @@ -54,7 +55,8 @@ func (o *output) success(cmd *command, data any) int {
return 0
}
if cmd.human != nil {
cmd.human(o.stdout, data)
// Text fields are translated before the summary is laid out (columns, "标题:原因" lines).
cmd.human(o.stdout, i18n.Localize(i18n.LangOf(o.stdout), data))
}
return 0
}
Expand Down
63 changes: 63 additions & 0 deletions internal/i18n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Translations / 界面翻译

The code is written in Chinese, and the Chinese text is the key: `locales/<lang>/<package>.json` maps each Chinese
string the code shows to its translation (`web.json` holds the page's own strings). See the package comment in
`i18n.go` for where translation happens and what stays Chinese.

代码里照旧写中文,中文原文就是键。新加或改了中文字符串以后:

```
go test ./internal/i18n -run TestCatalog -update # 把缺的键以 "" 加进表
go test ./internal/i18n -run TestCatalog -update -prune # 同时删掉源码里已经没有的键
```

再把 `""` 填成英文。`null` 表示故意不翻(只进日志文件、问题包,或者是拿来匹配中文输出的文字、发进客体的命令)。
`TestCatalog` 查四件事:表里有没有漏的、有没有过期的、译文的参数(`%d`、`%[2]s`、页面的 `{name}`)和原文对不对得上、
页面里有没有写在 `t()` 外面的中文。

`extra.json` 放代码拼出来、但拼的那段本身没有中文词的句式(比如 `fmt.Sprintf("%s%s(%s)", 标题, "通过", 原因)`,按拼好的样子写成 `%s通过(%s)`),不查是否过期。

## Writing the English

- Keep every verb of the key (`%d`, `%s`, `%v`, `%q`, `%.1f` …). To reorder, index them: `%[2]s … %[1]d`. A key
built from a concatenation has `%v` for each non-literal part.
- Plain, short English; sentence case; no Chinese punctuation. Keep leading / trailing spaces and newlines as in the
key.
- A message is often built from several keys ("外层:内层"): translate each key on its own so it reads well alone.
- `null` only for text no person sees in the manager or the CLI: problem report contents, patterns matched against
Chinese output of Windows or games, commands and values sent to the guest, file names.

## Terms

| 中文 | English |
| --- | --- |
| 实例 / 实例 3 | instance / instance 3 |
| 新建 | create |
| 开机 / 启动 | start (the instance); boot (Android booting) |
| 关机 / 强制关机 | shut down / force stop |
| 根目录 | root folder |
| 管理器 | manager |
| 命令行 | command line, CLI |
| 组件 / 补全组件 | components / Fetch components |
| 数据盘 / 自动扩盘 | data disk / automatic size |
| 无头 | headless |
| 老板键 | boss key |
| 托盘 | tray |
| 执行桥 | execution bridge |
| 问题包 | problem report |
| 电脑检查 | System check |
| 更新源 | update source |
| Mirror 酱 | MirrorChyan |
| 客体 / 宿主 | guest / host |
| 首次初始化 | initial setup |
| 更多操作 | More actions |
| 设置 | Settings |
| 检查更新 / 更新 | Check for updates / Update |
| 打开管理器 / 退出 | Open manager / Exit |
| 帧率 | frame rate (FPS) |
| 分辨率 | resolution |
| 加速 | acceleration |
| 诊断 | diagnostics |
| 代理 | proxy |
| 安卓 | Android |
| 星铁 / 终末地 / 1999 / 明日方舟 / 阴阳师 | Honkai: Star Rail / Arknights: Endfield / Reverse: 1999 / Arknights / Onmyoji |
Loading
Loading