Translation Framework

Oct 5 2025 · Swift 6, iOS 26, Xcode 26

Lesson 03: Advanced Translation Control with TranslationSession

Demo

Episode complete

Play next episode

Next
Transcript

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.

The cafe review app is a perfect example of this! Imagine you’re a Japanese tourist in an English-speaking country, and you want to translate all the cafe names into your native language. You can do that very easily by leveraging the power of the TranslationSession.

To implement that, open the ViewModel class from the starter project and add the following method below as an extension:

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

  }
}

This method uses a TranslationSession originating from your UI. For example, it could be due to a configuration change after source and target language selection. Then it runs according to this order:

Add the following:

let cafeNames = cafeReviews.compactMap { $0.name }

The code filters out all the names from the data source - cafeReviews and puts them in a new array called cafeNames. cafeNames will be used to create translation requests.

let requests: [TranslationSession.Request] = cafeNames.enumerated().map
  { (index, string) in
    .init(sourceText: string, clientIdentifier: "\(index)")
}

Then, the code iterates through cafeNames and creates a request for translating each single string. Each request is assigned a client identifier from their index. The clientIdentifier is an optional unique identifier that associates a translation request with its response.

do {
  for try await response in session.translate(batch: requests) {
    guard let index = Int(response.clientIdentifier ?? "") else { continue }
  }
} catch {
  print("Error executing translateSequence: \(error)")
}

First you setup a do-catch. Inside, the code executes asynchronous session.translate() requests and waits for the responses. As soon as a response is available with a translation, it uses the returned clientIdentifier, the index, to map the request to the corresponding response.

cafeReviews[index].name = response.targetText

Finally, it updates the names of the cafeReviews for each index (aka clientIdentifier) with the targetText from the response, which is the translated text.

You’ll be able to log or handle any errors in the process within the catch block and take necessary action, such as displaying an alert to the users if that happens.

Now open the ContentView class and update the translationTask callback as follows:

.translationTask(configuration) { session in
  Task {
    await viewModel.translateSequence(using: session)
  }
}

This code tells the ViewModel to execute translateSequence for the session to translate all the cafe names in the list.

Run the app on your device.

Tap Translation Menu and go to the Translation Config screen. Choose English (en-US) from the Source Picker and choose Japanese (ja-JP) from the Target Picker.

Now, go back using the navigation menu. In the Cafe Reviews screen, tap Translation Menu and select Translate Cafe Names.

You may see a system popup to download the languages involved in the translation session.

Tap the download icon next to the languages to start the download process. This is a one-off popup, and the downloaded data will be applied for all subsequent uses of this language pair for translation. You can manage the downloaded languages anytime from your device settings later. The language download will proceed in the background.

Try again once the download is complete. You’ll see all the cafe names translated into Japanese this time!

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion