Skip to content

Instantly share code, notes, and snippets.

@apatronl
Created September 20, 2024 22:12
Show Gist options
  • Save apatronl/311265bac4219688f0df28402e5427a3 to your computer and use it in GitHub Desktop.
Save apatronl/311265bac4219688f0df28402e5427a3 to your computer and use it in GitHub Desktop.
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