<!--
{
  "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(_:action:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewP013_Translation_aB0E15translationTask_6actionQr0D00D7SessionC13ConfigurationVSg_yAIYactF"
  },
  "title" : "translationTask(_:action:)"
}
-->

# translationTask(_:action:)

Adds a task to perform before this view appears or when the translation configuration changes.

```
nonisolated func translationTask(_ configuration: TranslationSession.Configuration?, action: @escaping (TranslationSession) async -> Void) -> some View
```

## Parameters

`configuration`

A configuration for a <doc://com.apple.documentation/documentation/Translation/TranslationSession>. When this configuration is non-`nil` and changes, the `action` runs providing an instance of
the session to perform translations.

`action`

This closure runs when the <doc://com.apple.documentation/documentation/Translation/TranslationSession/Configuration> is non-`nil` and changes. If the configuration is initially non-`nil`, it calls the action closure when the view appears and provides a session to perform one or more translations.

## Discussion

This task provides an instance of <doc://com.apple.documentation/documentation/Translation/TranslationSession> to use in translations.
Whenever <doc://com.apple.documentation/documentation/Translation/TranslationSession/Configuration>  changes and isn’t `nil`, the closure `action` runs,
providing a session instance to perform one or more translations.

For example, you can create a configuration in response to a button press to initiate the translation:

```
struct ContentView: View {
    @State private var sourceText = "Hallo, Welt!"
    var sourceLanguage: Locale.Language?
    var targetLanguage: Locale.Language?

    @State private var targetText: String?
    @State private var configuration: TranslationSession.Configuration?

    var body: some View {
        VStack {
            Text(targetText ?? sourceText)
            Button("Translate") {
                guard configuration == nil else {
                    configuration?.invalidate()
                    return
                }

                 configuration = TranslationSession.Configuration(
                    source: sourceLanguage,
                    target: targetLanguage)
            }
            .translationTask(configuration) { 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 configuration. This causes the `action` closure
to provide a new session 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)