<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 15.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/translationTask(source:target:action:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewP013_Translation_aB0E15translationTask6source6target6actionQr10Foundation6LocaleV8LanguageVSg_ANy0D00D7SessionCYactF"
  },
  "title" : "translationTask(source:target:action:)"
}
-->

# translationTask(source:target:action:)

Adds a task to perform before this view appears or when the specified source or target
languages change.

```
nonisolated func translationTask(source: Locale.Language? = nil, target: Locale.Language? = nil, action: @escaping (TranslationSession) async -> Void) -> some View
```

## Parameters

`source`

The language the source content is in. If this is `nil`, the session tries to
identify the language and prompts the person to pick the source language if it’s unclear. All
text translated within the session should be in the same source language. Changing this
value cancels previous tasks and creates a new session to perform translations again.

`target`

The language to translate content into. A `nil` value means the session picks a
target according to the person’s `Locale.preferredLanguages` and the `source`.
Changing this value cancels previous tasks and creates a new one to perform
translations again.

`action`

A closure that runs when the view first appears and when `source` or
`target` change.  It provides a <doc://com.apple.documentation/documentation/Translation/TranslationSession> instance to perform
one or multiple translations.

## Discussion

This task provides an instance of <doc://com.apple.documentation/documentation/Translation/TranslationSession>  to use to perform translations.

To perform new translations with the same `source` and `target` language, use <doc://com.apple.documentation/documentation/SwiftUI/View/translationTask(_:action:)>
and call <doc://com.apple.documentation/documentation/Translation/TranslationSession/Configuration/invalidate()>.

For example, you can translate when content appears:

```
 struct ContentView: View {
     var sourceText = "Hallo, Welt!"
     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
                 Task { @MainActor in
                     do {
                         let response = try await session.translate(sourceText)
                         targetText = response.targetText
                     } catch {
                         // Handle any errors.
                     }
                 }
             }
     }
 }
```

The system throws a `fatalError` if you use a <doc://com.apple.documentation/documentation/Translation/TranslationSession> instance after the attached view disappears or if you use it after
changing the `source` or `target` parameters. This causes the `action` closure to provide a new instance.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)