mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
A Spanish-speaking TestFlight tester liked Translate what I say but always
speaks Spanish and only sometimes wants the English. Until now translation
was all or nothing, set in Experiments.
- DictationTranslationSettings.pastesTranslation (App Group key
experiments.translateDictation.pastesTranslation, default true, so
existing users still get translations) is a switch that keeps the last
choice. translateIfEnabled skips translation while it says as spoken, in
the app (warm session, handoff, Action Button) and in in-keyboard
dictation alike.
- TranslationPasteChoice names both sides in English, like the rest of
Hex's copy: "Paste in English" / "Paste in Spanish" ("Paste as spoken"
when the dictation language is unknown, "Norwegian" without "Bokmål").
It exists only while the experiment is on, the keyboard has Full Access,
and you dictate in a language other than the target.
- Full keyboard: the Hex key menu gets two rows under Language, each with a
checkmark, and the space bar reads "Español → English" while dictations
paste translated.
- Dictation bar: the language tray gets a "Paste in English" chip, lit
while translating, and the language label reads "Español → English". The
label now has layout priority so it isn't cut to "Español → E…".
- Settings: a "Paste in English" toggle under Insert in mirrors the switch.
Turning the experiment on resets it to translated.
- Simulator fixtures: --translate-to <code> and --paste-as-spoken.
Verified: new TranslationPasteSwitchTests (titles and visibility, the
remembered choice, as spoken skipping translation, the model's label, and
the Hex key menu's rows, their checkmarks and their callback). Full
xcodebuild test: 230 tests, 0 failures, 1 skipped (AppleSpeechClipTests).
Rendered on an iPhone 18 Pro simulator (iOS 27.2): full-keyboard-style in
light and dark, translated and as spoken, without translation (unchanged),
full-keyboard-idle, keyboard-language, keyboard-warm, keyboard-recording and
experiments. The simulator can't actually translate (Apple's
LanguageAvailability says es -> en is unsupported there), so a translated
paste is unverified on device.
126 lines
5.3 KiB
Swift
126 lines
5.3 KiB
Swift
import SwiftUI
|
||
@preconcurrency import Translation
|
||
|
||
/// Experiments › Translate what I say: the toggle, the target language, the switch the
|
||
/// keyboard's menus also have (paste translated or as spoken), and the one-time download
|
||
/// of Apple's on-device language pack.
|
||
struct TranslateDictationRow: View {
|
||
@State private var enabled = DictationTranslationSettings().isEnabled
|
||
@State private var target = DictationTranslationSettings().targetLanguage
|
||
@State private var pastesTranslation = DictationTranslationSettings().pastesTranslation
|
||
@State private var languages: [Locale.Language] = []
|
||
@State private var status: LanguageAvailability.Status?
|
||
@State private var download: TranslationSession.Configuration?
|
||
|
||
/// What you dictate in: Settings › Dictation language, or the language it last resolved.
|
||
private var source: Locale.Language {
|
||
LanguageSettings().effectiveDictationLanguage.map { Locale(identifier: $0).language }
|
||
?? Locale.current.language
|
||
}
|
||
|
||
/// "Paste in English" for what you dictate in, or nil when that is the target already.
|
||
private var pasteChoice: TranslationPasteChoice? {
|
||
TranslationPasteChoice(
|
||
target: target,
|
||
source: LanguageSettings().effectiveDictationLanguage ?? Locale.current.identifier
|
||
)
|
||
}
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 8) {
|
||
Toggle(isOn: $enabled) {
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
Text("Translate what I say")
|
||
.font(.system(size: 15, weight: .regular))
|
||
.foregroundStyle(HexTheme.textBase)
|
||
Text("Speak your language, insert another, translated on this iPhone")
|
||
.font(.system(size: 12))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
}
|
||
}
|
||
.tint(HexTheme.success)
|
||
.onChange(of: enabled) { _, value in
|
||
DictationTranslationSettings().isEnabled = value
|
||
// Turning it on means translating, whatever the switch said last time.
|
||
if value {
|
||
pastesTranslation = true
|
||
}
|
||
}
|
||
|
||
if enabled {
|
||
HStack {
|
||
Text("Insert in")
|
||
.font(.system(size: 13))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
Spacer()
|
||
Picker("Insert in", selection: $target) {
|
||
ForEach(languages, id: \.minimalIdentifier) { language in
|
||
Text(name(of: language)).tag(language.minimalIdentifier)
|
||
}
|
||
}
|
||
.pickerStyle(.menu)
|
||
}
|
||
.onChange(of: target) { _, value in
|
||
DictationTranslationSettings().targetLanguage = value
|
||
Task { await refreshStatus() }
|
||
}
|
||
|
||
if let pasteChoice {
|
||
Toggle(isOn: $pastesTranslation) {
|
||
Text(pasteChoice.translatedTitle)
|
||
.font(.system(size: 13))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
}
|
||
.tint(HexTheme.success)
|
||
.onChange(of: pastesTranslation) { _, value in
|
||
DictationTranslationSettings().pastesTranslation = value
|
||
}
|
||
}
|
||
|
||
switch status {
|
||
case .installed:
|
||
Text("Ready. Works offline.")
|
||
.font(.system(size: 12))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
case .supported:
|
||
Button("Download \(name(of: Locale.Language(identifier: target)))") {
|
||
download = TranslationSession.Configuration(
|
||
source: source,
|
||
target: Locale.Language(identifier: target)
|
||
)
|
||
}
|
||
.font(.system(size: 13, weight: .medium))
|
||
case .unsupported:
|
||
Text("Apple can't translate between these languages.")
|
||
.font(.system(size: 12))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
default:
|
||
EmptyView()
|
||
}
|
||
}
|
||
}
|
||
.translationTask(download) { session in
|
||
try? await session.prepareTranslation()
|
||
await refreshStatus()
|
||
}
|
||
.onAppear {
|
||
// The keyboard's menus change it too.
|
||
pastesTranslation = DictationTranslationSettings().pastesTranslation
|
||
}
|
||
.task {
|
||
let supported = await LanguageAvailability().supportedLanguages
|
||
languages = supported
|
||
.filter { $0.languageCode != source.languageCode }
|
||
.sorted { name(of: $0) < name(of: $1) }
|
||
await refreshStatus()
|
||
}
|
||
}
|
||
|
||
private func refreshStatus() async {
|
||
status = await LanguageAvailability().status(from: source, to: Locale.Language(identifier: target))
|
||
}
|
||
|
||
private func name(of language: Locale.Language) -> String {
|
||
Locale.current.localizedString(forIdentifier: language.minimalIdentifier) ?? language.minimalIdentifier
|
||
}
|
||
}
|