hex/ios/HEXShared/FullKeyboard/KeyboardLanguage.swift
Ryan Vogel bb5875cea8 iOS: Norwegian, Spanish and German full-keyboard layouts
Testers asked for the Norwegian layout and, with it, Spanish and
German. The layout follows the dictation language unless set.

What changed:
- KeyboardLanguage holds each layout's letter rows, long-press accents
  and spelling dictionary: Norsk (a-ring ending row 1, o-slash and ae
  row 2), Espanol (n-tilde ending row 2), Deutsch (QWERTZ, u/o/a-umlaut,
  sharp s on a held S), as Apple lays them out. Adding a language is a
  new case with those three filled in.
- KeyboardLayout takes the language. Rows of 11 keys narrow only the
  character keys; shift, delete and the bottom row keep their English
  (10-key) sizes, and row 3's letters sit centred between shift and
  delete. letterWidth stays the 10-key width; characterWidth is new.
- Experiments > Full keyboard > Keyboard layout: Automatic, English,
  Norsk, Espanol, Deutsch (settings.keyboardLayout). Automatic follows
  the dictation language (no layout yet for Hebrew and others: English),
  else the first iPhone keyboard or preferred language with a layout. A
  pick in the keyboard's language switch changes the layout at once.
- Autocorrect uses the layout's UITextChecker dictionary (nb, es, de);
  without one it offers nothing but your text replacements instead of
  correcting into English. The English contractions and typo list
  (i -> I) apply only to English; neighbouring-key costs follow the
  layout. LanguageSettings.effectiveDictationLanguage now prefers what
  Automatic picks over the last language dictation ran in.
- Typing logs record the layout's language; the snapshot stage takes
  --keyboard-layout, follows --dictation-language and --accent-key.
- TypingLogTests pins English, since FullKeyboardView now reads the
  App Group's dictation language.

Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2):
206 tests, 0 failures, 1 skipped (the Apple Speech clip test), with
layout tests for every language at 320-440 pt and landscape (rows,
11-key widths, hit-frame tiling, balloons, accent menus) and autocorrect
tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds;
light and dark screenshots of each layout, its accents, shift, the
settings row and the dictation bar's re-shot trigger checked by eye. The
simulator has dictionaries for nb, es and de. Accent lists are from
Apple's layouts as remembered, not checked on a device; the 123 and #+=
pages are English's in every language.
2026-11-20 07:52:48 -05:00

134 lines
5.1 KiB
Swift

import Foundation
/// The full keyboard's letters. Each language is its letter rows, its long-press accents,
/// and the spelling dictionary its autocorrect uses, matching Apple's keyboard for it;
/// another language is a new case with those filled in.
nonisolated enum KeyboardLanguage: String, CaseIterable, Sendable {
case english = "en"
case norwegian = "nb"
case spanish = "es"
case german = "de"
/// Its own name, as Apple's keyboard list shows it.
var displayName: String {
switch self {
case .english: "English"
case .norwegian: "Norsk"
case .spanish: "Español"
case .german: "Deutsch"
}
}
/// The three letter rows, lowercase. Rows 1 and 2 may hold 11 keys (Apple's Nordic and
/// German layouts); row 3 sits between shift and delete.
var letterRows: [[String]] {
let rows = switch self {
case .english: ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
case .norwegian: ["qwertyuiopå", "asdfghjkløæ", "zxcvbnm"]
case .spanish: ["qwertyuiop", "asdfghjklñ", "zxcvbnm"]
case .german: ["qwertzuiopü", "asdfghjklöä", "yxcvbnm"]
}
return rows.map { $0.map(String.init) }
}
/// Letters held for their accents, base letter first, where this language's keyboard
/// differs from `AccentCatalog`'s shared table: letters with their own key drop out of
/// the vowels' lists.
var accentOverrides: [String: [String]] {
switch self {
case .english:
[:]
case .norwegian:
// Æ and Ø offer the Swedish and German Ä and Ö.
[
"a": ["a", "à", "á", "â", "ä", "ã", "ā"],
"o": ["o", "ô", "ö", "ò", "ó", "œ", "ō", "õ"],
"æ": ["æ", "ä"],
"ø": ["ø", "ö"],
]
case .spanish:
// Spanish accents first; ª and º with A and O.
[
"a": ["a", "á", "à", "ä", "â", "ã", "å", "ą", "æ", "ā", "ª"],
"e": ["e", "é", "è", "ë", "ê", "ę", "ė", "ē"],
"i": ["i", "í", "ï", "ì", "î", "į", "ī"],
"o": ["o", "ó", "ò", "ö", "ô", "õ", "ø", "œ", "ō", "º"],
"u": ["u", "ú", "ü", "ù", "û", "ū"],
"n": ["n", "ñ", "ń"],
]
case .german:
// ß stays on a held S (the shared table).
[
"a": ["a", "à", "á", "â", "æ", "ã", "å", "ā"],
"o": ["o", "ô", "ò", "ó", "œ", "ø", "ō", "õ"],
"u": ["u", "û", "ù", "ú", "ū"],
]
}
}
/// `UITextChecker` language codes to try, best first.
var spellCheckCandidates: [String] {
switch self {
case .english: ["en_US", "en"]
case .norwegian: ["nb", "nb_NO", "no", "no_NO"]
case .spanish: ["es", "es_ES", "es_MX"]
case .german: ["de", "de_DE"]
}
}
/// The first candidate the iPhone has a dictionary for, or nil: then autocorrect and
/// suggestions stay off rather than "correcting" this language into another.
func spellCheckLanguage(available: [String]) -> String? {
spellCheckCandidates.first { available.contains($0) }
}
/// Whether the built-in English contractions and typo list apply.
var usesEnglishFixes: Bool { self == .english }
/// Each letter's (row, column) for autocorrect's neighbouring-key cost, with rows 2
/// and 3 shifted as the keys are.
var keyCoordinates: [Character: (Double, Double)] {
var map: [Character: (Double, Double)] = [:]
let offsets = [0.0, 0.5, 1.5]
for (row, letters) in letterRows.enumerated() {
for (column, letter) in letters.enumerated() {
guard let character = letter.first else { continue }
map[character] = (Double(row), Double(column) + offsets[row])
}
}
return map
}
/// The layout for a language identifier ("nb-NO", "no", "nn_NO", "es-MX"), if any.
static func matching(_ identifier: String) -> KeyboardLanguage? {
let code = identifier.lowercased()
.split(whereSeparator: { $0 == "-" || $0 == "_" })
.first.map(String.init) ?? ""
switch code {
case "nb", "no", "nn": return .norwegian
case "en": return .english
case "es": return .spanish
case "de": return .german
default: return nil
}
}
/// Automatic follows the dictation language: its layout, or English for a language
/// without one (Hebrew, French). Before any dictation language is known, the first
/// iPhone keyboard, then preferred language, that has a layout.
static func automatic(
dictationLanguage: String?,
inputModes: [String],
preferredLanguages: [String]
) -> KeyboardLanguage {
if let dictationLanguage {
return matching(dictationLanguage) ?? .english
}
for identifier in inputModes + preferredLanguages {
if let language = matching(identifier) {
return language
}
}
return .english
}
}