fix(l10n): 观察宿主语言变化并即时刷新界面

This commit is contained in:
2026-11-03 20:43:04 +08:00
parent 3b1e711556
commit 7109d483b6
6 changed files with 77 additions and 9 deletions
+40
View File
@@ -0,0 +1,40 @@
import AppKit
import SwiftUI
private struct LocaleProbe: NSViewRepresentable {
@Environment(\.locale) var locale
var updated: (String) -> Void
func makeNSView(context: Context) -> NSView { NSView() }
func updateNSView(_ view: NSView, context: Context) { updated(locale.identifier) }
}
@main struct LanguageScopeChecks {
@MainActor static func main() {
_ = NSApplication.shared
Task { @MainActor in
let suite = "kymo-language-scope-" + UUID().uuidString
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let preferences = LanguagePreferences(defaults: defaults)
var observed = ""
var changes = 0
let window = NSWindow(contentRect: NSRect(x: 100, y: 100, width: 100, height: 100),
styleMask: [.borderless], backing: .buffered, defer: false)
// Build the host only once, just as the AppKit scene/panel does.
window.contentView = NSHostingView(rootView: AppLanguageScope(preferences: preferences, onChange: { changes += 1 }) {
LocaleProbe { observed = $0 }
})
window.orderFront(nil)
for language in [AppLanguage.english, .traditionalChinese, .simplifiedChinese] {
preferences.language = language
try? await Task.sleep(for: .milliseconds(150))
precondition(observed == language.identifier(), "Hosted locale did not refresh")
}
precondition(changes >= 2)
window.orderOut(nil)
print("PASS: AppKit-hosted locale refreshes in place through three languages and invokes change callbacks")
NSApp.terminate(nil)
}
NSApp.run()
}
}
+7
View File
@@ -218,3 +218,10 @@ swiftc -parse-as-library kymo/Core/Services/PublicIPAddressService.swift kymoTes
```
完整字符串表覆盖与格式参数检查:`python3 kymoTests/LocalizationCatalogChecks.py`。以 zh-Hans 为参数语义基准检查三语完整性,不能替代翻译语义审阅和界面排版验收。
AppKit 宿主即时语言刷新回归检查:
```bash
swiftc -parse-as-library kymo/Core/Services/LanguagePreferences.swift kymo/UI/Components/AppLanguageScope.swift kymoTests/LanguageScopeChecks.swift -o /private/tmp/kymo-scope-checks
/private/tmp/kymo-scope-checks
```