commit c74d3e4938bbc1ea51115edb57932d48d2558f1e
parent df6911f9cba7768ea5b920a6516ac775b23718ba
Author: William Casarin <jb55@jb55.com>
Date: Sun, 30 Apr 2023 22:04:33 -0700
fix some translation bugs
Diffstat:
2 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift
@@ -60,7 +60,10 @@ struct TranslateView: View {
func translate() {
Task {
- let res = await translate_note(profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, event: event, settings: damus_state.settings, note_lang: translations_model.note_language)
+ guard let note_language = translations_model.note_language else {
+ return
+ }
+ let res = await translate_note(profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, event: event, settings: damus_state.settings, note_lang: note_language)
DispatchQueue.main.async {
self.translations_model.state = res
}
@@ -68,27 +71,30 @@ struct TranslateView: View {
}
func attempt_translation() {
- guard should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: self.translations_model.note_language) else {
+ guard should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: self.translations_model.note_language), damus_state.settings.auto_translate else {
return
}
translate()
}
+ func should_transl(_ note_lang: String) -> Bool {
+ should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: note_lang)
+ }
+
var body: some View {
Group {
switch self.translations_model.state {
case .havent_tried:
if damus_state.settings.auto_translate {
Text("")
- } else if should_translate(event: event, our_keypair: damus_state.keypair, settings: damus_state.settings, note_lang: translations_model.note_language ?? current_language()) {
- TranslateButton
+ } else if let note_lang = translations_model.note_language, should_transl(note_lang) {
+ TranslateButton
+ } else {
+ Text("")
}
case .translating:
- Text("Translating...", comment: "Text to display when waiting for the translation of a note to finish processing before showing it.")
- .foregroundColor(.gray)
- .font(.footnote)
- .padding([.top, .bottom], 10)
+ Text("")
case .translated(let translated):
let languageName = Locale.current.localizedString(forLanguageCode: translated.language)
TranslatedView(lang: languageName, artifacts: translated.artifacts)
@@ -118,9 +124,7 @@ struct TranslateView_Previews: PreviewProvider {
}
}
-func translate_note(profiles: Profiles, privkey: String?, event: NostrEvent, settings: UserSettingsStore, note_lang: String?) async -> TranslateStatus {
-
- let note_lang = note_lang ?? current_language()
+func translate_note(profiles: Profiles, privkey: String?, event: NostrEvent, settings: UserSettingsStore, note_lang: String) async -> TranslateStatus {
// If the note language is different from our preferred languages, send a translation request.
let translator = Translator(settings)
diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift
@@ -37,7 +37,7 @@ enum ImageMetaProcessState {
}
class TranslationModel: ObservableObject {
- var note_language: String?
+ @Published var note_language: String?
@Published var state: TranslateStatus
init(state: TranslateStatus) {
@@ -283,14 +283,14 @@ func should_translate(event: NostrEvent, our_keypair: Keypair, settings: UserSet
}
// we should start translating if we have auto_translate on
- return settings.auto_translate
+ return true
}
func should_preload_translation(event: NostrEvent, our_keypair: Keypair, current_status: TranslateStatus, settings: UserSettingsStore, note_lang: String?) -> Bool {
switch current_status {
case .havent_tried:
- return should_translate(event: event, our_keypair: our_keypair, settings: settings, note_lang: note_lang)
+ return should_translate(event: event, our_keypair: our_keypair, settings: settings, note_lang: note_lang) && settings.auto_translate
case .translating: return false
case .translated: return false
case .not_needed: return false
@@ -377,7 +377,7 @@ func preload_event(plan: PreloadPlan, profiles: Profiles, our_keypair: Keypair,
let note_language = plan.event.note_language(our_keypair.privkey) ?? current_language()
- if plan.load_translations {
+ if plan.load_translations, should_translate(event: plan.event, our_keypair: our_keypair, settings: settings, note_lang: note_language) {
translations = await translate_note(profiles: profiles, privkey: our_keypair.privkey, event: plan.event, settings: settings, note_lang: note_language)
}