Created
September 20, 2024 22:12
-
-
Save apatronl/311265bac4219688f0df28402e5427a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func translateAllMessages(using session: TranslationSession) async { | |
Task { | |
let requests: [TranslationSession.Request] = messages.enumerated().map { (index, message) in | |
// Assign each request a client identifier. | |
.init(sourceText: message.text, clientIdentifier: "\(index)") | |
} | |
do { | |
for try await response in session.translate(batch: requests) { | |
// Use the returned client identifier (the index) to map the request to the response. | |
guard let index = Int(response.clientIdentifier ?? "") else { continue } | |
messages[index].translation = response.targetText | |
} | |
} catch { | |
// Handle any errors. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment