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

+2
View File
@@ -217,6 +217,8 @@ final class AppEnvironment {
translationFontSize: settings.translationFontSize,
renderMarkdown: settings.renderMarkdown,
closeWithEscape: settings.closeWithEscape,
autoSwitchTarget: settings.autoSwitchTarget,
fallbackTarget: settings.fallbackTargetModel,
recentTargets: settings.recentTargets
)
}
+7 -2
View File
@@ -5,6 +5,7 @@ import SwiftUI
struct LanguagePickerView: View {
@Binding var selection: ModelLanguage
var recent: [ModelLanguage] = []
var excluding: String?
@Environment(\.dismiss) private var dismiss
@State private var query = ""
@@ -45,8 +46,12 @@ struct LanguagePickerView: View {
let all = LanguageCatalogue.all.sorted {
$0.localizedDisplayName.localizedCaseInsensitiveCompare($1.localizedDisplayName) == .orderedAscending
}
guard !query.trimmingCharacters(in: .whitespaces).isEmpty else { return all }
return all.filter {
let available = all.filter { language in
guard let excluding else { return true }
return language.code.caseInsensitiveCompare(excluding) != .orderedSame
}
guard !query.trimmingCharacters(in: .whitespaces).isEmpty else { return available }
return available.filter {
$0.localizedDisplayName.localizedCaseInsensitiveContains(query)
|| $0.modelName.localizedCaseInsensitiveContains(query)
|| $0.code.localizedCaseInsensitiveContains(query)
+26 -3
View File
@@ -36,7 +36,7 @@ extension PanelView {
MarkdownTranslationView(
markdown: model.translation,
fontSize: model.configuration.translationFontSize,
isRTL: model.configuration.target.isRTL
isRTL: model.effectiveTarget.isRTL
)
} else {
Text(translationText)
@@ -46,7 +46,7 @@ extension PanelView {
.foregroundStyle(model.translation.isEmpty ? .secondary : .primary)
.environment(
\.layoutDirection,
model.configuration.target.isRTL ? .rightToLeft : .leftToRight
model.effectiveTarget.isRTL ? .rightToLeft : .leftToRight
)
.accessibilityLabel(String(localized: "Translation"))
}
@@ -66,11 +66,34 @@ extension PanelView {
}
}
private func autoSwitchMessage(from original: ModelLanguage) -> String {
let source = original.localizedDisplayName
let target = model.effectiveTarget.localizedDisplayName
return String(localized: "Detected \(source). Translating to \(target).")
}
@ViewBuilder var warningArea: some View {
if let original = model.autoSwitchedFrom {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "arrow.left.arrow.right.circle.fill")
.foregroundStyle(.blue)
.accessibilityHidden(true)
Text(autoSwitchMessage(from: original))
.font(.callout)
Spacer()
Button(String(localized: "Translate to \(original.localizedDisplayName)")) {
Task { await model.overrideTarget(original) }
}
.controlSize(.small)
}
.padding(8)
.background(.blue.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
}
if model.sameLanguageWarning != nil {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "info.circle.fill").foregroundStyle(.blue)
Text("This text already appears to be in \(model.configuration.target.localizedDisplayName).")
Text("This text already appears to be in \(model.effectiveTarget.localizedDisplayName).")
.font(.callout)
Spacer()
Button(String(localized: "Translate anyway")) {
+3 -3
View File
@@ -62,7 +62,7 @@ struct PanelView: View {
VStack(alignment: .leading, spacing: 1) {
Text(String(localized: "Translate"))
.font(.headline)
Text("\(sourceLanguageLabel) → \(model.configuration.target.localizedDisplayName)")
Text("\(sourceLanguageLabel) → \(model.effectiveTarget.localizedDisplayName)")
.font(.caption)
.foregroundStyle(.secondary)
}
@@ -75,14 +75,14 @@ struct PanelView: View {
Button {
showLanguagePicker.toggle()
} label: {
Label(model.configuration.target.localizedDisplayName, systemImage: "globe")
Label(model.effectiveTarget.localizedDisplayName, systemImage: "globe")
.labelStyle(.titleAndIcon)
}
.accessibilityLabel(String(localized: "Target language"))
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
LanguagePickerView(
selection: Binding(
get: { model.configuration.target },
get: { model.effectiveTarget },
set: { language in
Task { await model.setTarget(language) }
}
@@ -104,3 +104,10 @@
"Render bold, italic, headings, lists, code and links when the source is Markdown." = "Fett, kursiv, Überschriften, Listen, Code und Links anzeigen, wenn die Quelle Markdown ist.";
"Close with Esc without clicking" = "Mit Esc schließen, ohne zu klicken";
"The panel takes keyboard focus while it is visible. The source app stays frontmost." = "Das Panel übernimmt die Tastatureingabe, solange es sichtbar ist. Die Quell-App bleibt im Vordergrund.";
"Translate the other direction when the text is already in the target language" = "In die andere Richtung übersetzen, wenn der Text bereits in der Zielsprache ist";
"Translate the other direction" = "In die andere Richtung übersetzen";
"Fallback target language" = "Ausweich-Zielsprache";
"Used only when the text is confidently detected as the target language." = "Wird nur verwendet, wenn der Text sicher als Zielsprache erkannt wird.";
"Detected %@. Translating to %@." = "%@ erkannt. Übersetzung nach %@.";
"Translate to %@" = "Nach %@ übersetzen";
"This text already appears to be in %@ (detected %@)." = "Dieser Text scheint bereits auf %@ zu sein (erkannt: %@).";
@@ -104,3 +104,10 @@
"Render bold, italic, headings, lists, code and links when the source is Markdown." = "Render bold, italic, headings, lists, code and links when the source is Markdown.";
"Close with Esc without clicking" = "Close with Esc without clicking";
"The panel takes keyboard focus while it is visible. The source app stays frontmost." = "The panel takes keyboard focus while it is visible. The source app stays frontmost.";
"Translate the other direction when the text is already in the target language" = "Translate the other direction when the text is already in the target language";
"Translate the other direction" = "Translate the other direction";
"Fallback target language" = "Fallback target language";
"Used only when the text is confidently detected as the target language." = "Used only when the text is confidently detected as the target language.";
"Detected %@. Translating to %@." = "Detected %@. Translating to %@.";
"Translate to %@" = "Translate to %@";
"This text already appears to be in %@ (detected %@)." = "This text already appears to be in %@ (detected %@).";
+54
View File
@@ -0,0 +1,54 @@
import SprachmittlerCore
import SprachmittlerEngines
import SwiftUI
/// The optional reverse-direction controls in Settings ▸ General.
struct FallbackTargetSettings: View {
@Binding var autoSwitchTarget: Bool
@Binding var fallbackTargetLanguage: String
let targetLanguage: String
let recentTargets: [ModelLanguage]
@State private var showLanguagePicker = false
private var fallbackTarget: ModelLanguage {
HyMT3Languages.resolve(fallbackTargetLanguage) ?? HyMT3Languages.english
}
var body: some View {
Toggle(
String(
localized: "Translate the other direction when the text is already in the target language"
),
isOn: $autoSwitchTarget
)
.accessibilityLabel(String(localized: "Translate the other direction"))
if autoSwitchTarget {
LabeledContent(String(localized: "Fallback target language")) {
Button {
showLanguagePicker.toggle()
} label: {
HStack {
Text(fallbackTarget.localizedDisplayName)
Image(systemName: "chevron.up.chevron.down")
.font(.caption)
}
}
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
LanguagePickerView(
selection: Binding(
get: { fallbackTarget },
set: { fallbackTargetLanguage = $0.code }
),
recent: recentTargets,
excluding: targetLanguage
)
}
.accessibilityLabel(String(localized: "Fallback target language"))
}
Text(String(localized: "Used only when the text is confidently detected as the target language."))
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
+5
View File
@@ -123,6 +123,11 @@ struct SettingsView: View {
)
.font(.caption)
.foregroundStyle(.secondary)
FallbackTargetSettings(
autoSwitchTarget: $draft.autoSwitchTarget,
fallbackTargetLanguage: $draft.fallbackTargetLanguage,
targetLanguage: draft.targetLanguage, recentTargets: draft.recentTargets
)
}
Section(String(localized: "Double copy")) {