Translation Framework

Sep 18 2024 · Swift 6.0, iOS 18.0, Xcode 16.0 beta 3

Lesson 03: Advanced Translation Control with TranslationSession

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous lesson, you learned how to translate multiple texts in a single session and display the result at once. Imagine a situation where you have an indefinite number of texts to translate and want to display them as soon as the translation is available.

extension ViewModel {
  func translateSequence(using session: TranslationSession) async {

  }
}
let cafeNames = cafeReviews.compactMap { $0.name }
let requests: [TranslationSession.Request] = cafeNames.enumerated().map
  { (index, string) in
    .init(sourceText: string, clientIdentifier: "\(index)")
}
do {
  for try await response in session.translate(batch: requests) {
    guard let index = Int(response.clientIdentifier ?? "") else { continue }
  }
} catch {
  print("Error executing translateSequence: \(error)")
}
cafeReviews[index].name = response.targetText
.translationTask(configuration) { session in
  Task {
    await viewModel.translateSequence(using: session)
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion