IceCubesApp/Packages/Network/Sources/Network/DeepLClient.swift

59 lines
1.9 KiB
Swift
Raw Normal View History

2023-01-21 08:58:38 +00:00
import Foundation
import Models
2023-01-21 08:58:38 +00:00
2024-01-26 12:01:23 +00:00
public struct DeepLClient: Sendable {
public enum DeepLError: Error {
case notFound
}
2023-02-12 15:29:41 +00:00
2023-03-14 17:50:19 +00:00
private var deeplUserAPIKey: String?
private var deeplUserAPIFree: Bool
private var endpoint: String {
"https://api\(deeplUserAPIFree && (deeplUserAPIKey != nil) ? "-free" : "").deepl.com/v2/translate"
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
private var authorizationHeaderValue: String {
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 11:27:21 +00:00
"DeepL-Auth-Key \(deeplUserAPIKey ?? "")"
2023-01-21 08:58:38 +00:00
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
public struct Response: Decodable {
public struct Translation: Decodable {
public let detectedSourceLanguage: String
public let text: String
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
public let translations: [Translation]
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}
2023-01-22 05:38:30 +00:00
2023-03-14 17:50:19 +00:00
public init(userAPIKey: String?, userAPIFree: Bool) {
deeplUserAPIKey = userAPIKey
deeplUserAPIFree = userAPIFree
}
2023-01-22 05:38:30 +00:00
public func request(target: String, text: String) async throws -> Translation {
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 11:27:21 +00:00
var components = URLComponents(string: endpoint)!
var queryItems: [URLQueryItem] = []
queryItems.append(.init(name: "text", value: text))
queryItems.append(.init(name: "target_lang", value: target.uppercased()))
components.queryItems = queryItems
var request = URLRequest(url: components.url!)
request.httpMethod = "POST"
request.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let (result, _) = try await URLSession.shared.data(for: request)
let response = try decoder.decode(Response.self, from: result)
if let translation = response.translations.first {
return .init(content: translation.text.removingPercentEncoding ?? "",
detectedSourceLanguage: translation.detectedSourceLanguage,
provider: "DeepL.com")
2023-01-21 08:58:38 +00:00
}
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 11:27:21 +00:00
throw DeepLError.notFound
2023-01-21 08:58:38 +00:00
}
}