One Logo Pool Ball
Translation · IOS API REFERENCE

TranslationSession.Request

struct · iOS 18.0+ · SDK 27.1

struct Request

A translation request containing a single item of text to translate. Create a translation request to translate a string of text. Initialize the request using ``init(sourceText:clientIdentifier:)-(String,_)``. Set the ``sourceText`` to the string of text you want to translate. Then pass that request in an array to one of the batch translation functions. Keep track of the correspondence between the responses and requests by setting the ``clientIdentifier`` on the sent request, then matching it with the ``Response/clientIdentifier`` of the received response when the translation completes.

Members (8)

init · iOS 26.4+

init

TypeScript
static initSourceTextClientIdentifier(sourceText: _Foundation_AttributedString, clientIdentifier?: (string) | null): TranslationSession_Request
Swift declaration
init(sourceText: AttributedString, clientIdentifier: String? = nil)

Creates a request for translating a single attributed string. The plain text is automatically extracted and stored in ``sourceText``.

init · iOS 18.0+

init

TypeScript
static initSourceTextClientIdentifier35269(sourceText: string, clientIdentifier?: (string) | null): TranslationSession_Request
Swift declaration
init(sourceText: String, clientIdentifier: String? = nil)

Creates a request for translating a single string of text.

get · iOS 18.0+

sourceText

TypeScript
get sourceText(): string
Swift declaration
var sourceText: String

The plain text input to translate.

set · iOS 18.0+

sourceText

TypeScript
set sourceText(value: string)
Swift declaration
var sourceText: String

The plain text input to translate.

get · iOS 18.0+

clientIdentifier

TypeScript
get clientIdentifier(): (string) | null
Swift declaration
var clientIdentifier: String?

An optional unique identifier to associate a translation request with its response. When you set this identifier, it returns the same value in the corresponding ``TranslationSession/Response``.

set · iOS 18.0+

clientIdentifier

TypeScript
set clientIdentifier(value: (string) | null)
Swift declaration
var clientIdentifier: String?

An optional unique identifier to associate a translation request with its response. When you set this identifier, it returns the same value in the corresponding ``TranslationSession/Response``.

get · iOS 26.4+

attributedSourceText

TypeScript
get attributedSourceText(): (_Foundation_AttributedString) | null
Swift declaration
var attributedSourceText: AttributedString? { get set }

The text to translate, including styling like bold text, italics, and hyperlink data for linking between data sources. When you translate an attributed string, the framework makes a best effort to preserve formatting and attributes such as accessibility instructions or timestamps for synchronized captions or lyrics. The following example marks coordinates that VoiceOver spells out. When translated, VoiceOver spells out the translated coordinates character by character instead of reading them as full numbers: ```swift import SwiftUI import Translation var attributedSource = AttributedString("The GPS coordinates are N37°47'11.5\" W122°25'34.2\"") // Find the coordinate's range. if let coordinatesRange = attributedSource.range(of: "N37°47'11.5\" W122°25'34.2\"") { // Mark coordinates for VoiceOver to spell out. attributedSource[coordinatesRange].accessibilitySpeechSpellsOutCharacters = true } // In your translation task translate the attributed text. .translationTask(configuration) { session in do { let response = try await session.translate(attributedSource) // Preserve the spell out attribute for the Spanish translation. // VoiceOver reads: "Las coordenadas GPS son N - tres - siete - // grados, and so on." if var translated = response.attributedTargetText { // Set the language so VoiceOver uses the correct voice. translated.languageIdentifier = response.targetLanguage.languageCode?.identifier } } catch { // Handle any errors. } } ``` When translating transcribed speech, the framework preserves time ranges so you can synchronize translated captions to the original audio. The Speech framework provides time ranges when you transcribe audio with <doc://com.apple.documentation/documentation/speech/sfspeechrecognizer>. The following example shows setting time ranges and reads them back from the translation: ```swift import SwiftUI import Translation import CoreMedia import Speech import AVFoundation // Create a transcription with time ranges. var transcription = AttributedString("Hello World") // Set time ranges indicating when each word was spoken. if let helloRange = transcription.range(of: "Hello") { transcription[helloRange][AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] = CMTimeRange(start: CMTime(seconds: 0.0, preferredTimescale: 600), duration: CMTime(seconds: 0.6, preferredTimescale: 600)) } if let worldRange = transcription.range(of: "World") { transcription[worldRange][AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] = CMTimeRange(start: CMTime(seconds: 0.6, preferredTimescale: 600), duration: CMTime(seconds: 0.15, preferredTimescale: 600)) } // In your translation task translate the transcription with the specified range in place. .translationTask(configuration) { session in do { let response = try await session.translate(transcription) // Read preserved time ranges to synchronize captions to audio playback. if let translated = response.attributedTargetText { for run in translated.runs { if let timeRange = run[AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] { let start = CMTimeGetSeconds(timeRange.start) let end = CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange)) // Highlight the translated word when audio is between start and end. } } } } catch { // Handle any errors. } } ```

set · iOS 26.4+

attributedSourceText

TypeScript
set attributedSourceText(value: (_Foundation_AttributedString) | null)
Swift declaration
var attributedSourceText: AttributedString? { get set }

The text to translate, including styling like bold text, italics, and hyperlink data for linking between data sources. When you translate an attributed string, the framework makes a best effort to preserve formatting and attributes such as accessibility instructions or timestamps for synchronized captions or lyrics. The following example marks coordinates that VoiceOver spells out. When translated, VoiceOver spells out the translated coordinates character by character instead of reading them as full numbers: ```swift import SwiftUI import Translation var attributedSource = AttributedString("The GPS coordinates are N37°47'11.5\" W122°25'34.2\"") // Find the coordinate's range. if let coordinatesRange = attributedSource.range(of: "N37°47'11.5\" W122°25'34.2\"") { // Mark coordinates for VoiceOver to spell out. attributedSource[coordinatesRange].accessibilitySpeechSpellsOutCharacters = true } // In your translation task translate the attributed text. .translationTask(configuration) { session in do { let response = try await session.translate(attributedSource) // Preserve the spell out attribute for the Spanish translation. // VoiceOver reads: "Las coordenadas GPS son N - tres - siete - // grados, and so on." if var translated = response.attributedTargetText { // Set the language so VoiceOver uses the correct voice. translated.languageIdentifier = response.targetLanguage.languageCode?.identifier } } catch { // Handle any errors. } } ``` When translating transcribed speech, the framework preserves time ranges so you can synchronize translated captions to the original audio. The Speech framework provides time ranges when you transcribe audio with <doc://com.apple.documentation/documentation/speech/sfspeechrecognizer>. The following example shows setting time ranges and reads them back from the translation: ```swift import SwiftUI import Translation import CoreMedia import Speech import AVFoundation // Create a transcription with time ranges. var transcription = AttributedString("Hello World") // Set time ranges indicating when each word was spoken. if let helloRange = transcription.range(of: "Hello") { transcription[helloRange][AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] = CMTimeRange(start: CMTime(seconds: 0.0, preferredTimescale: 600), duration: CMTime(seconds: 0.6, preferredTimescale: 600)) } if let worldRange = transcription.range(of: "World") { transcription[worldRange][AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] = CMTimeRange(start: CMTime(seconds: 0.6, preferredTimescale: 600), duration: CMTime(seconds: 0.15, preferredTimescale: 600)) } // In your translation task translate the transcription with the specified range in place. .translationTask(configuration) { session in do { let response = try await session.translate(transcription) // Read preserved time ranges to synchronize captions to audio playback. if let translated = response.attributedTargetText { for run in translated.runs { if let timeRange = run[AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] { let start = CMTimeGetSeconds(timeRange.start) let end = CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange)) // Highlight the translated word when audio is between start and end. } } } } catch { // Handle any errors. } } ```

Skipped declarations

No declarations were skipped in this snapshot.

All Translation types →