feat(settings): mother-tongue language routing table

Replace the single reverse-direction pair with a general routing table. The saved target is the mother tongue and the default target for every source; languageRoutes is an empty-by-default, source-unique list of source-to-target rules (for example Chinese -> English, Traditional Chinese -> English, German -> English). Rules apply only when the source is confidently detected; otherwise the mother tongue is used. Same-language rules are dropped.\n\nThe panel shows the effective direction and a banner explaining a rule, with a one-capture override back to the mother tongue; the panel picker is now a session-only override. Persistent rules are edited in Settings. The old autoSwitchTarget/fallbackTargetLanguage keys are ignored, so the default after migration is everything -> mother tongue.\n\nAdd LanguageRoute, generalize the direction policy and tests, add the rules UI, and update ADR-022, architecture, translation output, user guide, README and progress. Verified: make verify green; empty rules translate French -> German, a Chinese -> English rule produces Chinese (Simplified) -> English with the banner, and the rules table renders in Settings.
This commit is contained in:
schihei committed 2026-11-26 18:31:45 +01:00
1 parent 41f1cea769
commit 940abcc996
24 files changed
+454 -336

No files matched your search

@@ -1,13 +1,8 @@
import Foundation
import SprachmittlerEngines
private struct DecodedDirection {
let autoSwitch: Bool
let fallback: String
}
extension AppSettings {
public init(from decoder: any Decoder) throws {
public extension AppSettings {
init(from decoder: any Decoder) throws {
let defaults = AppSettings()
let container = try decoder.container(keyedBy: CodingKeys.self)
@@ -21,9 +16,9 @@ extension AppSettings {
let decodedTarget = try container.decodeIfPresent(String.self, forKey: .targetLanguage)
?? defaults.targetLanguage
targetLanguage = HyMT3Languages.resolve(decodedTarget)?.code ?? defaults.targetLanguage
let direction = try Self.decodeDirection(from: container, defaults: defaults, target: targetLanguage)
autoSwitchTarget = direction.autoSwitch
fallbackTargetLanguage = direction.fallback
languageRoutes = try AppSettings.normalizedRoutes(
container.decodeIfPresent([LanguageRoute].self, forKey: .languageRoutes) ?? []
)
recentTargetLanguages = try Self.decodeRecentTargets(from: container)
let limits = try Self.decodeLimits(from: container, defaults: defaults)
doubleCopyWindowMs = limits.window
@@ -60,15 +55,14 @@ extension AppSettings {
settingsVersion = Self.currentVersion
}
public func encode(to encoder: any Encoder) throws {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(endpoint, forKey: .endpoint)
try container.encode(allowRemoteEndpoint, forKey: .allowRemoteEndpoint)
try container.encode(model, forKey: .model)
try container.encode(fastModel, forKey: .fastModel)
try container.encode(targetLanguage, forKey: .targetLanguage)
try container.encode(autoSwitchTarget, forKey: .autoSwitchTarget)
try container.encode(fallbackTargetLanguage, forKey: .fallbackTargetLanguage)
try container.encode(languageRoutes, forKey: .languageRoutes)
try container.encode(recentTargetLanguages, forKey: .recentTargetLanguages)
try container.encode(doubleCopyWindowMs, forKey: .doubleCopyWindowMs)
try container.encode(maxCharactersLatin, forKey: .maxCharactersLatin)
@@ -93,21 +87,6 @@ extension AppSettings {
try container.encode(settingsVersion, forKey: .settingsVersion)
}
private static func decodeDirection(
from container: KeyedDecodingContainer<CodingKeys>,
defaults: AppSettings,
target: String
) throws -> DecodedDirection {
let autoSwitch = try container.decodeIfPresent(Bool.self, forKey: .autoSwitchTarget)
?? defaults.autoSwitchTarget
let code = try container.decodeIfPresent(String.self, forKey: .fallbackTargetLanguage)
?? defaults.fallbackTargetLanguage
return DecodedDirection(
autoSwitch: autoSwitch,
fallback: AppSettings.normalizedFallback(code, target: target)
)
}
private static func decodeRecentTargets(
from container: KeyedDecodingContainer<CodingKeys>
) throws -> [String] {