fix(l10n): 观察宿主语言变化并即时刷新界面
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user