translations(from:)
Translates multiple strings of text of the same language, returning the results all at once when complete.
Declaration
func translations(from batch: [TranslationSession.Request]) async throws -> [TranslationSession.Response]Parameters
- batch:
The array of requests to translate.
Return Value
An array of responses containing the text translations matching the order they were sent.
Discussion
This function translates multiple strings as a batch 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). This method takes longer to return than translate(batch:), but it has the advantage of not mapping translation requests to responses. The responses return in the same order the requests are sent.
If the required languages for translation have already downloaded and the source language is clear, this function returns results without showing any further prompts to the person.
If the source or target language aren’t installed, the framework asks the person for permission to download the languages. During download a progress indicator displays. After the download 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.