hex/ios/HEXMobile/TranslateDictationRow.swift
Ryan Vogel ee34feb49b iOS: dictate in every language Apple Speech has on device
A Norwegian TestFlight tester's iPhone silently fell back to English:
Hex picked the first preferred language SpeechTranscriber supports, and
SpeechTranscriber has no Norwegian, Danish, Swedish or Hebrew. Testers
also asked for Spanish and German.

What changed:
- SpeechLanguagePicker picks the dictation language: an explicit choice
  wins (SpeechTranscriber where it has the language, else
  DictationTranscriber); Automatic takes the first preferred iPhone
  language either module has, SpeechTranscriber first for the same
  language, then the region's locale, then en-US. Nynorsk tries Bokmal.
- AppleSpeechModule wraps SpeechTranscriber and DictationTranscriber
  (preset longDictation) for the same SpeechAnalyzer flow and asset
  download; the app's AppleSpeechTranscriber and in-keyboard dictation
  both use it and AppleSpeechLanguages, so they resolve the same language
  and share downloaded assets.
- Settings > Dictation language (iOS 26): Automatic plus every locale
  either module supports, by name, saved in the App Group
  (settings.dictationLanguage). Picking one prepares its model now, or
  after the dictation in flight. The setup card's Speech model row names
  the real language ("Norwegian Bokmal · Apple").
- English-only steps follow the language: the cleanup tagger skips other
  languages; Apple Intelligence cleanup runs only when the model supports
  the language; filler trimming is per language (English "er" was eating
  Norwegian "er" = "is"; Portuguese "um" is a word); translation's source
  is the dictation language instead of a guess.
- Replaced: AppleSpeechTranscriber.preferredLocale()/isAvailable with
  preferredLanguage()/shouldUse(_:). An iPhone without SpeechTranscriber
  still dictates English with Parakeet, as before.

Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2),
189 tests, 0 failures, including new picker, language-change, filler and
tagger tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO
succeeds. The simulator can't transcribe (SpeechTranscriber unavailable,
DictationTranscriber "Model not available"); the same Swift files built
for macOS 26 transcribed say clips word for word: de-DE and es-ES via
SpeechTranscriber, nb-NO and he-IL via DictationTranscriber (no
punctuation from DictationTranscriber under any preset). Not tried on a
physical iPhone.
2026-11-20 07:52:32 -05:00

96 lines
4 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
@preconcurrency import Translation
/// Experiments › Translate what I say: the toggle, the target language, 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 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
}
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
}
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() }
}
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()
}
.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
}
}