hex/ios/HEXShared/DictationLanguageSwitch.swift
Ryan Vogel 2b225b390b iOS: switch the dictation language from the keyboard
Testers asked to change the dictation language without opening Hex.

What changed:
- Dictation bar: a language trigger beside the style ("Norsk") opens a
  tray of chips: Auto plus up to five languages.
- Full keyboard: the Hex key menu gets a Language row and page with the
  same choices, and the space bar names the dictation language faintly
  ("English", "Norsk"), like Apple's.
- DictationLanguageSwitch picks the choices (the language set, recent
  picks, then the iPhone's preferred languages, each matched to a locale
  Apple Speech has; Nynorsk maps to Bokmal) and names each in its own
  language, adding a region code only where two share a language.
- LanguageSettings keeps recent picks, what Automatic resolves to, and
  Apple's language list (cached when Hex or the keyboard lists it); a
  pick in the keyboard writes the same settings.dictationLanguage as
  Hex's menu. In-keyboard dictation reads it per dictation; a warm Hex
  session follows it in its 100 ms loop and prepares the language
  between dictations; a keyboard handoff and the Action Button pick it
  up before recording.
- Typing logs record the dictation language setting and a
  dictation_language UI event.

Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2):
195 tests, 0 failures, 1 skipped (the Apple Speech clip test, which
needs HEX_SPEECH_CLIPS), including new switch, recents, follow and
space-bar tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO
succeeds; screenshots of the Hex menu's language page (light, dark),
the space bar, the dictation bar's tray and the settings row checked by
eye. The trigger's last change (chevron instead of a globe, which looked
like the next-keyboard key) was compiled, not re-shot. Not tried on a
physical iPhone.
2026-11-20 07:52:38 -05:00

70 lines
3.5 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 Foundation
/// The keyboard's quick dictation-language switch: which languages it offers and the names
/// it shows for them. The choice is Settings › Dictation language (`LanguageSettings`).
nonisolated enum DictationLanguageSwitch {
static let maxChoices = 5
/// Up to five languages, best first: the one set now, the ones picked recently, then the
/// iPhone's preferred languages. Each is matched to a language Apple Speech has
/// (`supported`, BCP 47) when that list is known, and dropped when it has none.
static func choices(current: String?, recent: [String], preferred: [String], supported: [String]) -> [String] {
var result: [String] = []
for identifier in [current].compactMap({ $0 }) + recent + preferred {
guard let match = supportedMatch(for: identifier, in: supported),
!result.contains(match) else { continue }
result.append(match)
if result.count == maxChoices { break }
}
return result
}
/// The supported locale for an identifier: the same region, else the language's usual
/// region ("es" is Spain's), else any. Nynorsk maps to Bokmål. With no list, itself.
static func supportedMatch(for identifier: String, in supported: [String]) -> String? {
let wanted = Locale(identifier: identifier)
guard !supported.isEmpty else { return wanted.identifier(.bcp47) }
var code = wanted.language.languageCode?.identifier
if code == "nn" || code == "no" { code = "nb" }
let candidates = supported.filter { Locale(identifier: $0).language.languageCode?.identifier == code }
guard !candidates.isEmpty, let code else { return nil }
if let region = wanted.region?.identifier,
let exact = candidates.first(where: { Locale(identifier: $0).region?.identifier == region }) {
return exact
}
let usual = Locale.Language(identifier: Locale.Language(identifier: code).maximalIdentifier).region?.identifier
return candidates.first { Locale(identifier: $0).region?.identifier == usual } ?? candidates.first
}
/// The language's own short name: "English", "Norsk", "Español", "Deutsch", "עברית".
static func nativeName(for identifier: String) -> String {
let code = Locale(identifier: identifier).language.languageCode?.identifier ?? identifier
switch code {
case "nb", "no": return "Norsk"
case "nn": return "Nynorsk"
default: break
}
let own = Locale(identifier: code)
guard let name = own.localizedString(forLanguageCode: code) else { return identifier }
return name.prefix(1).uppercased(with: own) + name.dropFirst()
}
/// Native names for a list, with the region code only where two share a language:
/// "English (US)", "English (GB)", "Norsk".
static func nativeNames(for identifiers: [String]) -> [String: String] {
var perLanguage: [String: Int] = [:]
for identifier in identifiers {
perLanguage[nativeName(for: identifier), default: 0] += 1
}
var names: [String: String] = [:]
for identifier in identifiers {
let name = nativeName(for: identifier)
if perLanguage[name, default: 0] > 1, let region = Locale(identifier: identifier).region?.identifier {
names[identifier] = "\(name) (\(region))"
} else {
names[identifier] = name
}
}
return names
}
}