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.
48 lines
2.0 KiB
Swift
48 lines
2.0 KiB
Swift
import SprachmittlerText
|
|
|
|
/// The effective target for one translation, after an optional reverse-direction switch.
|
|
public struct TranslationDirection: Sendable, Equatable {
|
|
/// The language the request will translate into.
|
|
public let target: ModelLanguage
|
|
/// True when the configured target was replaced by the fallback target.
|
|
public let didSwitch: Bool
|
|
/// The configured target when `didSwitch` is true.
|
|
public let originalTarget: ModelLanguage?
|
|
|
|
/// Creates a direction.
|
|
public init(target: ModelLanguage, didSwitch: Bool, originalTarget: ModelLanguage?) {
|
|
self.target = target
|
|
self.didSwitch = didSwitch
|
|
self.originalTarget = originalTarget
|
|
}
|
|
}
|
|
|
|
/// Chooses the direction for a capture. Detection stays advisory: a switch needs the same
|
|
/// high-confidence agreement as the same-language advisory.
|
|
public enum TranslationDirectionPolicy {
|
|
/// Resolves the target.
|
|
///
|
|
/// - Parameters:
|
|
/// - configuredTarget: The user's saved target language.
|
|
/// - fallbackTarget: The language to use when the source is already the configured target.
|
|
/// - detection: The advisory source-language detection.
|
|
/// - autoSwitch: Whether the fallback is enabled.
|
|
/// - override: True after an explicit target choice for the current capture.
|
|
public static func resolve(
|
|
configuredTarget: ModelLanguage,
|
|
fallbackTarget: ModelLanguage?,
|
|
detection: LanguageDetection?,
|
|
autoSwitch: Bool,
|
|
override: Bool
|
|
) -> TranslationDirection {
|
|
guard autoSwitch, !override, let fallbackTarget,
|
|
fallbackTarget.code.caseInsensitiveCompare(configuredTarget.code) != .orderedSame,
|
|
let detection,
|
|
LanguageMatching.isSameLanguage(detection, target: configuredTarget)
|
|
else {
|
|
return TranslationDirection(target: configuredTarget, didSwitch: false, originalTarget: nil)
|
|
}
|
|
return TranslationDirection(target: fallbackTarget, didSwitch: true, originalTarget: configuredTarget)
|
|
}
|
|
}
|