Add nokyctranslate translation option
Changelog-Added: Add nokyctranslate translation option Closes: #946
This commit is contained in:
committed by
William Casarin
parent
2048e68d67
commit
6ac68b5a73
@@ -31,6 +31,7 @@ enum TranslationService: String, CaseIterable, Identifiable, StringCodable {
|
|||||||
case none
|
case none
|
||||||
case libretranslate
|
case libretranslate
|
||||||
case deepl
|
case deepl
|
||||||
|
case nokyctranslate
|
||||||
|
|
||||||
var model: Model {
|
var model: Model {
|
||||||
switch self {
|
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."))
|
return .init(tag: self.rawValue, displayName: NSLocalizedString("LibreTranslate (Open Source)", comment: "Dropdown option for selecting LibreTranslate as the translation service."))
|
||||||
case .deepl:
|
case .deepl:
|
||||||
return .init(tag: self.rawValue, displayName: NSLocalizedString("DeepL (Proprietary, Higher Accuracy)", comment: "Dropdown option for selecting DeepL as the translation service."))
|
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."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -197,12 +197,33 @@ 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() {
|
init() {
|
||||||
do {
|
do {
|
||||||
deepl_api_key = try Vault.getPrivateKey(keychainConfiguration: DamusDeepLKeychainConfiguration())
|
deepl_api_key = try Vault.getPrivateKey(keychainConfiguration: DamusDeepLKeychainConfiguration())
|
||||||
} catch {
|
} catch {
|
||||||
deepl_api_key = ""
|
deepl_api_key = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
nokyctranslate_api_key = try Vault.getPrivateKey(keychainConfiguration: DamusNoKYCTranslateKeychainConfiguration())
|
||||||
|
} catch {
|
||||||
|
nokyctranslate_api_key = ""
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func saveLibreTranslateApiKey(_ apiKey: String) throws {
|
private func saveLibreTranslateApiKey(_ apiKey: String) throws {
|
||||||
@@ -213,6 +234,14 @@ class UserSettingsStore: ObservableObject {
|
|||||||
try Vault.deletePrivateKey(keychainConfiguration: DamusLibreTranslateKeychainConfiguration())
|
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 {
|
private func saveDeepLApiKey(_ apiKey: String) throws {
|
||||||
try Vault.savePrivateKey(apiKey, keychainConfiguration: DamusDeepLKeychainConfiguration())
|
try Vault.savePrivateKey(apiKey, keychainConfiguration: DamusDeepLKeychainConfiguration())
|
||||||
}
|
}
|
||||||
@@ -229,6 +258,8 @@ class UserSettingsStore: ObservableObject {
|
|||||||
return URLComponents(string: libretranslate_url) != nil
|
return URLComponents(string: libretranslate_url) != nil
|
||||||
case .deepl:
|
case .deepl:
|
||||||
return deepl_api_key != ""
|
return deepl_api_key != ""
|
||||||
|
case .nokyctranslate:
|
||||||
|
return nokyctranslate_api_key != ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,7 +276,12 @@ struct DamusDeepLKeychainConfiguration: KeychainConfiguration {
|
|||||||
var accountName = "deepl_apikey"
|
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 {
|
func pk_setting_key(_ pubkey: String, key: String) -> String {
|
||||||
return "\(pubkey)_\(key)"
|
return "\(pubkey)_\(key)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ public struct Translator {
|
|||||||
switch userSettingsStore.translation_service {
|
switch userSettingsStore.translation_service {
|
||||||
case .libretranslate:
|
case .libretranslate:
|
||||||
return try await translateWithLibreTranslate(text, from: sourceLanguage, to: targetLanguage)
|
return try await translateWithLibreTranslate(text, from: sourceLanguage, to: targetLanguage)
|
||||||
|
case .nokyctranslate:
|
||||||
|
return try await translateWithNoKYCTranslate(text, from: sourceLanguage, to: targetLanguage)
|
||||||
case .deepl:
|
case .deepl:
|
||||||
return try await translateWithDeepL(text, from: sourceLanguage, to: targetLanguage)
|
return try await translateWithDeepL(text, from: sourceLanguage, to: targetLanguage)
|
||||||
case .none:
|
case .none:
|
||||||
@@ -86,6 +88,29 @@ public struct Translator {
|
|||||||
return response.translations.map { $0.text }.joined(separator: " ")
|
return response.translations.map { $0.text }.joined(separator: " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func translateWithNoKYCTranslate(_ text: String, from sourceLanguage: String, to targetLanguage: String) async throws -> String? {
|
||||||
|
let url = try makeURL("https://translate.nokyctranslate.com", path: "/translate")
|
||||||
|
|
||||||
|
var request = URLRequest(url: url)
|
||||||
|
request.httpMethod = "POST"
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
|
|
||||||
|
struct RequestBody: Encodable {
|
||||||
|
let q: String
|
||||||
|
let source: String
|
||||||
|
let target: String
|
||||||
|
let api_key: String?
|
||||||
|
}
|
||||||
|
let body = RequestBody(q: text, source: sourceLanguage, target: targetLanguage, api_key: userSettingsStore.nokyctranslate_api_key)
|
||||||
|
request.httpBody = try encoder.encode(body)
|
||||||
|
|
||||||
|
struct Response: Decodable {
|
||||||
|
let translatedText: String
|
||||||
|
}
|
||||||
|
let response: Response = try await decodedData(for: request)
|
||||||
|
return response.translatedText
|
||||||
|
}
|
||||||
|
|
||||||
private func makeURL(_ baseUrl: String, path: String) throws -> URL {
|
private func makeURL(_ baseUrl: String, path: String) throws -> URL {
|
||||||
guard var components = URLComponents(string: baseUrl) else {
|
guard var components = URLComponents(string: baseUrl) else {
|
||||||
throw URLError(.badURL)
|
throw URLError(.badURL)
|
||||||
|
|||||||
@@ -65,6 +65,17 @@ struct TranslationSettingsView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if settings.translation_service == .nokyctranslate {
|
||||||
|
SecureField(NSLocalizedString("API Key (required)", comment: "Prompt for optional entry of API Key to use translation server."), text: $settings.nokyctranslate_api_key)
|
||||||
|
.disableAutocorrection(true)
|
||||||
|
.disabled(settings.translation_service != .nokyctranslate)
|
||||||
|
.autocapitalization(UITextAutocapitalizationType.none)
|
||||||
|
|
||||||
|
if settings.nokyctranslate_api_key == "" {
|
||||||
|
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")!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if settings.translation_service != .none {
|
if settings.translation_service != .none {
|
||||||
Toggle(NSLocalizedString("Automatically translate notes", comment: "Toggle to automatically translate notes."), isOn: $settings.auto_translate)
|
Toggle(NSLocalizedString("Automatically translate notes", comment: "Toggle to automatically translate notes."), isOn: $settings.auto_translate)
|
||||||
.toggleStyle(.switch)
|
.toggleStyle(.switch)
|
||||||
|
|||||||
Reference in New Issue
Block a user