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

@@ -21,10 +21,8 @@ public enum PanelPhase: Sendable, Equatable {
public struct PanelConfiguration: Sendable, Equatable {
/// The selected target language.
public var target: ModelLanguage
/// Whether the source-equals-target direction switches to `fallbackTarget`.
public var autoSwitchTarget: Bool
/// The fallback target used when the source is already the target.
public var fallbackTarget: ModelLanguage?
/// Source-to-target rules. Empty means everything goes to `target` (the mother tongue).
public var languageRoutes: [LanguageRoute]
/// The selected model tag.
public var model: String
/// The Ollama keep-alive value.
@@ -52,8 +50,7 @@ public struct PanelConfiguration: Sendable, Equatable {
translationFontSize: Int = 17,
renderMarkdown: Bool = true,
closeWithEscape: Bool = true,
autoSwitchTarget: Bool = true,
fallbackTarget: ModelLanguage? = HyMT3Languages.english,
languageRoutes: [LanguageRoute] = [],
recentTargets: [ModelLanguage] = []
) {
self.target = target
@@ -64,8 +61,7 @@ public struct PanelConfiguration: Sendable, Equatable {
self.translationFontSize = translationFontSize
self.renderMarkdown = renderMarkdown
self.closeWithEscape = closeWithEscape
self.autoSwitchTarget = autoSwitchTarget
self.fallbackTarget = fallbackTarget
self.languageRoutes = languageRoutes
self.recentTargets = recentTargets
}
}
@@ -96,8 +92,8 @@ public final class PanelViewModel {
public private(set) var configuration: PanelConfiguration
/// The target used by the current request, after an optional reverse-direction switch.
public private(set) var effectiveTarget: ModelLanguage
/// The saved target that was replaced by `effectiveTarget`, or nil.
public private(set) var autoSwitchedFrom: ModelLanguage?
/// The matched rule source when `effectiveTarget` differs from the mother tongue, or nil.
public private(set) var routedSource: ModelLanguage?
/// True after the user dismisses the same-language advisory.
public private(set) var sameLanguageDismissed = false
@@ -217,13 +213,12 @@ public final class PanelViewModel {
sourceLanguage = detection?.code
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: configuration.target,
fallbackTarget: configuration.fallbackTarget,
routes: configuration.languageRoutes,
detection: detection,
autoSwitch: configuration.autoSwitchTarget,
override: directionOverride
)
effectiveTarget = direction.target
autoSwitchedFrom = direction.didSwitch ? direction.originalTarget : nil
routedSource = direction.didRoute ? direction.source : nil
sameLanguageDismissed = directionOverride
phase = .translating
}
@@ -262,7 +257,7 @@ public final class PanelViewModel {
timing = nil
sourceLanguage = nil
effectiveTarget = configuration.target
autoSwitchedFrom = nil
routedSource = nil
directionOverride = false
}
@@ -271,7 +266,7 @@ public final class PanelViewModel {
guard language.code != effectiveTarget.code else { return }
configuration.target = language
effectiveTarget = language
autoSwitchedFrom = nil
routedSource = nil
directionOverride = true
onTargetChanged(language)
if let lastCaptured {
@@ -283,7 +278,7 @@ public final class PanelViewModel {
public func overrideTarget(_ language: ModelLanguage) async {
guard language.code != effectiveTarget.code else { return }
effectiveTarget = language
autoSwitchedFrom = nil
routedSource = nil
directionOverride = true
sameLanguageDismissed = true
if let lastCaptured {
@@ -326,7 +321,7 @@ public final class PanelViewModel {
self.configuration = configuration
if targetChanged || source == nil {
effectiveTarget = configuration.target
autoSwitchedFrom = nil
routedSource = nil
directionOverride = false
}
}
@@ -345,7 +340,7 @@ public final class PanelViewModel {
isReplacing = false
sameLanguageDismissed = false
effectiveTarget = configuration.target
autoSwitchedFrom = nil
routedSource = nil
directionOverride = false
lastCaptured = nil
}