translate(batch:)
Translates multiple strings of text of the same language, returning a sequence of responses as they’re available.
Declaration
func translate(batch: [TranslationSession.Request]) -> TranslationSession.BatchResponseParameters
- batch:
The array of requests to translate.
Return Value
An AsyncSequence of responses containing the text translations.
Discussion
This function translates multiple strings of text incrementally and might display different UI depending on the state of the translation. The app doesn’t need to install the required languages before calling this method.
Pass in the strings of text you want to translate as an array of the TranslationSession.Request type. To create requests for attributed strings, use Request/init(sourceText:clientIdentifier:)-(AttributedString). Set the clientIdentifier on each request so you can match the request with the response as the translations incrementally return.
If the required languages for translation have already downloaded and the source language is clear, this function returns results without showing any UI to the person.
If the source or target language aren’t installed, the framework asks the person for permission to download the languages. During the download a progress indicator displays. After it completes, the framework performs the translation.
If the sourceLanguage is nil and the framework can’t detect the source language from the content, the framework prompts the person to choose the source language.
The framework only supports string translations of the same language. The strings must either match the sourceLanguage you set in the configuration, or if the sourceLanguage is nil, be of the same language.
This function throws an Error if:
The person doesn’t agree to downloading the languages
The person dismisses the progress view during language downloads
TranslationSession fails system validation
The session doesn’t allow requesting downloads and languages aren’t installed
You already cancelled the session
Something goes wrong during translation
If a person dismisses the progress view while the languages download, the system throws a userCancelled error, and the languages continue to download in the background.