translationTask does not execute when content appears

The documentation for translationTask(source:target:action:) says it should translate when content appears, but this isn't happening. I’m only able to translate when I manually associate that task with a configuration, and instantiate the configuration.

Here’s the complete source code:

import SwiftUI
import Translation

struct ContentView: View {

    @State private var originalText = "The orange fox jumps over the lazy dog"

    @State private var translationTaskResult = ""
    @State private var translationTaskResult2 = ""

    @State private var configuration: TranslationSession.Configuration?

    var body: some View {
        List {
        	// THIS DOES NOT WORK
            Section {
                Text(translationTaskResult)
                    .translationTask { session in
                        Task { @MainActor in
                            do {
                                let response = try await session.translate(originalText)
                                translationTaskResult = response.targetText
                            } catch { print(error) }
                        }
                    }
            }

			// THIS WORKS
            Section {
                Text(translationTaskResult2)
                    .translationTask(configuration) { session in
                        Task { @MainActor in
                            do {
                                let response = try await session.translate(originalText)
                                translationTaskResult2 = response.targetText
                            } catch { print(error) }
                        }
                    }

                Button(action: {
                    if configuration == nil {
                        configuration = TranslationSession.Configuration()
                        return
                    }
                    configuration?.invalidate()
                }) { Text("Translate") }
            }
        }
    }
}

How can I automatically translate a given text when it appears using the new translationTask API?

Answered by Engineer in 792701022

Thank you for the post. This indeed seems like a bug; could you use Feedback Assistant to report this and include the report number here so we can use your bug to track this? That way you'll also be updated on status. A sysdiagnose and full sample project aren't necessary, just the description and code snippet you have here would be great.

In the meantime I can think of two workarounds:

  1. Use translationTask(_:action:) and either directly pass in a Configuration, or if you store it in @State give the configuration some initial value.
  2. Still use translationTask(source:target:action:), but provide a non-nil value for source and/or target.
Accepted Answer

Thank you for the post. This indeed seems like a bug; could you use Feedback Assistant to report this and include the report number here so we can use your bug to track this? That way you'll also be updated on status. A sysdiagnose and full sample project aren't necessary, just the description and code snippet you have here would be great.

In the meantime I can think of two workarounds:

  1. Use translationTask(_:action:) and either directly pass in a Configuration, or if you store it in @State give the configuration some initial value.
  2. Still use translationTask(source:target:action:), but provide a non-nil value for source and/or target.

Thanks! I’ve created FB14079140 and I’ll give the workarounds a try in the meantime.

Thank you for filing this feedback! This should now be fixed in Developer Beta 5 of iOS 18 and macOS 15. Please let us know if you're still seeing any issues with the API.

translationTask does not execute when content appears
 
 
Q