AI-generated Key Takeaways
-
Translatoris a class that provides text translation capabilities between specified languages. -
You can obtain a
Translatorinstance using thetranslator(options:)method with desired configurations. -
The
translate(_:completion:)method performs the actual translation, returning the result or an error via a completion handler. -
To ensure model availability, utilize
downloadModelIfNeeded(completion:)ordownloadModelIfNeeded(with:completion:)to download necessary files before translating.
Translator
class Translator : NSObjectA class that translates the given input text.
-
Gets a
Translatorinstance for the specified options. This method is thread safe.Declaration
Swift
class func translator(options: MLKTranslatorOptions) -> TranslatorParameters
optionsThe options for the translator.
Return Value
A
Translatorinstance that provides translation with the given options. -
Translates the given text from the source language into the target language.
This method will return an error if the model files have not been downloaded.
Declaration
Swift
func translate(_ text: String, completion: @escaping TranslatorCallback)Parameters
textA string in the source language.
completionHandler to call back on the main queue with the translation result or error.
-
Downloads the model files required for translation, if they are not already downloaded.
Declaration
Swift
func downloadModelIfNeeded(completion: @escaping TranslatorDownloadModelIfNeededCallback)Parameters
completionHandler to call back on the main queue with an error, if any.
-
Downloads the model files required for translation when the given conditions are met. If model has already been downloaded, completes without additional work.
Declaration
Swift
func downloadModelIfNeeded(with conditions: MLKModelDownloadConditions, completion: @escaping TranslatorDownloadModelIfNeededCallback)Parameters
conditionsThe downloading conditions for the translate model.
completionHandler to call back on the main queue with an error, if any.
-
Unavailable.