damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

TranslationSettingsView.swift (6720B)


      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                 Toggle(NSLocalizedString("Show only preferred languages on Universe feed", comment: "Toggle to show notes that are only in the device's preferred languages on the Universe feed and hide notes that are in other languages."), isOn: $settings.show_only_preferred_languages)
     20                     .toggleStyle(.switch)
     21 
     22                 Picker(NSLocalizedString("Service", comment: "Prompt selection of translation service provider."), selection: $settings.translation_service) {
     23                     ForEach(TranslationService.allCases.filter({ damus_state.purple.enable_purple ? true : $0 != .purple }), id: \.self) { server in
     24                         Text(server.model.displayName)
     25                             .tag(server.model.tag)
     26                     }
     27                 }
     28                 
     29                 if settings.translation_service == .purple && damus_state.purple.enable_purple {
     30                     NavigationLink(destination: DamusPurpleView(damus_state: damus_state)) {
     31                         Text(NSLocalizedString("Configure Damus Purple", comment: "Button to allow Damus Purple to be configured"))
     32                     }
     33                 }
     34 
     35                 if settings.translation_service == .libretranslate {
     36                     Picker(NSLocalizedString("Server", comment: "Prompt selection of LibreTranslate server to perform machine translations on notes"), selection: $settings.libretranslate_server) {
     37                         ForEach(LibreTranslateServer.allCases, id: \.self) { server in
     38                             Text(server.model.displayName)
     39                                 .tag(server.model.tag)
     40                         }
     41                     }
     42 
     43                     if settings.libretranslate_server == .custom {
     44                         TextField(NSLocalizedString("URL", comment: "Example URL to LibreTranslate server"), text: $settings.libretranslate_url)
     45                             .disableAutocorrection(true)
     46                             .autocapitalization(UITextAutocapitalizationType.none)
     47                     }
     48 
     49                     SecureField(NSLocalizedString("API Key (optional)", comment: "Prompt for optional entry of API Key to use translation server."), text: $settings.libretranslate_api_key)
     50                         .disableAutocorrection(true)
     51                         .disabled(settings.translation_service != .libretranslate)
     52                         .autocapitalization(UITextAutocapitalizationType.none)
     53                 }
     54 
     55                 if settings.translation_service == .deepl {
     56                     Picker(NSLocalizedString("Plan", comment: "Prompt selection of DeepL subscription plan to perform machine translations on notes"), selection: $settings.deepl_plan) {
     57                         ForEach(DeepLPlan.allCases, id: \.self) { server in
     58                             Text(server.model.displayName)
     59                                 .tag(server.model.tag)
     60                         }
     61                     }
     62 
     63                     SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.deepl_api_key)
     64                         .disableAutocorrection(true)
     65                         .disabled(settings.translation_service != .deepl)
     66                         .autocapitalization(UITextAutocapitalizationType.none)
     67 
     68                     if settings.deepl_api_key == "" {
     69                         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")!)
     70                     }
     71                 }
     72 
     73                 if settings.translation_service == .nokyctranslate {
     74                     SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.nokyctranslate_api_key)
     75                         .disableAutocorrection(true)
     76                         .disabled(settings.translation_service != .nokyctranslate)
     77                         .autocapitalization(UITextAutocapitalizationType.none)
     78                     
     79                     if settings.nokyctranslate_api_key == "" {
     80                         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")!)
     81                     }
     82                 }
     83 
     84                 if settings.translation_service == .winetranslate {
     85                     SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for required entry of API Key to use translation server."), text: $settings.winetranslate_api_key)
     86                         .disableAutocorrection(true)
     87                         .disabled(settings.translation_service != .winetranslate)
     88                         .autocapitalization(UITextAutocapitalizationType.none)
     89 
     90                     if settings.winetranslate_api_key == "" {
     91                         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")!)
     92                     }
     93                 }
     94                 
     95                 if settings.translation_service != .none {
     96                     Toggle(NSLocalizedString("Automatically translate notes", comment: "Toggle to automatically translate notes."), isOn: $settings.auto_translate)
     97                         .toggleStyle(.switch)
     98                     
     99                     /*
    100                     Toggle(NSLocalizedString("Translate DMs", comment: "Toggle to translate direct messages."), isOn: $settings.translate_dms)
    101                         .toggleStyle(.switch)
    102                      */
    103                 }
    104             }
    105         }
    106         .navigationTitle(NSLocalizedString("Translation", comment: "Navigation title for translation settings."))
    107         .onReceive(handle_notify(.switched_timeline)) { _ in
    108             dismiss()
    109         }
    110     }
    111 }
    112 
    113 struct TranslationSettingsView_Previews: PreviewProvider {
    114     static var previews: some View {
    115         TranslationSettingsView(settings: UserSettingsStore(), damus_state: test_damus_state)
    116     }
    117 }