translate(_:)
Translates a single string of text.
Declaration
func translate(_ string: String) async throws -> TranslationSession.ResponseParameters
- string:
The string of plain text to translate.
Return Value
The response containing the text translation.
Discussion
This function translates a single line of text 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.
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.
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.