release v1.2 (#5)

- Support for api's that use keys
- Removed idle instances.
---------
Co-authored-by: Viktor Kalyniuk <@>
Co-authored-by: Simon Rieger <simono41@brothertec.eu>
This commit is contained in:
Viktor 2023-08-23 13:06:15 +03:00 committed by GitHub
parent 5ac00486dc
commit 3474967700
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 11 deletions

View file

@ -8,14 +8,15 @@
import Foundation
struct TranslationParsing {
static func parse(url: String, text: String, inputLanguage: Languages, outputLanguage: Languages, handler: @escaping(TranslationDecode) -> Void) {
static func parse(url: String, text: String, inputLanguage: Languages, outputLanguage: Languages, key: String, handler: @escaping(TranslationDecode) -> Void) {
if let url = URL(string: url) {
let session = URLSession.shared
let parameters: [String: String] = [
"q": "\(text)",
"source": "\(inputLanguage.rawValue)",
"target": "\(outputLanguage.rawValue)",
"format": "text"
"format": "text",
"api_key": "\(key)"
]
var request = URLRequest(url: url)

View file

@ -11,6 +11,7 @@ import SwiftUI
class InstanceURL: ObservableObject {
@AppStorage(Keys.libreTranslateDefaultURL) var defaultURL: Links.LibreTranslateMirrors = Links.LibreTranslateMirrors.de
@AppStorage(Keys.libreTranslateSelfHostURL) var selfHostURL: String = ""
@AppStorage(Keys.libreTranslateSelfHostKey) var selfHostKey: String = ""
func getURL() -> String {
if selfHostURL.isEmpty {
@ -19,4 +20,8 @@ class InstanceURL: ObservableObject {
return selfHostURL
}
func getKey() -> String {
return selfHostKey
}
}

View file

@ -10,6 +10,7 @@ import Foundation
struct Keys {
static let bookmarks: String = "bookmarks"
static let libreTranslateSelfHostURL: String = "libreTranslateSelfHostURL"
static let libreTranslateSelfHostKey: String = "libreTranslateSelfHostKey"
static let libreTranslateDefaultURL: String = "libreTranslateURL"
static let languageInputSelection: String = "languageInputSelection"
static let languageOutputSelection: String = "languageOutputSelection"

View file

@ -14,9 +14,9 @@ struct Links {
case de = "https://libretranslate.de/translate"
case argosopentech = "https://translate.argosopentech.com/translate"
// case skitzen = "https://translate.api.skitzen.com/translate"
case fortytwo = "https://translate.fortytwo-it.com/translate"
// case fortytwo = "https://translate.fortytwo-it.com/translate"
case terraprint = "https://translate.terraprint.co/translate"
case vern = "https://lt.vern.cc/translate"
// case vern = "https://lt.vern.cc/translate"
}
struct Info {

View file

@ -42,7 +42,9 @@ struct InputBottomButtons: View {
.parse(url: instance.getURL(),
text: textData.input,
inputLanguage: selection.input,
outputLanguage: selection.output) { data in
outputLanguage: selection.output,
key: instance.getKey()
) { data in
DispatchQueue.main.async {
textData.output = data.translatedText
}

View file

@ -27,7 +27,9 @@ struct ChooseLanguagesView: View {
.parse(url: instance.getURL(),
text: textData.input,
inputLanguage: selection.input,
outputLanguage: selection.output) { data in
outputLanguage: selection.output,
key: instance.getKey()
) { data in
DispatchQueue.main.async {
textData.output = data.translatedText
}
@ -48,7 +50,9 @@ struct ChooseLanguagesView: View {
.parse(url: instance.getURL(),
text: textData.input,
inputLanguage: selection.input,
outputLanguage: selection.output) { data in
outputLanguage: selection.output,
key: instance.getKey()
) { data in
DispatchQueue.main.async {
textData.output = data.translatedText
}

View file

@ -12,10 +12,13 @@ struct SelfHostingView: View {
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
@State private var input: String = ""
@State private var inputURL: String = ""
@State private var inputKey: String = ""
private let selfHostingTitle = "Self Hosting"
private let textFieldTitle = "url"
private let selfApiTitle = "API Key (if needed)"
private let textApiTitle = "key"
private let useInstanceTitle = "Use default instance"
private let infoTitle = "Info"
private let instructionTitle = "Instruction"
@ -24,9 +27,18 @@ struct SelfHostingView: View {
var body: some View {
List {
Section(selfHostingTitle) {
TextField(textFieldTitle, text: $input)
TextField(textFieldTitle, text: $inputURL)
.onAppear {
input = instance.selfHostURL
inputURL = instance.selfHostURL
}
.listRowBackground(Colors.Background.primaryView)
.foregroundColor(Colors.Foreground.label)
}
.textCase(nil)
Section(selfApiTitle) {
TextField(textApiTitle, text: $inputKey)
.onAppear {
inputKey = instance.selfHostKey
}
.listRowBackground(Colors.Background.primaryView)
.foregroundColor(Colors.Foreground.label)
@ -59,7 +71,8 @@ struct SelfHostingView: View {
.background(Colors.Background.mainView)
.toolbar {
Button {
instance.selfHostURL = input
instance.selfHostURL = inputURL
instance.selfHostKey = inputKey
self.mode.wrappedValue.dismiss()
} label: {
Text(doneTitle)