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.
53 lines
2.1 KiB
Swift
53 lines
2.1 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 a language rule changed the target.
|
|
public let didRoute: Bool
|
|
/// The rule source when `didRoute` is true.
|
|
public let source: ModelLanguage?
|
|
|
|
/// Creates a direction.
|
|
public init(target: ModelLanguage, didRoute: Bool, source: ModelLanguage?) {
|
|
self.target = target
|
|
self.didRoute = didRoute
|
|
self.source = source
|
|
}
|
|
}
|
|
|
|
/// 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 mother tongue, used when no rule matches.
|
|
/// - routes: The source-to-target rules.
|
|
/// - detection: The advisory source-language detection.
|
|
/// - override: True after an explicit target choice for the current capture.
|
|
public static func resolve(
|
|
configuredTarget: ModelLanguage,
|
|
routes: [LanguageRoute],
|
|
detection: LanguageDetection?,
|
|
override: Bool
|
|
) -> TranslationDirection {
|
|
guard !override, let detection else {
|
|
return TranslationDirection(target: configuredTarget, didRoute: false, source: nil)
|
|
}
|
|
for route in routes {
|
|
guard let source = route.sourceLanguage,
|
|
let target = route.targetLanguage,
|
|
target.code.caseInsensitiveCompare(source.code) != .orderedSame,
|
|
target.code.caseInsensitiveCompare(configuredTarget.code) != .orderedSame,
|
|
LanguageMatching.isLanguage(detection, source)
|
|
else {
|
|
continue
|
|
}
|
|
return TranslationDirection(target: target, didRoute: true, source: source)
|
|
}
|
|
return TranslationDirection(target: configuredTarget, didRoute: false, source: nil)
|
|
}
|
|
}
|