Instruction 2

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

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

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

Unlock now

TranslationSession is a powerful API you can use to initiate a translation process, check translation eligibility from a source language to a target language, batch-translate multiple chunks of text at once and handle any errors that occur during the process.

An important task is to check the language availability. There is a class specifically for this task.

let availability = LanguageAvailability()
let status = await availability.status(from: translateFrom,
  to: translateTo)

The LanguageAvailability class checks whether the framework supports the language pairing to initiate a TranslationSession and returns with a status, such as supported or unsupported.

switch status {
case .installed, .supported:
  isTranslationSupported = true
case .unsupported:
  isTranslationSupported = false
@unknown default:
  print("Translation support status for the selected
    language pair is unknown")
}

Note that the Translation API doesn’t let you execute a translation session between the same language although you can translate between dialects such as English (en-US) and English (en-GB).

Listening to Configuration Changes

A TranslationSession.Configuration is a type used to pass information to perform the current translation. It keeps track of the source language and the target language that is used in the current translation.

@State private var configuration: TranslationSession.Configuration?
private func translateAll() {
  if configuration == nil {
    // Set the language pairing.
    configuration = .init(source: viewModel.translateFrom,
      target: viewModel.translateTo)
  } else {
    // Invalidate the previous configuration.
    configuration?.invalidate()
  }
}
.translationTask(configuration) { session in
}

Performing Batch Translations

A TranslationSession helps you translate a bunch of text at once. You use the TranslationSession to perform a batch job of translating multiple strings at the same time. You get a combined result using the session created from the translationTask callback.

Review to translate
Xewoed fu xpoyfcoxe

extension ViewModel {
  func translateAllAtOnce(review: Review,
    using session: TranslationSession) async -> Review {

      let requests: [TranslationSession.Request] = [
        TranslationSession.Request(sourceText: review.description),
        TranslationSession.Request(sourceText: review.highlights)
      ]
  do {
    let responses = try await session.translations(from: requests)
    let translatedReview = Review(
      id: review.id,
      name: review.name,
      address: review.address,
      description: responses.first?.targetText ?? review.description,
      highlights: responses.last?.targetText ?? review.highlights,
      price_range: review.price_range,
      rating: review.rating
    )
    return translatedReview
  } catch {
    print("Error executing translateAllAtOnce: \(error)")
    return review
  }
}
translationTask(configuration) { session in
  guard let translatableReview = review else {
    return
  }
  Task {
    review = await viewModel.translateAllAtOnce(
      review: translatableReview, using: session)
  }
}
See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Demo