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

@@ -15,103 +15,111 @@ struct TranslationDirectionTests {
LanguageDetection(code: code, confidence: confidence, alternatives: alternatives ?? [code])
}
@Test func keepsTheConfiguredTargetWhenTheSourceIsDifferent() {
@Test func usesTheMotherTongueWithoutRules() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
routes: [],
detection: detection("en"),
autoSwitch: true,
override: false
)
#expect(direction == TranslationDirection(target: german, didSwitch: false, originalTarget: nil))
#expect(direction == TranslationDirection(target: german, didRoute: false, source: nil))
}
@Test func switchesWhenTheSourceIsConfidentlyTheTarget() {
@Test func appliesAMatchingRule() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
routes: [LanguageRoute(source: "de", target: "en")],
detection: detection("de"),
autoSwitch: true,
override: false
)
#expect(direction == TranslationDirection(target: english, didSwitch: true, originalTarget: german))
#expect(direction == TranslationDirection(target: english, didRoute: true, source: german))
}
@Test func doesNotSwitchWhenDisabled() {
@Test func appliesAnArbitrarySourceRule() throws {
let french = try #require(HyMT3Languages.resolve("fr"))
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de"),
autoSwitch: false,
routes: [LanguageRoute(source: "fr", target: "en")],
detection: detection("fr"),
override: false
)
#expect(direction.target == english)
#expect(direction.didRoute)
#expect(direction.source == french)
}
@Test func supportsChineseToEnglish() throws {
let chinese = try #require(HyMT3Languages.resolve("zh"))
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
routes: [LanguageRoute(source: "zh", target: "en")],
detection: detection("zh-Hans"),
override: false
)
#expect(direction.target == english)
#expect(direction.source == chinese)
}
@Test func keepsTheMotherTongueWhenTheRuleTargetIsTheMotherTongue() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
routes: [LanguageRoute(source: "en", target: "de")],
detection: detection("en"),
override: false
)
#expect(direction.target == german)
#expect(!direction.didSwitch)
#expect(!direction.didRoute)
}
@Test func doesNotSwitchAfterAManualOverride() {
@Test func ignoresARuleWhoseTargetEqualsItsSource() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
routes: [LanguageRoute(source: "de", target: "de")],
detection: detection("de"),
override: false
)
#expect(direction.target == german)
#expect(!direction.didRoute)
}
@Test func ignoresRulesOnLowConfidence() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
routes: [LanguageRoute(source: "de", target: "en")],
detection: detection("de", confidence: 0.49),
override: false
)
#expect(direction.target == german)
}
@Test func ignoresRulesWhenTheAlternativesDisagree() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
routes: [LanguageRoute(source: "de", target: "en")],
detection: detection("de", alternatives: ["de", "en"]),
override: false
)
#expect(direction.target == german)
}
@Test func manualOverrideWins() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
routes: [LanguageRoute(source: "de", target: "en")],
detection: detection("de"),
autoSwitch: true,
override: true
)
#expect(direction.target == german)
#expect(!direction.didSwitch)
#expect(!direction.didRoute)
}
@Test func doesNotSwitchWithLowConfidence() {
@Test func noDetectionUsesTheMotherTongue() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de", confidence: 0.49),
autoSwitch: true,
routes: [LanguageRoute(source: "de", target: "en")],
detection: nil,
override: false
)
#expect(direction.target == german)
}
@Test func doesNotSwitchWhenTheAlternativesDisagree() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de", alternatives: ["de", "en"]),
autoSwitch: true,
override: false
)
#expect(direction.target == german)
}
@Test func doesNotSwitchWhenTheFallbackIsTheTarget() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: german,
detection: detection("de"),
autoSwitch: true,
override: false
)
#expect(direction.target == german)
}
@Test func doesNotSwitchWithoutAFallbackOrDetection() {
#expect(
TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: nil,
detection: detection("de"),
autoSwitch: true,
override: false
).target == german
)
#expect(
TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: nil,
autoSwitch: true,
override: false
).target == german
)
}
}