Files
Reims-Go/internal/opencore/lang.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

122 lines
3.7 KiB
Go

package opencore
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Nwflower/reims-go/internal/i18n"
)
const (
// GuestLangSidecarFile stores the user's chosen OpenCore prev-lang:kbd value
// inside the VM directory so `update-opencore` rebuilds can replay it.
GuestLangSidecarFile = ".reims-go-guest-lang"
)
// Preset describes a selectable macOS boot/recovery language (`prev-lang:kbd`).
type Preset struct {
Value string `json:"value"`
LabelZH string `json:"label_zh"`
LabelEN string `json:"label_en"`
}
// Presets returns the built-in macOS prev-lang:kbd choices.
// `:0` pairs the locale with the standard US keyboard layout so terminal
// commands in macOS Recovery / Setup Assistant stay predictable; `:252` uses
// OpenCore's Pinyin layout ID.
func Presets() []Preset {
return []Preset{
{Value: "zh-Hans:0", LabelZH: "简体中文(美式键盘,推荐)", LabelEN: "Simplified Chinese (US Keyboard)"},
{Value: "zh-Hans:252", LabelZH: "简体中文(拼音键盘)", LabelEN: "Simplified Chinese (Pinyin)"},
{Value: "zh-Hant:0", LabelZH: "繁體中文(美式键盘)", LabelEN: "Traditional Chinese (US Keyboard)"},
{Value: "en-US:0", LabelZH: "English (US)", LabelEN: "English (US)"},
{Value: "ja-JP:0", LabelZH: "日本語(美式键盘)", LabelEN: "Japanese (US Keyboard)"},
{Value: "ko-KR:0", LabelZH: "한국어(美式键盘)", LabelEN: "Korean (US Keyboard)"},
}
}
// DefaultGuestLang chooses the default macOS prev-lang:kbd matching the app UI locale.
func DefaultGuestLang(locale i18n.Locale) string {
resolved := strings.ToLower(string(i18n.ResolveLocale(string(locale))))
switch {
case resolved == "zh-tw" || resolved == "zh-hk" || resolved == "zh-mo" || strings.HasPrefix(resolved, "zh-hant"):
return "zh-Hant:0"
case strings.HasPrefix(resolved, "zh"):
return "zh-Hans:0"
case strings.HasPrefix(resolved, "ja"):
return "ja-JP:0"
case strings.HasPrefix(resolved, "ko"):
return "ko-KR:0"
default:
return "en-US:0"
}
}
var reGuestLang = regexp.MustCompile(`^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}:[0-9]{1,6}$`)
// NormalizeGuestLang validates and normalizes a prev-lang:kbd string.
// An empty input returns ("", nil), meaning "unspecified / keep existing".
func NormalizeGuestLang(raw string) (string, error) {
s := strings.TrimSpace(raw)
if s == "" {
return "", nil
}
if !reGuestLang.MatchString(s) {
return "", fmt.Errorf("invalid macOS language code %q (expected format like zh-Hans:0 or en-US:0)", raw)
}
return s, nil
}
// ReadSavedGuestLang reads the persisted prev-lang:kbd preference from vmDir.
func ReadSavedGuestLang(vmDir string) string {
if vmDir == "" {
return ""
}
b, err := os.ReadFile(filepath.Join(vmDir, GuestLangSidecarFile))
if err != nil {
return ""
}
norm, err := NormalizeGuestLang(string(b))
if err != nil {
return ""
}
return norm
}
// WriteSavedGuestLang saves (or clears when empty) the VM's prev-lang:kbd preference.
func WriteSavedGuestLang(vmDir, lang string) error {
if vmDir == "" {
return fmt.Errorf("vm directory is empty")
}
norm, err := NormalizeGuestLang(lang)
if err != nil {
return err
}
p := filepath.Join(vmDir, GuestLangSidecarFile)
if norm == "" {
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
return os.WriteFile(p, []byte(norm+"\n"), 0o644)
}
// ResolveOpenCorePath returns the OpenCore image path inside vmDir (preferring
// opencore.qcow2, falling back to opencore.img), or "" if neither exists.
func ResolveOpenCorePath(vmDir string) string {
if vmDir == "" {
return ""
}
for _, name := range []string{"opencore.qcow2", "opencore.img"} {
p := filepath.Join(vmDir, name)
if info, err := os.Stat(p); err == nil && !info.IsDir() {
return p
}
}
return ""
}