---
title: TranslationSession
framework: translation
role: symbol
role_heading: Class
path: translation/translationsession
---

# TranslationSession

A class that performs translations between a pair of languages.

## Declaration

```swift
class TranslationSession
```

## Overview

Overview This class provides a flexible way for you to translate one or more lines of text at a time. There are two ways in which you can initialize a TranslationSession. One way you can obtain an instance of this class is by adding a .translationTask() to a SwiftUI view within your app. You can either add a translationTask(_:action:) or a translationTask(source:target:action:) function to the SwiftUI view containing the content you want to translate, like a Text view. After adding the task, the function passes you an instance of a translation session in its action closure. With this instance, you can use one or more of the translate functions to translate a single string or multiple strings of text. Another way for contexts where there’s no UI, you can directly initialize the TranslationSession using init(installedSource:target:) to translate between languages. This initializer requires that you specify which source language you use and throws an error if the languages aren’t already installed on the person’s device. The following example demonstrates how to translate a single string of text within a SwiftUI view: struct TranslationExample: View {     var sourceText: String     var sourceLanguage: Locale.Language?     var targetLanguage: Locale.Language?

@State private var targetText: String?

var body: some View {         Text(targetText ?? sourceText)             .translationTask(                 source: sourceLanguage,                 target: targetLanguage             ) { session in                 do {                     let response = try await session.translate(sourceText)                     targetText = response.targetText                 } catch {                     // Handle error.                 }             }     } } note: All translations using the TranslationSession class are processed on the user’s device. Apple may collect API usage and performance metrics including the app bundle ID and the original and translated language, but this data does not include the original or translated content.

## Topics

### Initalizing a translation session

- [init(installedSource:target:)](translation/translationsession/init(installedsource:target:).md)
- [init(installedSource:target:preferredStrategy:)](translation/translationsession/init(installedsource:target:preferredstrategy:).md)

### Preparing for translation

- [TranslationSession.Configuration](translation/translationsession/configuration.md)
- [prepareTranslation()](translation/translationsession/preparetranslation().md)

### Getting the language configuration

- [sourceLanguage](translation/translationsession/sourcelanguage.md)
- [targetLanguage](translation/translationsession/targetlanguage.md)

### Translating the text

- [translate(_:)](translation/translationsession/translate(_:)-59zi2.md)
- [translate(_:)](translation/translationsession/translate(_:)-4m20l.md)
- [translate(batch:)](translation/translationsession/translate(batch:).md)
- [translations(from:)](translation/translationsession/translations(from:).md)
- [TranslationSession.Request](translation/translationsession/request.md)
- [TranslationSession.Response](translation/translationsession/response.md)
- [TranslationSession.BatchResponse](translation/translationsession/batchresponse.md)

### Accessing the session properties

- [canRequestDownloads](translation/translationsession/canrequestdownloads.md)
- [isReady](translation/translationsession/isready.md)
- [preferredStrategy](translation/translationsession/preferredstrategy.md)

### Canceling a translation session

- [cancel()](translation/translationsession/cancel().md)

### Configuring translation strategy

- [TranslationSession.Strategy](translation/translationsession/strategy.md)

## See Also

### Essentials

- [Translating text within your app](translation/translating-text-within-your-app.md)
- [translationPresentation(isPresented:text:attachmentAnchor:arrowEdge:replacementAction:)](swiftui/view/translationpresentation(ispresented:text:attachmentanchor:arrowedge:replacementaction:).md)
- [translationTask(_:action:)](swiftui/view/translationtask(_:action:).md)
- [translationTask(source:target:action:)](swiftui/view/translationtask(source:target:action:).md)
