A type containing the information to use when performing a translation.
Specify the source and target languages to use in a translation session with this object.
Initialize an instance of this type using the ``init(source:target:)`` and
passing in the `source` and `target` languages.
When you pass this configuration into the <doc://com.apple.documentation/documentation/swiftui/view/translationtask(_:action:)> function,
the framework uses the languages you specify for translation.
To re-run a translation, store the configuration object as state in your SwiftUI view by using the `State` property wrapper.
Then change one of the configuration properties (such as the source or target language) to re-run
the translation on a new pair of languages.
You can also call ``invalidate()`` on the configuration instance to re-run the translation using the same languages with new content to translate.
When you do, the action closure of <doc://com.apple.documentation/documentation/swiftui/view/translationtask(_:action:)>
runs and the framework translates the text.
The following example demonstrates how to initiate a new translation from a button press:
struct TranslationExample: View {
var sourceText: String
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 = TranslationSession.Configuration(
source: sourceLanguage,
target: targetLanguage)
return
}
self.configuration.invalidate()
}
}
.translationTask(configuration) { session in
do {
let response = try await session.translate(sourceText)
targetText = response.targetText
} catch {
// Handle error.
}
}
}
}
Members (10)
method · iOS 18.0+
invalidate
TypeScript
invalidate(): void
Swift declaration
mutating func invalidate()
Invalidate the current translation session and re-run it with new content.
Call this method when you want to translate new content using the same source and target languages.
When you do, it causes the <doc://com.apple.documentation/documentation/swiftui/view/translationtask(_:action:)> function to call
its `action` closure and translate the content again.
get · iOS 26.4+
preferredStrategy
TypeScript
get preferredStrategy(): TranslationSession_Strategy
Swift declaration
var preferredStrategy: TranslationSession.Strategy
The translation approach for this configuration.
set · iOS 26.4+
preferredStrategy
TypeScript
set preferredStrategy(value: TranslationSession_Strategy)
Swift declaration
var preferredStrategy: TranslationSession.Strategy
The translation approach for this configuration.
get · iOS 18.0+
source
TypeScript
get source(): (_Foundation_Locale_Language) | null
Swift declaration
var source: Locale.Language?
The language to translate content from.
If left to its default value of `nil`, the session tries to identify the
source language automatically, and prompts the person to choose a source language if it's unclear.
Changing this value cancels the previous task and creates a new one.
set · iOS 18.0+
source
TypeScript
set source(value: (_Foundation_Locale_Language) | null)
Swift declaration
var source: Locale.Language?
The language to translate content from.
If left to its default value of `nil`, the session tries to identify the
source language automatically, and prompts the person to choose a source language if it's unclear.
Changing this value cancels the previous task and creates a new one.
Creates a configuration from a source and target language.
When creating a translation session configuration
it's best to use `Locale.Language` values that return from
``LanguageAvailability/supportedLanguages``.
When you pass other `Locale.Language` values, the framework tries to match to one of
these supported languages.
- Parameters:
- source: The language the source content is in. If `nil` the session tries
to identify the language, and prompt the person to pick the source language if it's
unclear. All text translated with this session should be in the same source language.
- target: The language to translate content into. If `nil` the session tries to
pick a target language according to the person's <doc://com.apple.documentation/documentation/foundation/locale/preferredlanguages>,
and the `source`.
- preferredStrategy: The translation approach to use.
``TranslationSession/Strategy/highFidelity`` uses Apple Intelligence
models when available, or falls back to ``TranslationSession/Strategy/lowLatency``
when those models aren't available. The `lowLatency` strategy uses traditional
models and works on all devices.
Creates a configuration from a source and target language.
When creating a translation session configuration
it's best to use `Locale.Language` values that return from
``LanguageAvailability/supportedLanguages``.
When you pass other `Locale.Language` values, the framework tries to match to one of
these supported languages.
- Parameters:
- source: The language the source content is in. If `nil` the session tries
to identify the language, and prompt the person to pick the source language if it's
unclear. All text translated with this session should be in the same source language.
- target: The language to translate content into. If `nil` the session tries to
pick a target language according to the person's <doc://com.apple.documentation/documentation/foundation/locale/preferredlanguages>,
and the `source`.
get · iOS 18.0+
target
TypeScript
get target(): (_Foundation_Locale_Language) | null
Swift declaration
var target: Locale.Language?
The language to translate content into.
If left to its default value of `nil`, the session picks a target language
according to the `source` and the person's <doc://com.apple.documentation/documentation/foundation/locale/preferredlanguages>.
Changing this value cancels the previous tasks and creates a new one.
set · iOS 18.0+
target
TypeScript
set target(value: (_Foundation_Locale_Language) | null)
Swift declaration
var target: Locale.Language?
The language to translate content into.
If left to its default value of `nil`, the session picks a target language
according to the `source` and the person's <doc://com.apple.documentation/documentation/foundation/locale/preferredlanguages>.
Changing this value cancels the previous tasks and creates a new one.
get · iOS 18.0+
version
TypeScript
get version(): number
Swift declaration
var version: Int { get }
A value the equals function uses to represent change in the configuration instance.
This integer changes when you call ``invalidate()`` on an instance of your translation session.