Files
Reims-Go/internal/plugin/locale_tools_test.go
T
Dnyo666 ceeec602be feat(i18n,plugin): implement end-to-end i18n & L10n v2 architecture and language pack protocol
- 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
2026-11-30 14:56:19 +08:00

173 lines
6.1 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestOfficialReferenceLanguagePacks(t *testing.T) {
for _, rel := range []string{
filepath.Join("testdata", "official.lang.zh-tw"),
filepath.Join("testdata", "official.lang.ja-jp"),
} {
reports, findings, err := CheckLocalePack(rel)
if err != nil {
t.Fatalf("CheckLocalePack(%s): %v", rel, err)
}
if len(reports) != 1 {
t.Fatalf("CheckLocalePack(%s): expected 1 report, got %d", rel, len(reports))
}
for _, f := range findings {
if f.Severity == "error" || f.Severity == "warn" {
t.Fatalf("CheckLocalePack(%s) unexpected %s (%s): %s", rel, f.Severity, f.Check, f.Message)
}
}
}
// End-to-end Manager test: link and enable both official reference packs.
dataRoot := t.TempDir()
mgr, err := NewManager(ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.8"})
if err != nil {
t.Fatalf("NewManager: %v", err)
}
defer mgr.Close()
twAbs, err := filepath.Abs(filepath.Join("testdata", "official.lang.zh-tw"))
if err != nil {
t.Fatal(err)
}
jaAbs, err := filepath.Abs(filepath.Join("testdata", "official.lang.ja-jp"))
if err != nil {
t.Fatal(err)
}
if _, err := mgr.LinkDev(context.Background(), twAbs); err != nil {
t.Fatalf("LinkDev zh-tw: %v", err)
}
if err := mgr.Enable(context.Background(), "official.lang.zh-tw", false); err != nil {
t.Fatalf("Enable zh-tw: %v", err)
}
if _, err := mgr.LinkDev(context.Background(), jaAbs); err != nil {
t.Fatalf("LinkDev ja-jp: %v", err)
}
if err := mgr.Enable(context.Background(), "official.lang.ja-jp", false); err != nil {
t.Fatalf("Enable ja-jp: %v", err)
}
uiPacks, backendPacks := mgr.ResolvedLocalePacks()
i18n.SetPluginCatalogs(backendPacks)
defer i18n.SetPluginCatalogs(nil)
if len(uiPacks) != 2 {
t.Fatalf("expected 2 UI locale packs, got %d", len(uiPacks))
}
// 1. Verify zh-HK -> zh-TW -> zh-CN -> en-US multi-hop fallback!
if got := i18n.T(i18n.Locale("zh-HK"), "validate.invalid_memory"); got != "分配記憶體不得小於 2G(例如 8G、16G)" {
t.Fatalf("zh-HK -> zh-TW hop failed: got %q", got)
}
// Key not overridden in zh-TW should fall back to zh-CN (NOT en-US!).
if got := i18n.T(i18n.Locale("zh-HK"), "doctor.os"); got != "操作系统" {
t.Fatalf("zh-HK -> zh-TW -> zh-CN hop failed: got %q, want 操作系统", got)
}
// 2. Verify ja-JP -> en-US fallback & FormatGuestTerm dual terminology!
if got := i18n.T(i18n.Locale("ja-JP"), "guide.select_disk.btn.erase"); got != "ディスクユーティリティで消去する" {
t.Fatalf("ja-JP native translation failed: got %q", got)
}
if got := i18n.T(i18n.Locale("ja-JP"), "doctor.os"); got != "Operating System" {
t.Fatalf("ja-JP -> en-US fallback failed: got %q, want Operating System", got)
}
if dual := i18n.FormatGuestTerm(i18n.Locale("zh-TW"), "en-US:0", "macos.diskUtility", true); dual != "「磁碟工具程式 (Disk Utility)」" {
t.Fatalf("FormatGuestTerm(zh-TW, en-US:0) = %q", dual)
}
if dual := i18n.FormatGuestTerm(i18n.Locale("ja-JP"), "en-US:0", "macos.diskUtility", true); dual != "「ディスクユーティリティ (Disk Utility)」" {
t.Fatalf("FormatGuestTerm(ja-JP, en-US:0) = %q", dual)
}
if single := i18n.FormatGuestTerm(i18n.Locale("ja-JP"), "ja-JP:0", "macos.diskUtility", true); single != "「ディスクユーティリティ」" {
t.Fatalf("FormatGuestTerm(ja-JP, ja-JP:0) = %q", single)
}
}
func TestInitAndCheckLocalePackCLIWorkflow(t *testing.T) {
// 1. Scaffold full zh-TW host language pack -> should validate at 100% coverage with 0 errors.
outTW := filepath.Join(t.TempDir(), "lang-zh-tw")
if err := InitLocalePack(LocaleInitOptions{
Locale: "zh-TW",
OutDir: outTW,
}); err != nil {
t.Fatalf("InitLocalePack(zh-TW): %v", err)
}
reps, _, err := CheckLocalePack(outTW)
if err != nil {
t.Fatalf("CheckLocalePack(scaffolded zh-TW): %v", err)
}
if len(reps) != 1 || reps[0].Errors != 0 || reps[0].TotalPercent != 100.0 {
t.Fatalf("expected 100%% coverage and 0 errors on freshly scaffolded pack, got %+v", reps)
}
// Note: scaffolding zh-TW copies zh-CN source text as a starting template, so regional
// lint should proactively flag Mainland terms like 默认/内存/磁盘 for the translator!
if reps[0].Warnings == 0 {
t.Fatal("expected regional terminology warnings on untranslated zh-CN -> zh-TW template")
}
// 2. Scaffold cross-plugin override pack (--for acme.backup).
outFor := filepath.Join(t.TempDir(), "lang-for-plugin")
if err := InitLocalePack(LocaleInitOptions{
Locale: "ko-KR",
OutDir: outFor,
ForPlugin: "acme.backup",
}); err != nil {
t.Fatalf("InitLocalePack(--for): %v", err)
}
repsFor, _, err := CheckLocalePack(outFor)
if err != nil || len(repsFor) != 1 || repsFor[0].Errors != 0 {
t.Fatalf("CheckLocalePack(--for) failed: err=%v reps=%+v", err, repsFor)
}
// 3. Inject an ICU placeholder mismatch and a syntax error -> CheckLocalePack must report errors.
badDir := filepath.Join(t.TempDir(), "bad-pack")
if err := os.MkdirAll(filepath.Join(badDir, "locales"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(badDir, "locales", "core.json"), []byte(`{
"footerDiskValue": "{wrongVar} GB free",
"staleKeyNotInHost": "hello"
}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(badDir, "plugin.json"), []byte(`{
"schema": 1,
"id": "test.lang.de",
"version": "0.1.0",
"name": "Deutsch",
"contributes": {
"locales": [{"locale": "de-DE", "files": {"core": "locales/core.json"}}]
}
}`), 0o644); err != nil {
t.Fatal(err)
}
badReps, badFindings, err := CheckLocalePack(badDir)
if err != nil {
t.Fatalf("CheckLocalePack(badDir): %v", err)
}
if len(badReps) != 1 || badReps[0].Errors == 0 || len(badReps[0].UnknownKeys) != 1 {
t.Fatalf("expected placeholder mismatch error and 1 unknown key, got %+v", badReps)
}
hasParamErr := false
for _, f := range badFindings {
if f.Check == "icu-params" && strings.Contains(f.Message, "footerDiskValue") {
hasParamErr = true
}
}
if !hasParamErr {
t.Fatalf("expected icu-params finding for footerDiskValue, got %+v", badFindings)
}
}