feat(panel): optional reverse direction for target-language text

Add opt-in automatic reverse translation for a two-language pair. autoSwitchTarget defaults on with fallbackTargetLanguage English (or German when the target is English); the saved target and the effective request target are separate. Confident source-equals-target text uses the fallback, the header and a panel banner show the effective direction, and the banner can override the direction for one capture without changing the saved target. The language picker still changes the saved target, and changing the target to the fallback swaps the pair. JSON/HTML/code handling is unchanged.\n\nFilter near-zero NLLanguageRecognizer hypotheses so the existing high-confidence same-language rule works for real text. Add resolver, settings, view-model, detector and persistence tests; document ADR-022, the panel, settings and user guide. Verified: make verify green; German text with target German/fallback English renders German → English with the banner, English text stays English → German, a real click on the override shows German → German, and a new German capture switches back to English.
This commit is contained in:
schihei committed 2026-11-26 17:34:11 +01:00
1 parent 7c9cece201
commit 41f1cea769
24 files changed
+723 -148

No files matched your search

@@ -0,0 +1,117 @@
import SprachmittlerEngines
import SprachmittlerText
import Testing
@Suite("Translation direction")
struct TranslationDirectionTests {
private let german = HyMT3Languages.german
private let english = HyMT3Languages.english
private func detection(
_ code: String,
confidence: Double = 0.9,
alternatives: [String]? = nil
) -> LanguageDetection {
LanguageDetection(code: code, confidence: confidence, alternatives: alternatives ?? [code])
}
@Test func keepsTheConfiguredTargetWhenTheSourceIsDifferent() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("en"),
autoSwitch: true,
override: false
)
#expect(direction == TranslationDirection(target: german, didSwitch: false, originalTarget: nil))
}
@Test func switchesWhenTheSourceIsConfidentlyTheTarget() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de"),
autoSwitch: true,
override: false
)
#expect(direction == TranslationDirection(target: english, didSwitch: true, originalTarget: german))
}
@Test func doesNotSwitchWhenDisabled() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de"),
autoSwitch: false,
override: false
)
#expect(direction.target == german)
#expect(!direction.didSwitch)
}
@Test func doesNotSwitchAfterAManualOverride() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de"),
autoSwitch: true,
override: true
)
#expect(direction.target == german)
#expect(!direction.didSwitch)
}
@Test func doesNotSwitchWithLowConfidence() {
let direction = TranslationDirectionPolicy.resolve(
configuredTarget: german,
fallbackTarget: english,
detection: detection("de", confidence: 0.49),
autoSwitch: true,
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
)
}
}