- Build single-source-of-truth 7-domain locale catalog (`locales/{zh-CN,en-US}/*.json`, 1,100+ keys) and `cmd/i18ngen` generator emitting Go catalogs and eager/lazy-split TypeScript domain modules (`core`/`vm` eager; `wizard`/`settings`/`plugins` lazy)
- Add zero-dependency ICU MessageFormat subset (`{var}`, `{n, number}`, CLDR `plural`, `select`), 8-hop cycle-free regional fallback (`zh-HK/zh-MO -> zh-Hant -> zh-TW -> zh-CN`), `auto` OS display-language preference, and `en-XA` pseudo-localization across Go and TypeScript
- Introduce `CodedError` supportability with `[E_CODE]` badges, collapsible `[detail]` diagnostics, and automatic SMC OSK / SMBIOS serial / MAC / token redaction
- Add Host × Guest macOS dual-terminology annotation (`FormatGuestTerm`) across 6 official Apple guest UI locales and localize Win32 system tray, file pickers, and DPI-adaptive `installguide` overlays with CJK font fallback probing
- Upgrade L1–L4 plugin localization to dual-track v2: Track A full language packs (`files`, WOFF2 `fonts`, cross-plugin `pluginOverrides`), Track B plugin-scoped `l10n` bundles (`HasUIMessages` isolation), live `plugin/localeChanged` notifications, `< 2ms` static CLI pack loading, and `reims-go plugin locale init|check` translator toolchain with `zh-TW` and `ja-JP` reference packs
- Harden security, WCAG 2.1 AA token contrast, `< 768px` responsive drawer & form layouts, and RTL logical properties; add backend security/locale regression tests and frontend `node --test` static/runtime guard suite in CI
# Conflicts:
# frontend/src/i18n.ts
# internal/cli/cli.go
# internal/i18n/i18n.go
425 lines
12 KiB
Go
425 lines
12 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/driver/reims"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
"github.com/Nwflower/reims-go/internal/version"
|
|
)
|
|
|
|
func openCLIPluginManager(cfg *config.Config, safeMode bool) (*plugin.Manager, *driver.Router, error) {
|
|
mgr, err := plugin.NewManager(plugin.ManagerOptions{
|
|
DataRoot: cfg.DataRoot,
|
|
HostVersion: version.Version,
|
|
SafeMode: safeMode,
|
|
RedactFn: reims.RedactText,
|
|
LocaleFn: func() i18n.Locale {
|
|
return i18n.EffectiveLocale(cfg.Locale)
|
|
},
|
|
Logger: func(source, line string) {
|
|
fmt.Fprintf(os.Stderr, "[%s] %s\n", source, line)
|
|
},
|
|
SidecarDriverFactory: func(contrib plugin.GuestDriverContribution, sup *plugin.SidecarSupervisor) any {
|
|
return driver.NewSidecarDriver(contrib, sup)
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
cb := builtin.HostCallbacks{
|
|
GetConfig: func() config.Config { return *cfg },
|
|
Log: func(source, line string) {
|
|
fmt.Fprintf(os.Stderr, "[%s] %s\n", source, line)
|
|
},
|
|
}
|
|
router := driver.NewRouter(mgr.Registry(), func() string {
|
|
return filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms")
|
|
})
|
|
mgr.Registry().SetInUseChecker(func(pluginID string) (bool, string, error) {
|
|
seen := map[string]bool{}
|
|
candidates := append([]string{pluginID}, mgr.Registry().DeclaredDriverIDs(pluginID)...)
|
|
for _, did := range candidates {
|
|
if did == "" || seen[did] {
|
|
continue
|
|
}
|
|
seen[did] = true
|
|
if inUse, reason, err := router.CheckDriverInUse(context.Background(), did); err != nil || inUse {
|
|
return inUse, reason, err
|
|
}
|
|
}
|
|
return false, "", nil
|
|
})
|
|
if err := builtin.RegisterAll(mgr.Registry(), cb, mgr.DesiredFor); err != nil {
|
|
mgr.Close()
|
|
return nil, nil, err
|
|
}
|
|
if err := builtin.MigrateRetired(mgr); err != nil {
|
|
fmt.Fprintf(os.Stderr, "[plugin] could not migrate merged core plugin state: %v\n", err)
|
|
}
|
|
_ = mgr.LoadExternal()
|
|
mgr.Registry().Reconcile(context.Background())
|
|
mgr.MarkBootComplete()
|
|
|
|
return mgr, router, nil
|
|
}
|
|
|
|
func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
|
if len(args) == 0 {
|
|
return fmt.Errorf("usage: reims-go plugin <list|install|link|enable|disable|remove|info|doctor|locale|run>")
|
|
}
|
|
ctx := context.Background()
|
|
switch args[0] {
|
|
case "list":
|
|
fs := flag.NewFlagSet("plugin list", flag.ContinueOnError)
|
|
asJSON := fs.Bool("json", false, "Output JSON")
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
list := mgr.LocalizePluginInfos(mgr.Registry().List())
|
|
if *asJSON {
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", " ")
|
|
return enc.Encode(list)
|
|
}
|
|
for _, p := range list {
|
|
mark := " "
|
|
switch p.State {
|
|
case plugin.StateActive:
|
|
mark = "*"
|
|
case plugin.StateDegraded, plugin.StateFailed:
|
|
mark = "!"
|
|
}
|
|
fmt.Printf("%s %-34s %-8s %-8s %-9s %-8s %s\n",
|
|
mark, p.ID, p.Version, p.Tier, p.Trust, p.State, p.Name.Get(string(loc)))
|
|
}
|
|
return nil
|
|
|
|
case "install":
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go plugin install <archive.zip>")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
info, err := mgr.InstallLocalArchive(ctx, args[1], plugin.PackageInstallOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "plugin.installed", info.ID, info.Version, info.Trust, info.Tier))
|
|
if info.NeedsApproval {
|
|
fmt.Println(i18n.T(loc, "plugin.needs_approval", info.ID))
|
|
}
|
|
return nil
|
|
|
|
case "link":
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go plugin link <dir>")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
info, err := mgr.LinkDev(ctx, args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "plugin.linked", info.ID, info.Version, info.InstallDir))
|
|
return nil
|
|
|
|
case "enable":
|
|
fs := flag.NewFlagSet("plugin enable", flag.ContinueOnError)
|
|
approve := fs.Bool("approve-high-risk", false, "Explicitly approve high-risk permissions or community L3 execution")
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
if fs.NArg() < 1 {
|
|
return fmt.Errorf("usage: reims-go plugin enable [--approve-high-risk] <id>")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
if err := mgr.Enable(ctx, fs.Arg(0), *approve); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "plugin.enabled", fs.Arg(0)))
|
|
return nil
|
|
|
|
case "disable":
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go plugin disable <id>")
|
|
}
|
|
// Load in safeMode so a broken external plugin never prevents disabling it (M9).
|
|
mgr, _, err := openCLIPluginManager(cfg, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
if err := mgr.Disable(ctx, args[1]); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "plugin.disabled", args[1]))
|
|
return nil
|
|
|
|
case "remove":
|
|
fs := flag.NewFlagSet("plugin remove", flag.ContinueOnError)
|
|
purge := fs.Bool("purge", false, "Also delete plugin private data and logs")
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
if fs.NArg() != 1 {
|
|
return fmt.Errorf("usage: reims-go plugin remove [--purge] <id>")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
if err := mgr.Remove(ctx, fs.Arg(0), *purge); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "plugin.removed", fs.Arg(0), *purge))
|
|
return nil
|
|
|
|
case "info":
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go plugin info <id>")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
info, ok := mgr.Registry().Info(args[1])
|
|
if !ok {
|
|
return fmt.Errorf("%s", i18n.T(loc, "plugin.not_found", args[1]))
|
|
}
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", " ")
|
|
return enc.Encode(info)
|
|
|
|
case "doctor":
|
|
fs := flag.NewFlagSet("plugin doctor", flag.ContinueOnError)
|
|
contractPath := fs.String("contract", "", "Optional path to ui-contract.json for CSS validation")
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
var contract *plugin.UIContract
|
|
if *contractPath != "" {
|
|
raw, err := os.ReadFile(*contractPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var c plugin.UIContract
|
|
if err := json.Unmarshal(raw, &c); err != nil {
|
|
return fmt.Errorf("parse ui-contract.json: %w", err)
|
|
}
|
|
contract = &c
|
|
}
|
|
target := ""
|
|
if fs.NArg() > 0 {
|
|
target = fs.Arg(0)
|
|
}
|
|
// If target is an existing directory, validate it directly (developer workflow, §5.1).
|
|
if target != "" {
|
|
if st, err := os.Stat(target); err == nil && st.IsDir() {
|
|
findings := plugin.DoctorDirectory(target, version.Version, contract)
|
|
return printDoctorFindings(loc, findings)
|
|
}
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
if target != "" {
|
|
info, ok := mgr.Registry().Info(target)
|
|
if !ok {
|
|
return fmt.Errorf("%s", i18n.T(loc, "plugin.target_not_found", target))
|
|
}
|
|
if info.InstallDir == "" {
|
|
fmt.Printf("[ok] %s@%s (%s, state=%s)\n", info.ID, info.Version, info.Source, info.State)
|
|
return nil
|
|
}
|
|
return printDoctorFindings(loc, plugin.DoctorDirectory(info.InstallDir, version.Version, contract))
|
|
}
|
|
// Audit all installed external plugins.
|
|
var allFindings []plugin.DoctorFinding
|
|
for _, info := range mgr.Registry().List() {
|
|
if info.InstallDir != "" {
|
|
allFindings = append(allFindings, plugin.DoctorDirectory(info.InstallDir, version.Version, contract)...)
|
|
} else {
|
|
allFindings = append(allFindings, plugin.DoctorFinding{
|
|
Severity: "ok",
|
|
Check: info.ID,
|
|
Message: fmt.Sprintf("%s@%s state=%s", info.ID, info.Version, info.State),
|
|
})
|
|
}
|
|
}
|
|
return printDoctorFindings(loc, allFindings)
|
|
|
|
case "locale":
|
|
return cmdPluginLocale(loc, args[1:])
|
|
|
|
case "run":
|
|
if len(args) < 3 {
|
|
return fmt.Errorf("usage: reims-go plugin run <id> <command> [key=value...]")
|
|
}
|
|
mgr, _, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
cmdArgs := map[string]any{}
|
|
for _, kv := range args[3:] {
|
|
k, v, ok := strings.Cut(kv, "=")
|
|
if !ok || strings.TrimSpace(k) == "" {
|
|
return fmt.Errorf("invalid argument %q (expected key=value)", kv)
|
|
}
|
|
cmdArgs[k] = v
|
|
}
|
|
res, err := mgr.Registry().InvokeCommand(ctx, args[1], args[2], cmdArgs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", " ")
|
|
return enc.Encode(res)
|
|
|
|
default:
|
|
return fmt.Errorf("unknown plugin subcommand %q", args[0])
|
|
}
|
|
}
|
|
|
|
func printDoctorFindings(loc i18n.Locale, findings []plugin.DoctorFinding) error {
|
|
hasErr := false
|
|
for _, f := range findings {
|
|
fmt.Printf("[%s] (%s) %s\n", f.Severity, f.Check, f.Message)
|
|
if f.Severity == "error" {
|
|
hasErr = true
|
|
}
|
|
}
|
|
if hasErr {
|
|
return fmt.Errorf("%s", i18n.T(loc, "plugin.doctor.errors"))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cmdPluginLocale(loc i18n.Locale, args []string) error {
|
|
if len(args) == 0 {
|
|
return fmt.Errorf("usage: reims-go plugin locale <init|check> ...")
|
|
}
|
|
switch args[0] {
|
|
case "init":
|
|
// Allow flags either before or after the positional <bcp47-locale> argument.
|
|
var flagArgs, posArgs []string
|
|
rest := args[1:]
|
|
for i := 0; i < len(rest); i++ {
|
|
a := rest[i]
|
|
if strings.HasPrefix(a, "-") {
|
|
flagArgs = append(flagArgs, a)
|
|
if !strings.Contains(a, "=") && i+1 < len(rest) && !strings.HasPrefix(rest[i+1], "-") {
|
|
i++
|
|
flagArgs = append(flagArgs, rest[i])
|
|
}
|
|
} else {
|
|
posArgs = append(posArgs, a)
|
|
}
|
|
}
|
|
fs := flag.NewFlagSet("plugin locale init", flag.ContinueOnError)
|
|
outDir := fs.String("out", "", "Output directory for the scaffolded language pack")
|
|
pluginID := fs.String("id", "", "Optional reverse-domain plugin ID (default: community.lang.<locale>)")
|
|
forPlugin := fs.String("for", "", "Optional target plugin ID to scaffold pluginOverrides for")
|
|
if err := fs.Parse(flagArgs); err != nil {
|
|
return err
|
|
}
|
|
posArgs = append(posArgs, fs.Args()...)
|
|
if len(posArgs) < 1 || strings.TrimSpace(*outDir) == "" {
|
|
return fmt.Errorf("usage: reims-go plugin locale init <bcp47-locale> --out <dir> [--id <plugin.id>] [--for <target.plugin>]")
|
|
}
|
|
if err := plugin.InitLocalePack(plugin.LocaleInitOptions{
|
|
Locale: posArgs[0],
|
|
OutDir: *outDir,
|
|
PluginID: *pluginID,
|
|
ForPlugin: *forPlugin,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("[ok] scaffolded language pack (%s) in %s\n", posArgs[0], *outDir)
|
|
return nil
|
|
|
|
case "check":
|
|
var flagArgs, posArgs []string
|
|
for _, a := range args[1:] {
|
|
if strings.HasPrefix(a, "-") {
|
|
flagArgs = append(flagArgs, a)
|
|
} else {
|
|
posArgs = append(posArgs, a)
|
|
}
|
|
}
|
|
fs := flag.NewFlagSet("plugin locale check", flag.ContinueOnError)
|
|
asJSON := fs.Bool("json", false, "Output structured JSON report")
|
|
if err := fs.Parse(flagArgs); err != nil {
|
|
return err
|
|
}
|
|
posArgs = append(posArgs, fs.Args()...)
|
|
if len(posArgs) < 1 {
|
|
return fmt.Errorf("usage: reims-go plugin locale check [--json] <dir>")
|
|
}
|
|
reports, findings, err := plugin.CheckLocalePack(posArgs[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if *asJSON {
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(reports); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
for _, rep := range reports {
|
|
fmt.Printf("Locale %s (%s): %d/%d translated (%.1f%%)\n",
|
|
rep.Locale, rep.PluginID, rep.TotalTranslated, rep.TotalReference, rep.TotalPercent)
|
|
for _, d := range rep.Domains {
|
|
fmt.Printf(" %-15s %4d/%-4d (%5.1f%%)\n", d.Domain, d.Translated, d.Total, d.Percent)
|
|
}
|
|
}
|
|
for _, f := range findings {
|
|
if f.Check == "locale-coverage" {
|
|
continue
|
|
}
|
|
fmt.Printf("[%s] (%s) %s\n", f.Severity, f.Check, f.Message)
|
|
}
|
|
}
|
|
for _, f := range findings {
|
|
if f.Severity == "error" {
|
|
return fmt.Errorf("%s", i18n.T(loc, "plugin.doctor.errors"))
|
|
}
|
|
}
|
|
return nil
|
|
|
|
default:
|
|
return fmt.Errorf("unknown plugin locale subcommand %q (expected init or check)", args[0])
|
|
}
|
|
}
|