TranslationSettingsView.swift (6371B)
1 // 2 // TranslationSettingsView.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-04-05. 6 // 7 8 import SwiftUI 9 10 struct TranslationSettingsView: View { 11 @ObservedObject var settings: UserSettingsStore 12 var damus_state: DamusState 13 14 @Environment(\.dismiss) var dismiss 15 16 var body: some View { 17 Form { 18 Section(NSLocalizedString("Translations", comment: "Section title for selecting the translation service.")) { 19 Picker(NSLocalizedString("Service", comment: "Prompt selection of translation service provider."), selection: $settings.translation_service) { 20 ForEach(TranslationService.allCases.filter({ damus_state.purple.enable_purple ? true : $0 != .purple }), id: \.self) { server in 21 Text(server.model.displayName) 22 .tag(server.model.tag) 23 } 24 } 25 26 if settings.translation_service == .purple && damus_state.purple.enable_purple { 27 NavigationLink(destination: DamusPurpleView(damus_state: damus_state)) { 28 Text("Configure Damus Purple", comment: "Button to allow Damus Purple to be configured") 29 } 30 } 31 32 if settings.translation_service == .libretranslate { 33 Picker(NSLocalizedString("Server", comment: "Prompt selection of LibreTranslate server to perform machine translations on notes"), selection: $settings.libretranslate_server) { 34 ForEach(LibreTranslateServer.allCases, id: \.self) { server in 35 Text(server.model.displayName) 36 .tag(server.model.tag) 37 } 38 } 39 40 if settings.libretranslate_server == .custom { 41 TextField(NSLocalizedString("URL", comment: "Example URL to LibreTranslate server"), text: $settings.libretranslate_url) 42 .disableAutocorrection(true) 43 .autocapitalization(UITextAutocapitalizationType.none) 44 } 45 46 SecureField(NSLocalizedString("API Key (optional)", comment: "Prompt for optional entry of API Key to use translation server."), text: $settings.libretranslate_api_key) 47 .disableAutocorrection(true) 48 .disabled(settings.translation_service != .libretranslate) 49 .autocapitalization(UITextAutocapitalizationType.none) 50 } 51 52 if settings.translation_service == .deepl { 53 Picker(NSLocalizedString("Plan", comment: "Prompt selection of DeepL subscription plan to perform machine translations on notes"), selection: $settings.deepl_plan) { 54 ForEach(DeepLPlan.allCases, id: \.self) { server in 55 Text(server.model.displayName) 56 .tag(server.model.tag) 57 } 58 } 59 60 SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.deepl_api_key) 61 .disableAutocorrection(true) 62 .disabled(settings.translation_service != .deepl) 63 .autocapitalization(UITextAutocapitalizationType.none) 64 65 if settings.deepl_api_key == "" { 66 Link(NSLocalizedString("Get API Key", comment: "Button to navigate to DeepL website to get a translation API key."), destination: URL(string: "https://www.deepl.com/pro-api")!) 67 } 68 } 69 70 if settings.translation_service == .nokyctranslate { 71 SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.nokyctranslate_api_key) 72 .disableAutocorrection(true) 73 .disabled(settings.translation_service != .nokyctranslate) 74 .autocapitalization(UITextAutocapitalizationType.none) 75 76 if settings.nokyctranslate_api_key == "" { 77 Link(NSLocalizedString("Get API Key with BTC/Lightning", comment: "Button to navigate to nokyctranslate website to get a translation API key."), destination: URL(string: "https://nokyctranslate.com")!) 78 } 79 } 80 81 if settings.translation_service == .winetranslate { 82 SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.winetranslate_api_key) 83 .disableAutocorrection(true) 84 .disabled(settings.translation_service != .winetranslate) 85 .autocapitalization(UITextAutocapitalizationType.none) 86 87 if settings.winetranslate_api_key == "" { 88 Link(NSLocalizedString("Get API Key with BTC/Lightning", comment: "Button to navigate to translate.nostr.wine to get a translation API key."), destination: URL(string: "https://translate.nostr.wine")!) 89 } 90 } 91 92 if settings.translation_service != .none { 93 Toggle(NSLocalizedString("Automatically translate notes", comment: "Toggle to automatically translate notes."), isOn: $settings.auto_translate) 94 .toggleStyle(.switch) 95 96 /* 97 Toggle(NSLocalizedString("Translate DMs", comment: "Toggle to translate direct messages."), isOn: $settings.translate_dms) 98 .toggleStyle(.switch) 99 */ 100 } 101 } 102 } 103 .navigationTitle(NSLocalizedString("Translation", comment: "Navigation title for translation settings.")) 104 .onReceive(handle_notify(.switched_timeline)) { _ in 105 dismiss() 106 } 107 } 108 } 109 110 struct TranslationSettingsView_Previews: PreviewProvider { 111 static var previews: some View { 112 TranslationSettingsView(settings: UserSettingsStore(), damus_state: test_damus_state) 113 } 114 }