Files
Reims-Go/internal/macos/opencore/lang.go
T
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

107 lines
2.8 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"`
}
// 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 ""
}