Add nokyctranslate translation option

Changelog-Added: Add nokyctranslate translation option
Closes: #946
This commit is contained in:
symbsrcool
2023-04-16 22:29:40 -04:00
committed by William Casarin
parent 2048e68d67
commit 6ac68b5a73
4 changed files with 76 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
case none
case libretranslate
case deepl
case nokyctranslate
var model: Model {
switch self {
@@ -40,6 +41,8 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
return .init(tag: self.rawValue, displayName: NSLocalizedString("LibreTranslate (Open Source)", comment: "Dropdown option for selecting LibreTranslate as the translation service."))
case .deepl:
return .init(tag: self.rawValue, displayName: NSLocalizedString("DeepL (Proprietary, Higher Accuracy)", comment: "Dropdown option for selecting DeepL as the translation service."))
case .nokyctranslate:
return .init(tag: self.rawValue, displayName: NSLocalizedString("NoKYCTranslate.com (Prepay with BTC)", comment: "Dropdown option for selecting NoKYCTranslate.com as the translation service."))
}
}

View File

@@ -196,6 +196,20 @@ class UserSettingsStore: ObservableObject {
}
}
}
@Published var nokyctranslate_api_key: String {
didSet {
do {
if nokyctranslate_api_key == "" {
try clearNoKYCTranslateApiKey()
} else {
try saveNoKYCTranslateApiKey(nokyctranslate_api_key)
}
} catch {
// No-op.
}
}
}
init() {
do {
@@ -203,6 +217,13 @@ class UserSettingsStore: ObservableObject {
} catch {
deepl_api_key = ""
}
do {
nokyctranslate_api_key = try Vault.getPrivateKey(keychainConfiguration: DamusNoKYCTranslateKeychainConfiguration())
} catch {
nokyctranslate_api_key = ""
}
}
private func saveLibreTranslateApiKey(_ apiKey: String) throws {
@@ -213,6 +234,14 @@ class UserSettingsStore: ObservableObject {
try Vault.deletePrivateKey(keychainConfiguration: DamusLibreTranslateKeychainConfiguration())
}
private func saveNoKYCTranslateApiKey(_ apiKey: String) throws {
try Vault.savePrivateKey(apiKey, keychainConfiguration: DamusNoKYCTranslateKeychainConfiguration())
}
private func clearNoKYCTranslateApiKey() throws {
try Vault.deletePrivateKey(keychainConfiguration: DamusNoKYCTranslateKeychainConfiguration())
}
private func saveDeepLApiKey(_ apiKey: String) throws {
try Vault.savePrivateKey(apiKey, keychainConfiguration: DamusDeepLKeychainConfiguration())
}
@@ -229,6 +258,8 @@ class UserSettingsStore: ObservableObject {
return URLComponents(string: libretranslate_url) != nil
case .deepl:
return deepl_api_key != ""
case .nokyctranslate:
return nokyctranslate_api_key != ""
}
}
}
@@ -245,7 +276,12 @@ struct DamusDeepLKeychainConfiguration: KeychainConfiguration {
var accountName = "deepl_apikey"
}
struct DamusNoKYCTranslateKeychainConfiguration: KeychainConfiguration {
var serviceName = "damus"
var accessGroup: String? = nil
var accountName = "nokyctranslate_apikey"
}
func pk_setting_key(_ pubkey: String, key: String) -> String {
return "\(pubkey)_\(key)"
}