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:
1 parent
7c9cece201
commit
41f1cea769
24 files changed
+723
-148
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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")) {
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user