hex/ios/HEXShared/DictationLanguageSwitch.swift
Ryan Vogel c5400a4686
iOS: Russian and Serbian (Latin) full-keyboard layouts (#2075)
A TestFlight tester (build 35) asked for Russian and Serbian (Latin) layouts in
Settings › Keyboard › Keyboard layout. KeyboardLanguage gets russian ("ru") and
serbianLatin ("sr-Latn") cases, the way #2069 added Italian, French and Portuguese.

Russian is Apple's ЙЦУКЕН: 11, 11 and 9 letters, ё on a held е and ъ on a held ь.
The long-press lists are Apple's own, read from the iOS 27.2 simulator's
TextInput_ru bundle, base letter first (Apple's combining-macron forms а̄ е̄ о̄ с̄ are
left out). Autocorrect and suggestions use UITextChecker's ru_RU dictionary.

Serbian (Latin) is Apple's QWERTZ option for it. Apple's iPhone Serbian Latin
keyboard has no keys of its own for č ć đ š ž (TextInput_sr lists QWERTY, QWERTZ and
AZERTY; only the iPad's Croatian layouts give them keys), so they are held on c, d,
s and z with Apple's lists. iOS 27.2 has no Serbian (or Croatian) UITextChecker
dictionary: suggestions and autocorrect stay off for it, never English. The
dictionary candidates are Latin-only (sr_Latn), since a Cyrillic sr dictionary
would reject every Latin word.

Automatic picks Russian for ru-* and Serbian (Latin) for sr-Latn-* dictation
languages, iPhone keyboards or preferred languages. Bare sr / sr-RS (Cyrillic)
still gets English: Hex has no Cyrillic Serbian layout. Spatial autocorrect has no
ru or sr-Latn model, so neither gets a decoder (same rule as #2069).

KeyboardLayout: Russian's nine-letter row 3 overlapped the 10-key-sized shift and
delete by about 9 pt. Where a letter row wouldn't fit, shift and delete now narrow
to fit (to about a letter's width, as on Apple's Russian keyboard); every existing
layout computes the same modW, so their frames are unchanged. Hit-testing is
untouched. DictationLanguageSwitch.nativeName names a language in its own script
("sr-Latn-RS" is "Srpski", not "Српски"); the space bar still names the dictation
language ("Русский" for ru-RU).

Snapshot scenes get Russian and Serbian drafts (--keyboard-layout ru|sr-Latn,
--accent-key е|ь|c|s).

Verified on an iOS 27.2 iPhone 17 Pro simulator: full HEXMobileTests suite, 277
tests, 0 failures, 1 skipped as before (one new Russian autocorrect case was
adjusted and re-run on its own after the full run). New cases in
KeyboardLayoutTests, AutocorrectEngineTests, FullKeyboardEngineTests and
KeyboardBridgeTests. Live in Messages with the Hex keyboard: Automatic with ru-RU
dictation shows the Russian layout and "Русский" on the space bar, "минкт"
suggests "минут", and holding е typed "ещё". Picking Srpski in Settings, holding c
and s typed "čekaj šta" with an empty strip. Compared with Apple's Russian and
Serbian Latin keyboards on the same simulator: same rows, letter-sized
shift/delete on Russian, and the same е, ь and c menus.
2026-11-20 17:44:49 +00:00

72 lines
3.6 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", "עברית",
/// "Русский", and "Srpski" in its script for Serbian (Latin).
static func nativeName(for identifier: String) -> String {
let language = Locale(identifier: identifier).language
let code = language.languageCode?.identifier ?? identifier
switch code {
case "nb", "no": return "Norsk"
case "nn": return "Nynorsk"
default: break
}
let own = Locale(identifier: language.script.map { "\(code)-\($0.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
}
}